Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 433
Dustin James Avatar asked Aug 13 '14 21:08

Dustin James


2 Answers

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

like image 99
Kyle Krzeski Avatar answered Sep 19 '22 23:09

Kyle Krzeski


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.

like image 23
Jordan Running Avatar answered Sep 16 '22 23:09

Jordan Running