I have:
form.input :duration, as: select, collection: {}
I need:
<option value="" data-price="XXX"></option>
Rails does not support HTML5 data attributes for the option tag. Formtastic suggests to create a helper method for this.
Formtastic readme describes how to extend input tags. However, in select_input.rb I can't find any method which would affect the option tag. So, how do I do this?
Also, I found enhanced_select gem which does exactly what I need, but I am not able to make it work with formtastic.
Actually rails does support adding any kind of html tag to options. Usually you would have:
which would give you
If you add a hash into the arrays for each option the hash keys/values will be added as HTML attribute to the option, e.g.
will produce the required tags:
Assuming you have a model called Items, you can actually do this in formtastic like so:
Essentially what you are doing is passing an array of arrays where the final value in each array is a hash. E.g.
Rails 3+ (perhaps 2.x - I didn't confirm) will use the hash in such as array and simply add it to the html of the option tag. Giving you the following:
Produces
Advantage
Using data:{...} is more concise and if using multiple data tags can save code.