简体   繁体   中英

Ruby on Rails - Link_to with icon instead of a slash

i want to show an small icon instead of a textlink. using this piece of code:

      <li>
      <%= link_to '', :class => 'nav-icon' do %>
          <span class="icon-home">Home</span><%= root_path %>
      <% end %>
      </li>

but rails now shows me the icon followed by a / is there any way to avoid that behavior?

Following should work

  <li>
  <%= link_to '', :class => 'nav-icon' do %>
      <span class="icon-home">Home</span>
  <% end %>
  </li>

Reason: the / is coming due to this code <%= root_path %>

and writing just <%= link_to '', :class => 'nav-icon' do %> will create link with empty href

I think you should give your path in the first argument of link_to as follows

link_to(url, html_options = {}) do
  # your icon code
end
<li>
  <%= link_to root_path, :class => 'nav-icon' do %>
    <span class="icon-home">Home</span>
  <% end %>
</li>

The / displayed after your icon is generated by <%= root_path %> .

<%= %> seems:

please ruby, write this on my html.

so ruby wrote your root_path variable, and your root_path is equal to / .

remove your <%= root_path %> after <span class="icon-home">Home</span> and everything will be ok.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM