How to add a class to the form_for helper in a Rai

2019-04-16 04:31发布

问题:

I'm using bootstrap CCS to style my Rails 4 app, but cannot figure out how to add a class to the form when using the form_for helper. I have followed the suggestions on several other threads without success. Two such threads...

  • How do you override the class name in the form_for helper?
  • adding (not overwriting a class) for a form generated by the form_for helper

The app works as it should per the instructions, but I want it to look good too. Here is the working code and have commented out the first line as it does not add the additional class to the form as I need it to.

  • https://github.com/Brian-Boyd/TheIronYard/blob/master/03week-day4/patient/app/views/patients/_form.html.haml

Can anyone help with this challenge? Hopefully there is some smart Rails developers available to help out today.

回答1:

You might have got your answer,though these are some points might help you

form_for with class name in haml:

= form_for @patient, :html => {:class => "form-horizontal"} do |f|

it can also be written as

= form_for @patient, {:html => {:class => "form-horizontal"}} do |f|

but it couldn't be written as

- form_for @patient, :html => {:class => "form-horizontal"} do |f| #it gives error.

Hope it helps!



回答2:

In addition to Pavan's answer in Rails 4 you can simply write it this way if you are using a form_tag:

= form_tag some_path, method: 'get', class: 'some-class' do

This worked for me.