在Rails的link_to中添加span标签

34

我在SO上搜索了如何添加 <span> 标签,但是我没有看到使用Rails 3 link_to<span>放置在我想要的位置的示例:

<a href="#" class="button white"><span id="span">My span&nbsp;</span>My data</a>

我试过类似这样的东西:
<%= link_to(content_tag{:span => "My span&nbsp;", :id => "span"} @user.profile.my_data, "#", {:class => "button white"}) %>

但是那样做并没有奏效。

4个回答

89

link_to可以使用块,所以我认为你需要类似于这样的东西:

<%= link_to '#', :class => 'button white' do %>
    <span id="span">My span&nbsp;</span><%= @user.profile.my_data %>
<% end %>

mu太短了,如果您有时间,可以看一下我对此的后续。我想将您帮助我生成的链接值添加到数组中。http://stackoverflow.com/questions/7564247/adding-items-to-rails-3-array-onclick - tvalent2

10

使用.html_safe#{@user profile my_data}的组合也应该可以工作。

<%= link_to "<span id='span'>My span&nbsp;</span>#{@user.profile.my_data}".html_safe, "#", class: 'button white' %>

你还可以使用content_tag,那么它看起来会像这样:

<%= link_to(content_tag(:span, "My span&nbsp;", id:"span")+"#{@user.profile.my_data}", "#", class: 'button white' %> 

它们基本相同,但其中一个可能更容易阅读。此外,我对编码还很陌生,所以如果由于某种疯狂的原因出现错误,请在评论中指出,我会进行更改。谢谢。


2
这并没有错,但是 link_to ... do 正是为此而设计的,我认为它比你的解决方案更好看。 - Mischa

5
link_to '#', :class => 'button white' do
  <span id="span">My span&nbsp;</span>My data
end

0
在HAML中:
= link_to  new_post_mobile_path(topic.slug), class: 'add-new-place-btn' do
  %span{:class => "glyphicon glyphicon-plus", :style => "margin-right: 4px;"}
  New Place

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接