3

I am using Rails 5 and bootstrap_form_for to build a form.

I know there are some similar questions in SO but none of the helped me solve my issue...

I am trying to add a custom class to a select field. But I have is this:

<%= f.collection_select :location_id, Location.all, :id, :name, :include_blank => ("Insere um endereço ou escolha um local da lista..."), hide_label: true, :class => 'location' %>

But the class is not applied. What can I do diferente?

almo
  • 6,107
  • 6
  • 43
  • 86
  • Try `<%= f.collection_select :location_id, Location.all, :id, :name, {}, {class: "location"} %>` – Kevin Etore Feb 23 '17 at 19:57
  • Possible duplicate of your [other question](https://stackoverflow.com/questions/41617681/add-class-to-bootstrap-form-for-collection-select-in-rails-5). The subject of the two are different, but then you end up talking about `collection_select` and not `select` in both... – RyanQuey Mar 14 '19 at 14:45

2 Answers2

2
<%= f.collection_select(:location_id, Location.all, :id, :name, {:include_blank => ("Insere um endereço ou escolha um local da lista..."), hide_label: true}, class: "class-name") %>
gwalshington
  • 1,418
  • 2
  • 30
  • 60
1

You need to add an empty hash for the options part of the method parameters. It is not very intuitive I'm afraid but if you look at the method signature you can see why it's needed.

collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {}) 


<%= f.collection_select :location_id, 
  Location.all, 
  :id, 
  :name, 
  :include_blank => ("Insere um endereço ou escolha um local da lista..."), 
  hide_label: true, 
  {},
 :class => 'location' %>
Eyeslandic
  • 14,553
  • 13
  • 41
  • 54