13
votes

I have the following select element in a Rails form:

<%= f.select :customer_type, options_for_select(["Retail","Contractor","Dealer"]), class: "form-control" %>

Does anyone know why the bootstrap class: "form-control" is not rendering?

I'm guessing my syntax is wrong. Any help is appreciated.

2

2 Answers

28
votes

Per the documentation, the method signature for select is:

select(object, method, choices, options = {}, html_options = {})

html_options, which is where your HTML attribute options like :class should go, comes after options, but you're omitting options and putting html_options right after choices. Try this instead:

<%= f.select :customer_type, options_for_select(["Retail","Contractor","Dealer"]), {}, class: "form-control" %>

P.S. If you're wondering why the method signature specifies object, but in actual use we never pass object (method is always first), it's because we're not actually calling this method directly. When we call f.select(...) inside a form_for block, Rails calls select(f.object, ...) for us.

2
votes

Update - if you're seeing this now using you'll need to put the class in an object:

<%= f.select :customer_type, options_for_select(["Retail","Contractor","Dealer"]), {}, { class: "form-control" } %>

Took me some time to figure it out so I figured it was worth a post :)