Read more

Rails: Passing array values to tag helpers like link_to

Dominik Schöler
March 14, 2019Software engineer at makandra GmbH

From at least Rails 4, the ActionView tag helper turns Array values of HTML options into a single space-separated string Show 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>
Illustration book lover

Growing Rails Applications in Practice

Check out our e-book. Learn to structure large Ruby on Rails codebases with the tools you already know and love.

  • Introduce design conventions for controllers and user-facing models
  • Create a system for growth
  • Build applications to last
Read more
Posted by Dominik Schöler to makandra dev (2019-03-14 13:27)