<%= fa_icon 'book', 'aria-hidden': true %> <%= fa_icon 'book', 'aria-hidden': true %> <%= fa_icon 'book', 'aria-hidden': true %>

Nested html with content_tag

542 Views Asked by At

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>
2

There are 2 best solutions below

0
Alan Lattimore On BEST ANSWER

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

0
Anthony E On

Why not the following?:

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