Hopefully this is just a quicky....
I have a form to edit a prod开发者_如何学JAVAuct 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
The Codeglot's answer should have been:
<%= f.collection_select :category_id, Category.all, :id , :name %>
(See Rails: undefined method `map' for Ingredient for explanation)
<%= 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
Try this:
<%= f.select :category_id, options_from_collection_for_select(Category.find(:all), :id, :category, params[:category_id].to_i) %>
精彩评论