Allow hash for conditional class attribute in tag helper

102 views
Skip to first unread message

Stefan Schüßler

unread,
Mar 5, 2014, 10:36:32 AM3/5/14
to rubyonra...@googlegroups.com
When dealing with conditional class attribute values in HTML tags, I often find myself writing code like:

  link_to(post.title, post, class: "post #{post.active? ? "active" : ""}")

The code would be much cleaner if we allowed a hash syntax:

  link_to(post.title, post, class: {"post" => true, "active" => post.active?})

The implementation is straightforward:


What do you think?

-Stefan

Andrew Kaspick

unread,
Mar 5, 2014, 11:04:37 AM3/5/14
to Ruby on Rails: Core
-1

I don't see why 'class' would get the special treatment when all other attributes could be candidates too and that style is very inconsistent with pretty much all of rails and not particularly clear either.

I personally just use a helper if logic is required for attributes.


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-co...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
Visit this group at http://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/groups/opt_out.

Xavier Noria

unread,
Mar 5, 2014, 11:40:17 AM3/5/14
to rubyonrails-core
Agreed.

Sergio Campamá

unread,
Mar 5, 2014, 12:09:37 PM3/5/14
to rubyonra...@googlegroups.com
could you provide an example of one of those helpers?

--------------------------------------
Sergio Campamá
sergio...@gmail.com

Nicolás Sanguinetti

unread,
Mar 5, 2014, 12:16:01 PM3/5/14
to rubyonra...@googlegroups.com
You can pass Arrays to the `class:` option and it will do what you expect it to:

    link_to(post.title, post, class: ["post", "active" if post.active?])

Cheers,
-foca


--

Stefan Schüßler

unread,
Mar 5, 2014, 12:23:01 PM3/5/14
to rubyonra...@googlegroups.com, h...@nicolassanguinetti.info
That array syntax is invalid.

Stefan Schüßler

unread,
Mar 5, 2014, 12:26:28 PM3/5/14
to rubyonra...@googlegroups.com
I think the class attribute is quite unique because it's a space-separated list. Are there any other attributes like that?

Amiel Martin

unread,
Mar 5, 2014, 12:40:20 PM3/5/14
to rubyonra...@googlegroups.com
Here's a very simple example.


def link_to_post(post)
html_class = ["post"]
html_class << "active" if post.active?
link_to(post.title, post, class: html_class)
end

Nicolás Sanguinetti

unread,
Mar 5, 2014, 12:47:45 PM3/5/14
to Stefan Schüßler, rubyonra...@googlegroups.com
Sorry, it should be ["post", post.active? && "active"]

Carlos Antonio da Silva

unread,
Mar 5, 2014, 12:59:50 PM3/5/14
to rubyonra...@googlegroups.com, Stefan Schüßler
Or wrap with ():

  link_to(post.title, post, class: ["post", ("active" if post.active?)])
--
At.
Carlos Antonio

Rodrigo Rosenfeld Rosas

unread,
Mar 5, 2014, 5:24:14 PM3/5/14
to rubyonra...@googlegroups.com, Stefan Schüßler

They are not equivalent. One will return false and the other nil.

Draiken

unread,
Mar 6, 2014, 7:17:31 AM3/6/14
to rubyonra...@googlegroups.com
If you really need it just override the link_to and add this syntax you want to use

def link_to
   class_attr = deal_with_classes_the_way_i_want
   super(stuff, class: class_attr)
end

em...@leaplines.com

unread,
Oct 24, 2016, 8:12:14 PM10/24/16
to Ruby on Rails: Core
Well this is old, but still relevant :) I've seen the replies, but i've we do it the way we wanted and just because all the other attributes are doing it that way, i think you're doing it wrong. Rails is meant to please the developer, making painful constructs just to make a link selected or current is just crazy.

I'd would love it if rails would have a class: {selected: selected?, btn: true}

Rails, is elegant, makes sense and just works. Some hashes get special treatment, thats fine.

:class should be one of them. 

Carlos Ramirez III

unread,
Jan 12, 2017, 1:40:38 PM1/12/17
to Ruby on Rails: Core
To reiterate a post I made on a related topic...

Rather than alter all the individual helper methods which take a :class option (e.g. link_to, content_tag), why not just use a helper method to generate the finalized list of classes?

For example,

link_to "Hello", root_url, class: class_set(selected: selected?, btn: true)

There's an existing gem which was inspired by React's classSet and exposes this exact functionality:
Reply all
Reply to author
Forward
0 new messages