41

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.

gotqn
  • 42,737
  • 46
  • 157
  • 243
Ynv
  • 1,824
  • 3
  • 20
  • 29

2 Answers2

86

The correct API is:

f.input :date, :as => 'date_picker', :input_html => { :data => {:datepicker => :datepicker} }
rafaelfranca
  • 3,285
  • 24
  • 15
  • 3
    It doesn't work for me somehow… simple_form 2.1 / rails 3.0.20 / ruby 1.9.3p484 / haml 4.0.4 – gamov Jan 16 '14 at 04:54
  • 1
    Is there an update for this with more recent versions of simple_form? – Jordan Jun 18 '14 at 17:28
  • 1
    @sixty4bit I'm wondering what error you get? Did you try with `f.input :date, :as => :string, :input_html => {...}` ? (also don't forget the last ' } ' as it is slightly hidden..) – Lorro Jan 13 '16 at 12:49
  • `= f.input :due_date, as: :string, :input_html => { :data => {:provide => :datepicker} }` this works for me. I am using bootstrap with https://bootstrap-datepicker.readthedocs.io/ – Amit Patel Oct 20 '20 at 15:15
10

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.

EasyCo
  • 2,036
  • 20
  • 39