captncraig
captncraig

Reputation: 23078

Select Box not filling properly in rails

I am creating a select box for a form using this in _form.html.erb

<%= f.select(:category_id,options_for_select(@cats)) %>

@cats is an array created in my controller like this:

@cats = []
categories.each do |c|
  @cats.push([c.full_name,c.id])
end

The select box is properly filled, and the selected foreign key is even properly saved to the database. The problem is, when I come back in my edit action, the select box is moved back to the first item in the list, not the one corresponding to category_id. Reading the documentation it seems like this should just magically work. How do I get it to select the proper value?

Upvotes: 1

Views: 346

Answers (1)

Corey
Corey

Reputation: 2243

When you use the select helper you just pass the choices not the full option tags like you would with the select_tag helper. Try this instead

<%= f.select(:category_id, @cats) %>

Upvotes: 3

Related Questions