使用collection_select来实现

#实现has_many中的下拉框功能
tag has_many words
word belongs_to tag

#view实现
<%= f.collection_select :tag_id, current_user.tags, :id, :name%>

#view中添加css演示,直接显示current_user.tags中的name的值
<%= f.collection_select :tag_id, current_user.tags, :id, :name, {}, { class: "form-control" } %>

#view中添加没有选择的情况
<%= f.collection_select :tag_id, current_user.tags, :id, :name, {include_blank: 'None'}, {class: "form-control" } %>

#没有f在前面的情况下,collection_select的第一个参数是资源本身
<%= f.collection_select :word, :tag_id, current_user.tags, :id, :name, {include_blank: 'None'} %>

results matching ""

    No results matching ""