2011-09-27 177 views
28

我看了SO关于如何添加<span>标签,但是,我想用Rails 3 link_to我没有看到,放在<span>一个例子:添加span标签的link_to

<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"}) %> 

但这并没有奏效。

回答

70

link_to can take a block所以我觉得你这样的事情之后是:

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

完美,谢谢! – tvalent2

+0

亩太短,如果你有时间可以看看我的后续行动。我想将您帮助我制作的链接的值添加到数组中。 http://stackoverflow.com/questions/7564247/adding-items-to-rails-3-array-onclick – tvalent2

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

.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 ...做'是为了这个,它看起来好得多您的解决方案IMO。 – Mischa

+0

在我的情况下,带有'content_tag'的第二个解决方案只在我添加了'html_safe' - '“我的范围 ”.html_safe'和'“#{@ user.profile.my_data}”。html_safe'后才起作用 –

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