Drop-down menu help

23 views
Skip to first unread message

Shalini Sah

unread,
Jun 25, 2012, 8:31:10 AM6/25/12
to rubyonra...@googlegroups.com
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.

Aleksey V Zapparov

unread,
Jun 25, 2012, 9:09:17 AM6/25/12
to rubyonra...@googlegroups.com
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: zapp...@jabber.ru

*Origin: Happy Hacking!
signature.asc

aash dhariya

unread,
Jun 25, 2012, 9:10:21 AM6/25/12
to rubyonra...@googlegroups.com
<%= 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. 



--
You received this message because you are subscribed to the Google Groups "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 rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.



--
Thanks, 
Aash

Shalini Sah

unread,
Jun 25, 2012, 9:24:13 AM6/25/12
to rubyonra...@googlegroups.com
Thank you :) I got it :)
Reply all
Reply to author
Forward
0 new messages