Skip to content

Instantly share code, notes, and snippets.

@blairanderson
Last active December 4, 2018 15:07
Show Gist options
  • Save blairanderson/862a3a5065425d9bf39b to your computer and use it in GitHub Desktop.
Save blairanderson/862a3a5065425d9bf39b to your computer and use it in GitHub Desktop.
Rails Bootstrap Simple_Form Select with Data Attributes

This was super annoying so i decided to write it down.

I needed to add some data attributes to a simple_form input(f.input :people, as: :select) and also maintain the bootstrap classes

Here's how i solved it:

<%= f.input :car_id, label: 'Select a Car' do %>
    <%= f.select :car_id, @cars.map { |car| [car.name, car.id, {'data-car-price' => car.current_price}] }, {include_blank: true}, {class: 'form-control'} %>
<% end %>

The magic happens with the regular rails f.select :blah, :yada, nil... but you keep your simple_form styles when you wrap it with a simple_form input block.

i could not get the rails select block to work with the content_tag as written here: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-select

@marlosirapuan
Copy link

thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment