Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined method `merge' for :name:Symbol Rails 4.1.x collection_select

I have a form element with:

<%= f.collection_select :race, :id, Race.all, :id, :name, prompt: true %>

This allows you to select your characters race in a text adventure I am creating. The goal is to have a drop down with all available races, select by name and have the params pass the id of that race back.

But when I load the page I get undefined method 'merge' for :name:Symbol.

I looked up the docs and I think I am doing it right, but I guess not? what am i doing wrong?

like image 555
SeekingTruth Avatar asked Nov 25 '14 20:11

SeekingTruth


1 Answers

The f. indicates you are in a form_for block? Which means the method signature of f.collection_select is different to just plain collection_select. The first parameter is automatically supplied by the FormBuilder, so if the :race is an attribute of the form object, which I assume is a Character, you just need:

<%= f.collection_select :race, Race.all, :id, :name, prompt: true %>

See the documentation for the FormBuilder#collection_select method.

like image 81
Andrew France Avatar answered Sep 28 '22 08:09

Andrew France