Forum: Ruby on Rails getting the value of a form select

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ab4c5cd5d9cc028fcba7a5eec8e1bf30?d=identicon&s=25 Alain (Guest)
on 2006-04-05 01:29
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])%> <br/>
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!
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (Guest)
on 2006-04-05 01:56
(Received via mailing list)
Alain wrote:
<snip>
> -----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.
Ab4c5cd5d9cc028fcba7a5eec8e1bf30?d=identicon&s=25 Alain (Guest)
on 2006-04-05 02:09
> 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!
B5f2971378b0a1536e855e5dab9290dc?d=identicon&s=25 Nate McNamara (Guest)
on 2006-04-05 03:36
(Received via mailing list)
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
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2006-04-05 06:38
(Received via mailing list)
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
Ab4c5cd5d9cc028fcba7a5eec8e1bf30?d=identicon&s=25 Alain (Guest)
on 2006-04-05 13:47
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!
This topic is locked and can not be replied to.