Getting the value of a form select


#1

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!


#2

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.


#3

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!


#4

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


#5

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


#6

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!