# Form\_for with select

**URL:** <https://www.ruby-forum.com/t/form-for-with-select/138851>\
**Category:** Rails\
**Created:** [May 28, 2008, 11:26am UTC](https://www.ruby-forum.com/t/form-for-with-select/138851 "2008-05-28T11:26:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![maite](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e495f1/32.png) [@maite](https://www.ruby-forum.com/u/maite)\
**Post date:** [May 28, 2008, 11:26am UTC](https://www.ruby-forum.com/t/form-for-with-select/138851/1 "2008-05-28T11:26:33Z")

</div>

hello everybody

I want to pass user\_id to another controller, =\> category  
this is my code

\<%form\_for :user, @user, :url=\>{ :action =\>“index”, :controller =\>  
“categories”} do |f| %\>  
\<%= select(:category, :user\_id, @users.map{|p| [p.name, p.id] }) %\>  
\<%= submit\_tag “Ir” %\>  
\<% end %\>

when I push submit bottom form\_for generate the object params with 3  
paramaters

{“action” =\> “index”,  
“controller” =\> “categories”,  
but I donÂ´t know which is the third parameter}  
perhaps this?? I donÂ´t know  
“user”=\> {name=\> “bla bla”, id=\>“bla bla”}  
In my category controller in the method index I do this

@valor = params[:category]  
render :text =\> @valor

the browser writte this  
user\_id1; if i select one  
user\_id2; if I select two  
etc…

if I want take de user\_id, how do it?

my select and my form\_for are well???

thank you

---

<div class="post-metadata">

**Author:** ![maite](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e495f1/32.png) [@maite](https://www.ruby-forum.com/u/maite)\
**Post date:** [May 29, 2008, 2:17pm UTC](https://www.ruby-forum.com/t/form-for-with-select/138851/2 "2008-05-29T14:17:54Z")

</div>

any idea???

please :-S

---

<div class="post-metadata">

**Author:** ![maite](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e495f1/32.png) [@maite](https://www.ruby-forum.com/u/maite)\
**Post date:** [May 29, 2008, 6:33pm UTC](https://www.ruby-forum.com/t/form-for-with-select/138851/3 "2008-05-29T18:33:13Z")

</div>

Maite P. wrote:

> \<%form\_for :user, @user, :url=\>{ :action =\>“index”, :controller =\>  
> “categories”} do |f| %\>  
> \<%= select(:category, :user\_id, @users.map{|p| [p.name, p.id] }) %\>  
> \<%= submit\_tag “Ir” %\>  
> \<% end %\>

I believe the form\_for controller would be “category” not “categories”,  
as below, but that might just be a typo on your post…

\<%form\_for :user, @user, :url=\>{ :action =\>“index”, :controller =\>  
“category”} do |f| %\>  
\<%= select(:category, :user\_id, @users.map{|p| [p.name, p.id] }) %\>  
\<%= submit\_tag “Ir” %\>  
\<% end %\>

In the controller, to access the :user\_id selected in the form, you  
would use:

params[:category][‘user\_id’]

This is because the form helpers create form inputs with names of  
“:model[:method]”. If you look at the HTML source code of the page your  
form is on, you’ll see that the selection drop box form field is named  
“category[user\_id]”. Other form fields built with the helpers against  
the category model will be named similarly - “category[fieldname]”.

The result when the form is submitted is that all the input fields are  
put into a hash {‘user\_id’ =\> 3, ‘name’ =\> ‘joe blow’}, which is then  
added to the overall params hash with a key of :category, as if this had  
been done…

params{:action =\> ‘index’, :controller =\> ‘category’, :category =\>  
{:user\_id =\> n}}

What you are seeing here:

> user\_id1; if i select one  
> user\_id2; if I select two

…is Rails default rendering of the hash params[:category]. You are  
seeing the hash {‘user\_id’ =\> 1} or {‘user\_id’ =\> 2} flattened out for  
display. If you were to, instead:

render :text =\> @valor.inspect

You’ll see the contents of the params[:category] hash displayed a little  
more intelligently.

So, bottom line is, when you see this:

user\_id1; if i select one

then params[:category][‘user\_id’] is equal to 1.

---

<div class="post-metadata">

**Author:** ![maite](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e495f1/32.png) [@maite](https://www.ruby-forum.com/u/maite)\
**Post date:** [May 29, 2008, 6:42pm UTC](https://www.ruby-forum.com/t/form-for-with-select/138851/4 "2008-05-29T18:42:38Z")

</div>

Dammit - I just can’t edit myself today…

params = {:action =\> ‘index’, :controller =\> ‘category’, :category =\>  
{‘user\_id’ =\> n}}

---

<div class="post-metadata">

**Author:** ![maite](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e495f1/32.png) [@maite](https://www.ruby-forum.com/u/maite)\
**Post date:** [May 29, 2008, 6:34pm UTC](https://www.ruby-forum.com/t/form-for-with-select/138851/5 "2008-05-29T18:34:33Z")

</div>

Sorry - typo up there… where I wrote:

params{:action =\> ‘index’, :controller =\> ‘category’, :category =\>  
{:user\_id =\> n}}

I meant to write:

params = {:action =\> ‘index’, :controller =\> ‘category’, :category =\>  
{:user\_id =\> n}}
