简体   繁体   English

在Rails link_to中添加跨度标签

[英]Adding span tag in Rails link_to

I've looked on SO about how to add a <span> tag but I didn't see an example that placed the <span> where I want using Rails 3 link_to : 我已经看过有关如何添加<span>标签的方法,但是我没有看到使用Rails 3 link_to<span>放置在我想要的位置的示例:

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

I tried something like: 我尝试了类似的东西:

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

But that didn't work. 但这没有用。

link_to can take a block so I think you're after something like this: link_to可能会占用一个块,因此我认为您正在执行以下操作:

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

A combination of the .html_safe with #{@user.profile.my_data} should work as well. .html_safe#{@user.profile.my_data}组合也应该起作用。

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

You can also use a content_tag so it would look like: 您还可以使用content_tag使其看起来像:

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

They're basically identical but one might be easier on the eyes for you. 它们基本上是相同的,但是对您来说可能会更容易一些。 Also, I'm pretty new to coding so if this is dead wrong for some crazy reason, please just comment and I'll change it. 另外,我对编码还很陌生,因此,如果由于某种疯狂的原因而报错,请发表评论,然后我将其进行更改。 Thanks. 谢谢。

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

In HAML : 在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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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