form_for with select

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

any idea???

please :-S

Maite Piedra 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.

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}}

Dammit - I just can't edit myself today...

params = {:action => 'index', :controller => 'category', :category =>
{'user_id' => n}}