如何在rails中为options_from_collection_for_select添加数据属性 [英] How to add data attribute for options_from_collection_for_select in rails
本文介绍了如何在rails中为options_from_collection_for_select添加数据属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
将此视为我的选择标记
<%= select_tag "post[brand_slug]", options_from_collection_for_select(@products, 'slug', 'name'), prompt: 'select' %>
这很好用,但我需要一个数据属性
This works fine but I need a data attribute
<%= select_tag "post[brand_slug]", options_from_collection_for_select(@products, 'slug', 'name'), data: {id: @products.id} , prompt: 'select' %>
@product.id
不起作用.我怎样才能获得ID
@product.id
is not working. How can I get the ID
too
推荐答案
尝试以下操作:
<%= select_tag "post[brand_slug]", options_for_select(@products.map{ |product| [product.name, product.slug, { 'data-id' => product.id }] }), prompt: 'select' %>
这篇关于如何在rails中为options_from_collection_for_select添加数据属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文