[英]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? 但是rails现在会显示图标后跟/,有什么办法可以避免这种情况?
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 %>
原因: /
由于此代码<%= root_path %>
and writing just <%= link_to '', :class => 'nav-icon' do %>
will create link with empty href 并只写<%= link_to '', :class => 'nav-icon' do %>
将创建具有空href的链接
I think you should give your path in the first argument of link_to
as follows 我认为您应该在link_to
的第一个参数中给出路径,如下所示
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 %>
. 的/
由你的生成图标之后显示<%= root_path %>
。
<%= %>
seems: <%= %>
似乎:
please ruby, write this on my html. 请红宝石,将此写在我的html上。
so ruby wrote your root_path
variable, and your root_path
is equal to /
. 因此ruby编写了您的root_path
变量,并且您的root_path
等于/
。
remove your <%= root_path %>
after <span class="icon-home">Home</span>
and everything will be ok. 在<span class="icon-home">Home</span>
之后删除<%= root_path %>
<span class="icon-home">Home</span>
,一切都会好起来的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.