2012-12-05 54 views
0

我是Ruby on Rails的新手,我在創建類別時遇到問題。食譜中的NoMethodError#new

Ruby on Rails的當試圖查看的類別給出了這樣的消息:

<div class="field"> 
    <%= f.label :category %><br /> 
    <%= f.collection_select :category_id, @categories, :id, :name, :prompt => true %> 
</div> 
<div class="field"> 

回答

0

繼應該工作

<%= f.collection_select :category_id, @categories ? @categories : [], :id, :name, :prompt => true %> 

根據該collection_select方法@categories應的對象的陣列, 該錯誤發生,因爲它的值爲@categories作爲nil

+0

感謝您的幫助。 –

0

您的食譜控制器中的新操作方法似乎需要填充@categories。

e.g/

def new 
    @categories = Category.all 
    # .. other code 
end 
+1

只是一個建議的整理:'Category.all'永遠不會返回零,使得'|| []'不必要。 –

+0

收拾起來,謝謝:) – gef