Drop-down menu help


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


<%= 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.

First of all, options_for_select should be:


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(...)) %>

<%= 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.

Thank you :slight_smile: I got it :slight_smile: