I was having this issue the other day trying to get a unique javaScript class attribute to stick in HAML. HAML versus writing HTML is nice and easy in my opinion and it seems like it is saving a ton of time messing around with closing tags. This was my experience —

In HAML you can simply append different id and class definitions together like this –

1
2
 
%div.car_chase#dredlock

but I needed a class to stick which looked like this:

1
2
 
class="{required:true, email:true, messages:{email:'Please enter a valid email address'}}"

I was getting errors by simply concatenating the elements, but then I recalled additional attributes it might look something like this:

1
2
 
%input#dredlock{ :alt => "dirty hair", :name => "textarea", :type => "text"}

By adding the class to the id attribute list I got it to stick.

1
2
3
4
5
6
%div
  %label{ :for => "hairwashing" }
    Email address
    %abbr{ :title => "Required" }
      *
  %input#dreadlockemail{ :alt => "email", :name => "email", :type => "text", :class => "{required:true, email:true, messages:{email:'Please enter a valid email address'}}"}

Leave a Reply

Your email address will not be published. Required fields are marked *