Dustin James
Dustin James

Reputation: 571

f.select in Rails not applying Bootstrap

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.

Upvotes: 13

Views: 6090

Answers (2)

Kyle Krzeski
Kyle Krzeski

Reputation: 6527

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 :)

Upvotes: 2

Jordan Running
Jordan Running

Reputation: 106027

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.

Upvotes: 30

Related Questions