如何在form_tag中正确地放置collection select



我有一个模型叫做Genre

里面已经存储了很多记录。
我试图从genre表中获取所有记录,并在select中显示它们。请看下面我在视图中编写的代码。
但是这个返回的错误是这样的。我怎么解决这个问题?

未定义的方法' map' for:id:Symbol

我认为

  <%= form_tag communities_path, :method => :get, :class => 'form-search' do %>
   <div class="input-append">
    <%= form.collection_select :id, Genre.all, :id, :name %>
    <button type="submit" class="btn">Search</button>
   </div>
  <% end %>

collection_select需要一个标识符作为第一个参数,并且在提供的示例中没有设置变量form。因此代码应该是:

<%= form_tag communities_path, :method => :get, :class => 'form-search' do %>
 <div class="input-append">
  <%= collection_select :genre, :id, Genre.all, :id, :name %>
  <button type="submit" class="btn">Search</button>
 </div>
<% end %>

最新更新