1

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...

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.

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

Community
  • 1
  • 1
  • Did you get any error when using this line `form_for @patient, :html => {:class => "form-horizontal"} do |f|` which you have commented? – Pavan Apr 20 '14 at 16:21
  • No error. The page still loads, but when I look at the source code, the class is not added. –  Apr 20 '14 at 16:37
  • This is very strange as I just tried again and it works. The class has been replaced. I did not do anything different than before I wrote the question. –  Apr 20 '14 at 16:41
  • Hmm,might you restarted the server? – Pavan Apr 20 '14 at 16:42
  • This also works i think so `form_for @patient, {:html => {:class => "form-horizontal"}} do |f|` – Pavan Apr 20 '14 at 16:45
  • I had restarted the server, but did not see the change. I shut everything down and when I started it back up is when I saw the change. –  Apr 20 '14 at 17:06
  • Ok,anyways.I added my answer to give you some more useful info.Please appreciate it if you satisfied. – Pavan Apr 20 '14 at 17:08

2 Answers2

4

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!

Pavan
  • 33,316
  • 7
  • 50
  • 76
  • Thanks Pavan! This really help as I was able to go in and drew up the form nice. –  Apr 20 '14 at 17:52
0

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.

zarathustra
  • 1,898
  • 3
  • 18
  • 38