Empty attribute with Ruby HAML

Using something like %div{:itemscope => true} is the correct way to specify this in your Haml file. How this is rendered depends on how you set Haml’s format option. The default in Haml 3.1 is xhtml, and with that it will render as itemprop=’itemprop’, which is valid xhtml. To render with minimized attributes (like <div … Read more

How to access instance variables in CoffeeScript engine inside a Slim template

What’s happening is that “#{@user_name}” is being interpreted as CoffeeScript, not as Ruby code that’s evaluated and injected into the CoffeeScript source. You’re asking, “How do I inject a Ruby variable into my CoffeeScript source?” The short answer is: Don’t do this. The Rails team made an intentional decision not to support embedded CoffeeScript in … Read more

Ruby methods within Javascript within HAML

Usually, if something is a pain in haml, it means you should refactor the the tricky bit to a helper or partial and call that. // some_helper.rb def new_snazzy_select_tag(options = []) select_tag ‘tag_name_here’, options.map { |option| [option.id, option.name] } end Also, you should use the :javascript filter to render javascript since it will put it … Read more

Best way to handle data attributes in Slim

There are multiple ways in Slim As Hash Attributes which will be hyphenated if a Hash is given (e.g. data={a:1,b:2} will render as data-a=”1″ data-b=”2″) Use it directly as “mu is too short” mentioned, quite intuitive. a data-title=”help” data-content=”foo” Use Ruby code. I often do this and rarely above. = link_to ‘foo’, bar_path, data: {a: … Read more