[英]How to add data attribute for options_from_collection_for_select in rails
將此視為我的選擇標記
<%= 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
試試以下:
<%= select_tag "post[brand_slug]", options_for_select(@products.map{ |product| [product.name, product.slug, { 'data-id' => product.id }] }), prompt: 'select' %>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.