12

I am trying to make my rails form_for submit button look like the bootstrap btn btn-lg btn-primary button. I've tried a few different ways and have found no success. The best I could get is getting the form_for submit button in the center of a bootstrap button with different colors. Does anyone know what the command would be to get the bootstrap button to perform the action that my f.submit button does.

 <div class="Action">
     <%= f.submit %>
 </div>

4 Answers 4

21
= f.submit :Submit, class: 'btn btn-success'
2
  • I get the error undefined local variable or method 'f'
    – Whitecat
    Commented Jun 23, 2017 at 16:24
  • 1
    @Whitecat f is the form object, in a rails scaffold it's form e.g. form.submit
    – stevec
    Commented Jul 23, 2021 at 17:17
10

By default <%= f.submit %> will create an <input> element according to the docs.

If you want to make it an actual <button></button> element, you can do it like this:

<%= f.button "My Button", type: "submit", :class=> "btn btn-primary" %>

2

Not clear from the docs: if you still want the form_for to set the button name to 'Update <class>' or 'Create <class>' then pass nil into the submit:

<%= form.submit nil, class: 'btn btn-success' %>
1
= f.submit :Submit, class: 'btn btn-success'

or

= f.submit :Submit, class: :btn, class: :btn-success

Anyone getting errors on:

undefined local variable or method 'f'

Check what your form variable name is. It will appear when you're declaring a form such as:

form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f|
1
  • other likely cause for undefined local variable or method 'f' is that maybe you are using f.submit ouside the scope of the form_for block, where f is available. Commented Jun 8, 2021 at 19:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.