collection_select方法在Rails 3.1.1中给出错误

Bhu*_*dha 10 form-helpers ruby-on-rails-3 ruby-1.9.2

我有一个名为Category和其他模型产品的模型.他们有has_many和belongs_to关系.但在我看来代码

    <p><%= f.collection_select(:product, :category_id, Category.all, :id, :name)%>
Run Code Online (Sandbox Code Playgroud)

给了我

 undefined method `merge' for :name:Symbol
Run Code Online (Sandbox Code Playgroud)

有什么线索有什么问题吗?

Dyl*_*kow 37

有可能你有这样的事情:

<%= form_for @product do |f| %>
Run Code Online (Sandbox Code Playgroud)

因为f已经绑定了product,你不需要将它包含在你的第一个参数中,所以它应该只是:

<%= f.collection_select :category_id, Category.all, :id, :name %>
Run Code Online (Sandbox Code Playgroud)

或者,您无法使用f.:

<%= collection_select :product, :category_id, Category.all, :id, :name %>
Run Code Online (Sandbox Code Playgroud)