Ruby on Rails 3 Select Helper Question
Have have t开发者_开发问答his line of code in my form when I create a new item. Though when I edit the item, the default selection isn't the one that is selected. Do I need to set the initial value?
<%= f.select :category, options_for_select(Item::CATEGORIES) %>
options_for_select
accepts second param which identifies the selected value.
try
<%= f.collection_select :category_id, Item::CATEGORIES, :downcase, :titleize %>
It assumes your Item::CATEGORIES
gives a array of strings of categories.
for each category in Item::CATEGORIES
, category.downcase
will be used as the option's value, while category.titleize
will be used as the option's text.
ie.
<option value="<%= cate.downcase %>"><%= cate.titleize %></option>
======
or you could:
<%= f.select :category, options_for_select(Item::CATEGORIES, @cur_obj.category.id) %>
精彩评论