simple_forms custom data attribute

ⅰ亾dé卋堺 提交于 2019-12-03 10:34:34

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!