Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
777 views
in Technique[技术] by (71.8m points)

ruby 1.9.2 - collection_select method gives error in Rails 3.1.1

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 %>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...