Skip to content

Rails helpers could ignore HTML attributes which equal false #4725

nicolasblanco opened this Issue Jan 27, 2012 · 2 comments

3 participants


Currently Rails helpers only ignore HTML attributes which are nil :

<%= content_tag(:span, "element", class: nil) %>
<!--  # => <span>element</span> -->

Often you want to apply a particular class or id only if some condition is true, for example in navigation links. In this case you'll have to use the ternary operator like :

<%= content_tag(:span, "element", class: ? "active" : nil) %>

In HAML there is a feature where an attribute is also ignored when it equals false, so the line bellow can simply be written :

%span{ class: && "active" } element

Currently, Rails helpers output the attribute value "false". What do you think of copying the HAML behaviour for all Rails helpers ?

@scottwb scottwb pushed a commit to scottwb/rails that referenced this issue Feb 7, 2012
Caleb Land allow spaces and other characters in attribute names [#4725 state:res…

* define the dynamically defined methods with
  'define_method' instead of def
* wrap some string injected method names in quotes

Signed-off-by: Santiago Pastorino <>
parndt commented Mar 12, 2012

You could write your example like this and it would work:

<%= content_tag(:span, "element", class: ("active" if %>
Ruby on Rails member

I think that we can go with the @parndt suggestion. Since we don't have how to know if the predicate methods will return only true and false this will never work.

Another thing, sometimes you want to do this:

<%= content_tag(:span, "element", 'data-remote': false) %>

and changing how the helpers works will break this behavior.

I'm closing this issue. Thanks to reporting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.