开发者

Difference between two f.select option

开发者 https://www.devze.com 2023-01-22 12:11 出处:网络
Hopefully this is just a quicky.... I have a form to edit a prod开发者_如何学JAVAuct and each product belongs to a category.

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号