Class list helper for HTML tags

Hi Core List

The usual way to conditionally add classes to html tags is in the following form:

This is hard to read and nod very convenient. ReactJS has an Addon to use a Hash (Javascript Object) to conditionally add classes (GitHub - JedWatson/classnames: A simple javascript utility for conditionally joining classNames together).

Inspired by this I’d like to add a helper to ActionView which does exactly that. So you can write:

or

<%= content_tag_for :tr, @model, class: class_list(‘foo’, ready: @model.ready?) do %>

How do you like this idea? Where should that go? New gem? The newly extracted content tag gem? Action View Helpers?

Regards

phil

Hi Philippe,

you should be able to use Rails’ ability to pass in an array of classes, and Ruby’s parentheses to create a scope, to handle this scenario for you:

<%= content_tag_for :tr, @model, class: [‘foo’, (‘ready’ if @model.ready?)] do %>

That should give you the same html response you expect, without the need to extend the framework :slight_smile:

Hope that helps.