我试图在我的管理区域中显示特定用户的帐单详细信息。我想要做的只是将用户标识输入到文本字段中,然后按提交,链接到该用户标识的帐单将显示在下表中。这里是我的CAC递给努力迄今:试图在特定用户的表格中显示结果user_id
管理/ index.html.erb
<%= form_tag(:action => "show_billings") do %>
<div class="field">
<p>User ID</p>
<%= text_field_tag :user_id %>
</div>
<div class="actions">
<%= submit_tag "Show Billing For This User", :class => 'btn btn-success' %>
</div>
<% end %>
<table class="table table-hover table-striped table-bordered">
<thead style="background-color: #efefef">
<tr>
<th>Date</th>
<th>Description</th>
<th>Debits</th>
<th>Credits</th>
<th>Balance</th>
</tr>
</thead>
<tbody>
<% @billings.each do |billing| %>
<tr>
<td><%= billing.date %></td>
<td><%= billing.description %></td>
<td><%= number_to_currency(billing.debits) %></td>
<td><%= number_to_currency(billing.credits) %></td>
<td><%= number_to_currency(billing.balance) %></td>
</tr>
<% end %>
</tbody>
</table>
admin_controller.rb
def show_billings
billings = Billing.where(:user_id => params[:user_id])
if billings.nil?
@billings = Billing.where(:user_id => '22')
else
@billings = billings
end
end
我得到了下面的错误,这就是为什么我试图使@billings不是零:
undefined method `each' for nil:NilClass
我不知道def show_billings是否是必要的,仍然是非常新的轨道a nd我做的所有事情总是错的,所以这可能也是,我该如何解决?
是的,它是从索引行动,抱歉不包括。我如何让搜索表单再次进入索引?我在def show_billings中加入了redirect_to:action =>'index',但是当我将user_id添加到:user_id字段时,该表不会更新。这太令人沮丧了,因此基于user_id显示简单表格对我来说很难。我们甚至需要def show_billings还是有更好的方法来做到这一点? – railsy
您的索引操作看起来如何?如果你写'form_tag'没有参数,它会回到'form_tag:action => index'的索引。所以不需要show_billings。理想情况下,您将如何执行此操作:使用'admin/users'并点击用户以转到'admin/users /:id'查看用户帐单。更接近轨道。在单个用户的展示视图中,我将显示一个包含所有用户的下拉框以方便切换。祝你好运。 – nathanvda
好的非常感谢您的帮助,至少得到它的工作,将尝试您建议的更改 – railsy