Drop-down menu help

Hi,

I generated through scaffolding products with name and price. In the
products/new’s partial _form.html.erb i made a slight change. Instead of
having the user to enter the product. I provide a drop down menu
replacing :

<%=f.text_field :name %>

with

<%= select_tag(:name, options_for_select([‘Peas’,‘Butter’,‘Garlic’])) %>

But when I click on submit the name attribute is nil instead of taking
the
tag I selected.

Please tell me where am I going wrong.

Thanks & Regards.

<%= f.text_field :name%> sets the params array as:
params[:product][:name] (provided you have written <%= form_for
:product,
… %>)
while <%= select_tag(:name,
options_for_select([‘Peas’,‘Butter’,‘Garlic’]))
%> sets the params array as:
params[:name] after submit.

Probably, you are accessing the wrong array index.

On Mon, Jun 25, 2012 at 6:01 PM, Shalini S.
[email protected]wrote:

<%= select_tag(:name, options_for_select([‘Peas’,‘Butter’,‘Garlic’])) %>
“Ruby on Rails: Talk” group.
To view this discussion on the web visit
https://groups.google.com/d/msg/rubyonrails-talk/-/Yg92uN5u7OIJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


Thanks,
Aash

Thank you :slight_smile: I got it :slight_smile:

On Mon, 25 Jun 2012 05:31:10 -0700 (PDT)
Shalini S. [email protected] wrote:

%>

But when I click on submit the name attribute is nil instead of
taking the tag I selected.

Please tell me where am I going wrong.

First of all, options_for_select should be:

options_for_select([[‘Peas’,1],[‘Butter’,2],[‘Garlic’,3]])

In this case it will give you 1 for Peas, 2 for Butter, etc.
In your case you can use it as:

options_for_select([‘Peas’,‘Butter’,‘Garlic’].map{ |v| [v,v] })

And after all, I guess you better use select helper on a form
builder in your case:

<%= f.select(:name, options_for_select(…)) %>


Sincerely yours,
Aleksey V. Zapparov A.K.A. ixti
FSF Member #7118
Mobile Phone: +34 677 990 688
Homepage: http://www.ixti.net
JID: [email protected]

*Origin: Happy Hacking!