如何在rails中添加options_from_collection_for_select的数据属性

时间:2013-12-28 17:43:05

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4

将此视为我的选择标记

<%= 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

1 个答案:

答案 0 :(得分:63)

请尝试以下操作:

<%= select_tag "post[brand_slug]", options_for_select(@products.map{ |product| [product.name, product.slug, { 'data-id' => product.id }] }), prompt: 'select' %>