Rails: Passing array values to tag helpers like link_to

Posted About 5 years ago. Visible to the public. Repeats.

From at least Rails 4, the ActionView tag helper turns Array values of HTML options into a single space-separated string Show archive.org snapshot .
This means you can pass an array to :class options:

extra_classes = %w[one two]

= link_to 'Dashboard', root_path, class: ['btn', 'btn-primary', *extra_classes]
=> <a href="/" class="btn btn-primary one two">Dashboad</a>

= content_tag 'div', 'Hello World', class: %w[alert alert-info]
=> <div class="alert alert-info">Hello World</div>

= content_tag 'span', 'Hello World', class: []
=> <span class>Hello World</span>
Dominik Schöler
Last edit
Over 1 year ago
Emanuel
License
Source code in this card is licensed under the MIT License.
Posted by Dominik Schöler to makandra dev (2019-03-14 12:27)