Adding bootstrap button class to button_to in rail

2019-02-25 11:51发布

问题:

I have a button_to call in my Rails 4 haml file that looks like this

= button_to("Click Me", action: "click", class: "btn btn-primary")

However, the class: "btn btn-primary" code snippet isn't working properly, and my button isn't changing. I've also tried the old Ruby syntax of :class => "btn btn-primary", but that doesn't seem to do the trick.

Any help would be greatly appreciated.

回答1:

You need to make sure the second and third parameters to button_to are separate. Currently the action and class keys are being passed as a single hash, but they should be two arguments, one for options and one for html_options.

Try (note the extra {...} around action: "new" making it a separate hash):

= button_to("Click Me", { action: "new" }, class: "btn btn-primary")