# The name/id of the select tag generated by rails

**URL:** <https://www.ruby-forum.com/t/the-name-id-of-the-select-tag-generated-by-rails/136102>\
**Category:** Rails\
**Created:** [April 28, 2008, 10:54pm UTC](https://www.ruby-forum.com/t/the-name-id-of-the-select-tag-generated-by-rails/136102 "2008-04-28T22:54:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rasmus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7993a0/32.png) [@rasmus](https://www.ruby-forum.com/u/rasmus)\
**Post date:** [April 28, 2008, 10:54pm UTC](https://www.ruby-forum.com/t/the-name-id-of-the-select-tag-generated-by-rails/136102/1 "2008-04-28T22:54:04Z")

</div>

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”?

---

<div class="post-metadata">

**Author:** ![rasmus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7993a0/32.png) [@rasmus](https://www.ruby-forum.com/u/rasmus)\
**Post date:** [April 28, 2008, 11:21pm UTC](https://www.ruby-forum.com/t/the-name-id-of-the-select-tag-generated-by-rails/136102/2 "2008-04-28T23:21:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rasmus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7993a0/32.png) [@rasmus](https://www.ruby-forum.com/u/rasmus)\
**Post date:** [April 28, 2008, 11:23pm UTC](https://www.ruby-forum.com/t/the-name-id-of-the-select-tag-generated-by-rails/136102/3 "2008-04-28T23:23:16Z")

</div>

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](http://api.rubyonrails.com/classes/ActionView/Helpers/FormOptionsHelper.html)
