问题
I would like to have an additional data attribute on an input tag generated by simple_form. The following does not work:
<%= f.input :date, :as => 'date_picker', :data => {:datepicker => :datepicker} %>
How could this be done? Is it possible at all? As you might have guessed: I am trying to add bootstrap-datepicker to my site without using explicit js to initialize the date picker.
回答1:
The correct API is:
f.input :date, :as => 'date_picker', :input_html => { :data => {:datepicker => :datepicker} }
回答2:
For prosperity. On Rails 4.2.5 and Simple Form 3.2.1
The above from @rafaelfranca works for inputs:
f.input :first_name, input_html: { data: { cool_stuff: "yes" } }
If you want to add a data attribute to the simple_form_for helper:
simple_form_for @form, html: { data: { super_cool_key: "secret" } } do |f|
Note the difference: input_html:
vs html:
Also, the underscores will automatically get changed to a dash.
来源:https://stackoverflow.com/questions/8326355/simple-forms-custom-data-attribute