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.