views:

16

answers:

2

Hi all

Hopefully this is just a quicky....

I have a form to edit a product and each product belongs to a category. In the form_for(@product) I can populate a select box for the categories in a couple of ways:

<%= f.select :category_id, Category.find(:all).collect{|c| [c.category, c.id]}, :prompt => "Pick a Category" %>

or:

<%= f.select :category_id, options_from_collection_for_select(Category.find(:all), :id, :category) %>

The first option remembers the category when editing the product, the second option doesn't. Can anybody enlighten me as to why? Is there a way to use the options_from_collection_for_select in this scenario and have it remember the category upon editing?

Cheers, Adam

A: 

Try this:

<%= f.select :category_id, options_from_collection_for_select(Category.find(:all), :id, :category, params[:category_id].to_i) %>
Lucho
This doesn't remember what was selected when editing either. I have a category_id in my products table so there is a foreign key. And, as mentioned in my question <%= f.select :category_id, Category.find(:all).collect{|c| [c.category, c.id]}, :prompt => "Pick a Category" %> works perfectly. But can't get any sort of options_from_collection_for_select to work!!
Adam
A: 
<%= f.collection_select :category_id, Category, :id , :name %>

make sure you change :name to the field that you want displayed. It's probably :name or :title

Sam
I changed :name to :category but I get a "undefined method `map'" error using this.
Adam