# Getting the value of a form select

**URL:** <https://www.ruby-forum.com/t/getting-the-value-of-a-form-select/56858>\
**Category:** Rails\
**Created:** [April 5, 2006, 1:29am UTC](https://www.ruby-forum.com/t/getting-the-value-of-a-form-select/56858 "2006-04-05T01:29:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![alain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a183cd/32.png) [@alain](https://www.ruby-forum.com/u/alain)\
**Post date:** [April 5, 2006, 1:29am UTC](https://www.ruby-forum.com/t/getting-the-value-of-a-form-select/56858/1 "2006-04-05T01:29:05Z")

</div>

I have been working for a whole week on this problem without success, so  
I feel that its now time to give up and ask for some help since it seems  
that I am obvisouly missing something…

all I want to do is having a form with a dropdown menu. When the user  
select a value from the drop down and sumit the value, this value must  
be the new ‘active\_client’.

Technicaly, when the form is submited from the list page, it calls the  
change\_active\_client action that take care of changing the client’s ID  
in the session then returns the control to the list action.

My problem is that when I try to output the value stored in the session,  
I get anything but what I expected…

What am I missing?

the simplified version of my code goes like this:

------START\_CONTROLER-------  
class SpecificController \< ApplicationController

def list  
@active\_client = session[:active\_client]  
@clients = Client.find\_all.collect {|c| [c.name, c.id] }

end

def change\_active\_client  
session[:active\_client] = params[:client]  
redirect\_to(:action =\> “list”)  
end

end  
------END\_CONTROLER-------

------START\_VIEW-----------

\<%= form\_tag (:action =\> “change\_active\_client”)%\>

```
<%= select(:client, :id, @clients) %>
<input type="submit" value="Change" />

```

\<%= end\_form\_tag%\>

content of the session: \<%=h(session[:active\_client])%\>   
  
content of the @Active\_client: \<%=h(@active\_client)%\>

------END\_VIEW---------------

------ABOUT\_THE\_MODEL------  
its a basic table, with ID as a auto increment int and name as a  
varchar

-----THE OUTPUT OF ABOVE CODE—  
something like: id1 or id2, id3, etc… I want to be able to extract the  
name of the client but cant…

This thing is driving me nuts, especially since I am sure this is  
something obvious that I am missing!

Thx!

---

<div class="post-metadata">

**Author:** ![alain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a183cd/32.png) [@alain](https://www.ruby-forum.com/u/alain)\
**Post date:** [April 5, 2006, 1:56am UTC](https://www.ruby-forum.com/t/getting-the-value-of-a-form-select/56858/2 "2006-04-05T01:56:09Z")

</div>

Alain wrote:

> -----THE OUTPUT OF ABOVE CODE—  
> something like: id1 or id2, id3, etc… I want to be able to extract the  
> name of the client but cant…  
> You’ll need to do a Client.find() somewhere to turn the id you get back  
> from the select box into a Client object.

---

<div class="post-metadata">

**Author:** ![alain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a183cd/32.png) [@alain](https://www.ruby-forum.com/u/alain)\
**Post date:** [April 5, 2006, 2:09am UTC](https://www.ruby-forum.com/t/getting-the-value-of-a-form-select/56858/3 "2006-04-05T02:09:56Z")

</div>

> You’ll need to do a Client.find() somewhere to turn the id you get back  
> from the select box into a Client object.

Thx Alex.

I changed the action change\_active\_client for this:

def change\_active\_client  
session[:active\_client] = Client.find(params[:client])  
redirect\_to(:action =\> “list”)  
end

when I look at it, it seems right: when the form is submited, we get the  
ID from the client, then look for the row with that ID and assign it to  
:active\_client in the session. Unfortunaly, it doesnt work, I get this  
message:

Unknown key(s): id

It seems like the Client.find() method cant extract the ID from the  
session… could the problem be with how I created the select box?

Thx!

---

<div class="post-metadata">

**Author:** ![alain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a183cd/32.png) [@alain](https://www.ruby-forum.com/u/alain)\
**Post date:** [April 5, 2006, 3:36am UTC](https://www.ruby-forum.com/t/getting-the-value-of-a-form-select/56858/4 "2006-04-05T03:36:40Z")

</div>

I want to use Capistrano w/git, and while I was reading the source for  
the other scm adapters it occurred to me maybe somebody has done this  
already?

Thanks!  
Nate

---

<div class="post-metadata">

**Author:** ![alain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a183cd/32.png) [@alain](https://www.ruby-forum.com/u/alain)\
**Post date:** [April 5, 2006, 6:38am UTC](https://www.ruby-forum.com/t/getting-the-value-of-a-form-select/56858/5 "2006-04-05T06:38:48Z")

</div>

Hi Alain,

I still real new at this, but I believe you’ll see in the Parameters  
section  
of the error message you’re getting that your form is returning a hash  
of  
hashes rather than a simple hash. I think if you change your current  
Client.find(…) to Client.find(params[:client][:id]) you’ll get what you  
need. Actually, I think it will probably work with  
session[:active\_client]  
= params[:client][:id], but I’m not experienced with the use of the  
.collect. I’d be real interested, so I hope you’ll let us know.

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![alain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a183cd/32.png) [@alain](https://www.ruby-forum.com/u/alain)\
**Post date:** [April 5, 2006, 1:47pm UTC](https://www.ruby-forum.com/t/getting-the-value-of-a-form-select/56858/6 "2006-04-05T13:47:32Z")

</div>

Hi Bill,

thank you very much! You solved the problem! I replaced the  
Client.find() for Client.find(params[:client][:id]) and it fixed the  
problem!

Your other option: session[:active\_client] = params[:client][:id] did  
not work.

I think I will reread the documentation of .collect to make sure I  
understand what I did wrong…

The only thing is that I cant figure out how to have the updated page  
have the correct value from the select field selected. Right now, it  
resets itself to the first value everytime I return to the page.

Thanks!
