10

I have a Model called Category and other Model Product. They have has_many and belongs_to relation. But code in my view

    <p><%= f.collection_select(:product, :category_id, Category.all, :id, :name)%>

is giving me

 undefined method `merge' for :name:Symbol

Any clue what is wrong with it?

Bhushan Lodha
  • 6,824
  • 7
  • 62
  • 100

1 Answers1

37

Chances are you have something like this:

<%= form_for @product do |f| %>

Because f is already tied to product, you don't need to include it as your first argument, so it should just be:

<%= f.collection_select :category_id, Category.all, :id, :name %>

Or, you could not use f.:

<%= collection_select :product, :category_id, Category.all, :id, :name %>
Dylan Markow
  • 123,080
  • 26
  • 284
  • 201
  • That helped. Thank you. Could you explain conceptually what was wrong there. Help Appreciated. – Bhushan Lodha Nov 16 '11 at 06:27
  • Using form_for eliminates the need to put `:product` on every fields. Read this, it talks about how it expands the `f.` part: http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for – Dylan Markow Nov 16 '11 at 06:37
  • I was encountering this error as I tried to implement a custom form_helper for use with Twitter Bootstrap. I'm kind of new to Rails, and when I found your answer, it really opened my eyes. Thanks, to you, problem fixed! Many, many, thanks!! – MattSlay Jul 30 '12 at 03:51