The name/id of the select tag generated by rails

Hi guys,

I just recently fell in love with Rails. Now, I’m trying to make a
simple page where at list of tasks is presented. Each task has a user.

At the top of the list I want to be able to make a select/dropdown list
containing all users, so that will be able to select all tasks for a
certain user.

Looking at the Rails api I find the helper method: collection_select.
Just what I need!

There is just one problem. I want to select-tag to be named “user_id”.
However, according to the documentation you must specify a “object” and
a “method”.

For example:
<%= collection_select ‘test’, ‘boing’, @users, ‘id’, ‘name’ %>
Outputs:

Rasmus Dennis

This just doesn’t make any sense to. How come I can’t set the name of
the select tag to for example ‘user_id’ so that the path when search
will be ‘/tasks?user_id=1’ instead of ‘/tasks?test%5Bboing%5D=2’.

Am I taking a “non-rails approach” to this problem? How can I set the
name of the select-tag to “user_id”?

I achieved the wanted result like this:
<%= select_tag ‘user_id’, options_for_select(@users.collect { |u|
[u.name, u.id] }) %>

However, I guess you’ll agree it’s not the most beautiful code ever
written. Any suggestions for improvement?

Thanks in advance.

Rasmus N. wrote:

I achieved the wanted result like this:
<%= select_tag ‘user_id’, options_for_select(@users.collect { |u|
[u.name, u.id] }) %>

However, I guess you’ll agree it’s not the most beautiful code ever
written. Any suggestions for improvement?

Thanks in advance.

You might try options_from_collection_for_select:
http://api.rubyonrails.com/classes/ActionView/Helpers/FormOptionsHelper.html