Question: Icon within a link_to tag with <a> tags


Icon within a link_to tag with <a> tags

Answers 2
Added at 2017-01-01 13:01

I have a navbar button:

<%= link_to new_post_path do %>
  <a class="btn-navbar"><i class="fa fa-plus" aria-hidden="true"></i></a>
<% end %>

It does not allow me to click it and navigate to the correct page.

If I remove the <a> tags, it works but I need the <a> because its have the correct class associated with it to style the button.

The above code is styled correctly but when trying to click it it does not link to the right page.

nr: #1 dodano: 2017-01-01 13:01

Replace the a tag with a span and then give the span the same class you are giving to the a tag , this should give you the same results

<span class="btn-navbar">your code goes here</span>
nr: #2 dodano: 2017-01-01 14:01

Add the class to the link to helper to have it applied to the a tag.

<%= link_to new_post_path, class: 'btn-navbar' do %>
  <i class="fa fa-plus" aria-hidden="true"></i>
<% end %>

If you're using the official font-awesome rails gem:

<%= link_to icon('plus'), new_post_path, class: 'btn-navbar' %>
Source Show
◀ Wstecz