views:

48

answers:

2

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?

A: 

What does your edit action look like in comparison to your create action?

dhoss
both create the @cats array identically. The only difference is that new creates a new entity and edit finds it by id.
CaptnCraig
+2  A: 

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) %>
Corey
Thank you! Rails can be really frustrating with so many similarly named functions doing distinctly different things.
CaptnCraig
Totally understand, I look up the apis on the form helpers at least once week.
Corey