Collection_select

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):

12:
13: <% form_tag ‘/list’ %>
14:
15: <%= collection_select( :route_id, @routes, :id, :name, {}, {} )
%>
16:
17: <%= end_form_tag %>
18:

What am I doing wrong?

Thanks!

<%= 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,
text_method)

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

Steve

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?

Thanks,

Jason

Have a look at this.
http://shiningthrough.co.uk/blog/show/6

Actually, I figured that out, but how can I insert a default value, like
‘Please select an option…’ to the select box?