Wemmick
Wemmick

Reputation: 196

Using simple_form in Rails 4, trying to pass data attributes to an input_field

I'm using simple_form in Rails 4, and I was using an input that I then passed data attributes to like so:

<%= f.input :url, :input_html => {"data-toggle" => "tooltip", :title => "My tooltip"} %>

and it worked as expected, creating tags like:

<input data-toggle="tooltip" data-original-title="My tooltip">

Now, however, I need to use simple_form's input_field so I can have more control over the display, but it doesn't seem to accept the input_html argument. My code looks like:

<%= f.input_field :url, :input_html => {"data-toggle" => "tooltip", :title => "My tooltip"} %>

and it results in:

<input html="{:data=>{&quot;data-toggle&quot;=>&quot;tooltip&quot;, :title=>&quot;My tooltip&quot;}}">

Which is clearly suboptimal (I stripped other irrelevant properties and attributes to simplify). Any ideas on how to make this work?

Upvotes: 1

Views: 331

Answers (1)

gmcnaughton
gmcnaughton

Reputation: 2293

Looking at the code for SimpleForm::FormBuilder#input_field, it appears that all options passed in are treated as if they were under :input_html.

Try removing :input_html and just passing the options directly:

<%= f.input_field :url, "data-toggle" => "tooltip", :title => "My tooltip" %>

Upvotes: 2

Related Questions