0

I'm new to Ruby on Rails and I'm having a problem with creating categories.

Ruby on Rails gives this message when trying to to view the categories:

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

2 回答 2

0

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

例如/

def new
   @categories = Category.all
   # .. other code
end
于 2012-12-05T14:09:46.470 回答
0

以下应该工作

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

根据该collection_select方法@categories应该是一个对象数组,错误是在获取值时发生@categoriesnil

于 2012-12-05T14:08:01.150 回答