Forum: Ruby on Rails collection_select

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
74bd868b70df3590bf94cf7abe563ec4?d=identicon&s=25 Jason (Guest)
on 2007-02-01 21:33
What am I doing wrong here?  I'm trying to create a select box with a
normal id/value setup.

Here's my code:

<% form_tag '/list' %>

    <%= collection_select( :route_id, @routes, :id, :name, {}, {} ) %>

<%= end_form_tag %>

Here's the NoMethodError exception I'm getting:

undefined method `inject' for :id:Symbol

Extracted source (around line #15):

13: <% form_tag '/list' %>
15:     <%= collection_select( :route_id, @routes, :id, :name, {}, {} )
17: <%= end_form_tag %>

What am I doing wrong?

6d57db44c5a4e0721cc7f78cf0ce308a?d=identicon&s=25 Stephen Bartholomew (steveb)
on 2007-02-01 21:42
(Received via mailing list)
<%= collection_select( :route_id, @routes, :id, :name, {}, {} ) %>

You may be using the wrong tag here.  collection_select is for model
fields so expects:
collection_select(object, attribute, collection, value_method,

You probably want something like this:
<%= select_tag('route_id',
options_from_collection_for_select(@routes, :id, :name)) %>

74bd868b70df3590bf94cf7abe563ec4?d=identicon&s=25 Jason (Guest)
on 2007-02-02 01:15
I've tried that and it does work, so I appreciate your help.

How can I add an onchange event to this select box now?


74bd868b70df3590bf94cf7abe563ec4?d=identicon&s=25 Jason (Guest)
on 2007-02-02 01:27
Actually, I figured that out, but how can I insert a default value, like
'Please select an option...' to the select box?
7a666449acc7e18a7002bc88798a44d3?d=identicon&s=25 Pingu Penguin (pingu)
on 2007-05-11 12:36
This topic is locked and can not be replied to.