新手问题 collection_select 获取 selected 的值

yeyong14 · 2014年01月14日 · 最后由 Martin91 回复于 2014年01月15日 · 2796 次阅读

有一个新手问题烦。在collection_select中怎么获取selected 关系是这样的,怎么从category获取选中的值

#category
has_many tags
#tag
has_many products
#product
belongs_to tag

现在想在products/edit中看到之前选过的值。

<%= form_for @product |f| %>
#
#
<%= collection_select(:tag, :category_id, Category.all, :id, :name, {selected: Category.all.map{|cate| [cate.id, cate.name]}}) %>
<!-- 这样就全部选上了-->
<%= grouped_collection_select(:product, :tag_id, Category.all, :tags, :id, :id, :name) %>
<!--这个grouped_collection_select有selected. -->
<%= collection_select(:tag, :category_id, Category.all, :id, :name, {selected: Category.all.map{|cate| [cate.id, cate.name]}}) %>

这个是要干什么的啊

需要 登录 后方可回复, 如果你还没有账号请 注册新账号