Search code examples
ruby-on-railsactionviewcontent-tag

Nested html with content_tag


I want to get this html using content_tag or content_tag_for:

<a href="javascript:;" class="nav-link ">
  <%= fa_icon 'book', 'aria-hidden': true %>
  <span class="title">Courses</span>
</a>

I could put fa_icon inside link_to, but don't know how to put span. And if it is not doable, what if i do this way:

<a href="<%=courses_path%>" class="nav-link ">
  <%= fa_icon 'book', 'aria-hidden': true %>
  <span class="title">Courses</span>
</a>

Solution

  • I would try link_to ... do. Because it's a standard approach that's more descriptive than content_tag I think it will be easier to understand the original intent of the code when you come back 2 years from now to do maintenance.

    Here's what your snippet might look like, to get you going:

    <%= link_to courses_path, class: 'nav-link' do %>
      <%= fa_icon 'book', 'aria-hidden': true %>
      <span class="title">Courses</span>
    <% end %>
    

    Here is the documentation for Rails 4.2: http://apidock.com/rails/v4.2.1/ActionView/Helpers/UrlHelper/link_to