Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add data attribute for options_from_collection_for_select in rails

Consider this as my select tag

<%= 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 is not working. How can I get the ID too

like image 909
kndwsu Avatar asked Dec 28 '13 17:12

kndwsu


1 Answers

Try following:

<%= select_tag "post[brand_slug]", options_for_select(@products.map{ |product| [product.name, product.slug, { 'data-id' => product.id }] }), prompt: 'select' %>
like image 104
Utsav Kesharwani Avatar answered Oct 22 '22 06:10

Utsav Kesharwani