I am working on a solution to add custom data-attributes to option-tags with the collection_select
form helper in Rails.
I studied some posts at stackoverflow and did a lot of trial and error after consulting some API documentation. I am nearly there but sadly my solution only adds attributes to the select
-tag not to the option
-tags.
This way I am populating the html-options-hash (6th position):
<%= f.collection_select(:parallax_id, @parallax.all, :id, :title, {}, { :"data-icon" => @parallax.map{ |p| "#{p.image}"}} ) %>
This results in a select tag like:
<select data-icon="/uploads/image/image/4/169_strecken-ausgang.jpg" name="game[parallax_id]" id="game_parallax_id">...</select>
But I want the option to get the data-icon
attribute. When I switch positions and add my data-icon to the options-hash (5th position) nothing is output.
Is this what you want?
= f.select :parallax_id, options_for_select(@parallax.map {|p| [p.title, p.id, {'data-icon' => p.image }]})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With