【问题标题】:find(params) : ActionView::TemplateError (undefined method 'each' for #haus)查找(参数):ActionView::TemplateError(#haus 的未定义方法“每个”)
【发布时间】:2011-04-26 22:51:21
【问题描述】:

我有一个控制器,它有一个我想传递给 rhtml 的值。我已经设法正确地获取了值,并且基于此,我需要在 rhtml 中执行操作。对于代码 sn-ps,

def getsubtable     
  @subtables = Haus.find(params[:bewohner_id])

从上面的方法,当我调试它时,我可以得到“bewohner_id”的正确值。 (例如:bewohner_id = "2")。现在,我需要以另一种形式 (getsubtable.rhtml) 显示对应于 bewohner_id ="2" 的值列表。所以,如果我得到一个“2”,我会以新的形式显示“a,b,c”

<% for subtable in @subtables %>
  <option value="<%= subtable.bewohner_id %>"><%= subtable.bewohner_id %></option>
<% end %>

但是我得到了一个错误

ActionView::TemplateError (undefined method 'each' for #haus) 

请指导我,我如何使用来自 bewohner_id 的 '2' 的值并在上面的&lt;option value&gt; 中使用它

谢谢

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:
    Haus.find(params[:bewohner_id])
    

    返回单个记录,而不是集合。如果你想要一个你需要使用的集合

    Haus.find_all_by_bewohner_id(params[:bewohner_id])
    

    另外,使用@subtables.each 而不是for subtable in @subtables

    【讨论】:

      猜你喜欢
      • 2012-03-08
      • 2011-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-15
      • 2020-08-14
      • 2012-08-02
      • 2015-06-11
      相关资源
      最近更新 更多