Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rails helpers could ignore HTML attributes which equal false #4725

Closed
nicolasblanco opened this issue Jan 27, 2012 · 2 comments
Closed

Rails helpers could ignore HTML attributes which equal false #4725

nicolasblanco opened this issue Jan 27, 2012 · 2 comments

Comments

@nicolasblanco
Copy link

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: element.active? ? "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: element.active? && "active" } element

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

scottwb pushed a commit to scottwb/rails that referenced this issue Feb 7, 2012
…e:resolved]

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

Signed-off-by: Santiago Pastorino <santiago@wyeworks.com>
@parndt
Copy link
Contributor

parndt commented Mar 12, 2012

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

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

@rafaelfranca
Copy link
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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants