2012-12-27 35 views
0

我试图在我的管理区域中显示特定用户的帐单详细信息。我想要做的只是将用户标识输入到文本字段中,然后按提交,链接到该用户标识的帐单将显示在下表中。这里是我的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我做的所有事情总是错的,所以这可能也是,我该如何解决?

回答

1

嗯,您是否从index行动中致电show_billings?您向我们展示在index操作后呈现的index.html.erb。该表格确实发布到show_billings,但通常会显示show_billing.html.erb

因此,无论是写在你的index.html.erb,如@billings = [],所以你不会得到一个错误,并让show_billings呈现与索引相同的视图。但是,我甚至没有看到真正需要单独采取行动:让搜索表单再次进入索引?无论如何,这是相同的代码。

+0

是的,它是从索引行动,抱歉不包括。我如何让搜索表单再次进入索引?我在def show_billings中加入了redirect_to:action =>'index',但是当我将user_id添加到:user_id字段时,该表不会更新。这太令人沮丧了,因此基于user_id显示简单表格对我来说很难。我们甚至需要def show_billings还是有更好的方法来做到这一点? – railsy

+0

您的索引操作看起来如何?如果你写'form_tag'没有参数,它会回到'form_tag:action => index'的索引。所以不需要show_billings。理想情况下,您将如何执行此操作:使用'admin/users'并点击用户以转到'admin/users /:id'查看用户帐单。更接近轨道。在单个用户的展示视图中,我将显示一个包含所有用户的下拉框以方便切换。祝你好运。 – nathanvda

+0

好的非常感谢您的帮助,至少得到它的工作,将尝试您建议的更改 – railsy

1
def show_billings 
    if params[:user_id] 
     @billings = Billing.where(:user_id => params[:user_id]) 
    else 
     @billings = Billing.where(:user_id => '22') 
end 

让我知道你是如何得到的。

+0

谢谢,但仍然给我同样的错误:未定义的方法'each'为零:NilClass – railsy

+0

尝试@billings = Billing.find(:all)查看数组中的内容并缩小到您需要的范围? – hd1

+0

@ billings在def show_billings中并没有被拿起,我在@ billings = Billing.all中加入了它,并且仍然得到了零:NilClass错误(必须在@符号后添加一个空格以避开SO评论规则思维我试图通知某人叫billings) – railsy

相关问题