【问题标题】:Adding span tag in Rails link_to在 Rails link_to 中添加 span 标签
【发布时间】:2011-09-27 03:31:45
【问题描述】:

我已经查看了有关如何添加 <span> 标记的 SO,但我没有看到将 <span> 放置在我想要使用 Rails 3 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"}) %>

但这没有用。

【问题讨论】:

    标签: html ruby-on-rails ruby-on-rails-3 link-to


    【解决方案1】:

    link_to can take a block 所以我认为你在追求这样的事情:

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

    【讨论】:

    【解决方案2】:

    .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' %> 
    

    它们基本上是相同的,但对你来说可能更容易看。另外,我对编码很陌生,所以如果由于某种疯狂的原因这是完全错误的,请发表评论,我会改变它。谢谢。

    【讨论】:

    • 这没有错,但link_to ... do 就是为此而生的,它看起来比 IMO 的解决方案要好得多。
    【解决方案3】:
    link_to '#', :class => 'button white' do
      <span id="span">My span&nbsp;</span>My data
    end
    

    【讨论】:

      【解决方案4】:

      在 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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-26
        • 1970-01-01
        相关资源
        最近更新 更多