2010-10-18 44 views
1

我有一個控制器,有一個值,我想傳遞給rhtml。我已經設法正確地獲得了這個值,並且基於此,我需要在rhtml中執行一個操作。對於代碼片段,find(params):ActionView :: TemplateError(undefined方法'每個'爲#haus)

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) 

錯誤敬請指導我,怎麼我可以用「2」的值從bewohner_id,並用它在<option value>上述

謝謝

回答

1
Haus.find(params[:bewohner_id]) 

返回單個重線,而不是一個集合。如果你想有一個收集你需要使用

Haus.find_all_by_bewohner_id(params[:bewohner_id]) 

此外,使用@subtables.each而非for subtable in @subtables

相關問題