将此视为我的选择标记
<%= select_tag "post[brand_slug]", options_from_collection_for_select(@products, 'slug', 'name'), prompt: 'select' %>
这很好但我需要一个数据属性
<%= select_tag "post[brand_slug]", options_from_collection_for_select(@products, 'slug', 'name'), data: {id: @products.id} , prompt: 'select' %>
@product.id
无效。我怎样才能得到ID
答案 0 :(得分:63)
请尝试以下操作:
<%= select_tag "post[brand_slug]", options_for_select(@products.map{ |product| [product.name, product.slug, { 'data-id' => product.id }] }), prompt: 'select' %>