我可以使用link_to来生成一个包含span的链接吗?

时间:2022-11-23 11:41:26

I am basically trying to get this result:

我基本上是在尝试得到这个结果:

        <a href="#" class="button small-button green-button">
            Log in
            <span class="button-right"></span>
        </a>

But I don't know how to do this with a link_to in rails 3 ?

但是我不知道如何在rails 3中使用link_to实现这一点?

5 个解决方案

#1


43  

You can use the block form of link_to for that:

您可以使用link_to的块形式:

<%= link_to "#", :class => "button small-button green-button" do %>
  Log in
  <span class="button-right"></span>
<% end %>

#2


4  

The simplest way to do it is by using html_safe or raw functions

最简单的方法是使用html_safe或raw函数。

<%= link_to 'Log In<span class="button-right"></span>'.html_safe %>

or using raw function (recommended)

或者使用原始函数(推荐)

<%= link_to raw('Log In<span class="button-right"></span>') %>

Simple as it can get !!

就这么简单!

Don’t use html_safe method unless you’re sure your string isn’t nil. Instead use the raw() method, which wont raise an exception on nil.

不要使用html_safe方法,除非您确定您的字符串不是nil。而是使用raw()方法,它不会在nil上引发异常。

#3


3  

Your snippet looks like a static link, that will never change when interpreted by Rails; I suppose its save to insert the raw HTML in your view.

您的代码片段看起来像一个静态链接,在Rails解释时不会改变;我想它的保存是为了在你的视图中插入原始的HTML。

However:

然而:

<%= link_to("#", :class=>"button small-button green-button") do %>
  Log in
  <span class="button-right"></span>
<% end %>

Reference.

参考。

#4


3  

The following worked for me. I don't know why other pieces of code didn't (different ruby version?).

下面的方法对我很有效。我不知道为什么其他代码块没有(不同的ruby版本?)

<%= link_to content_tag(:span, 'Register'), {:action => "register"}, :class=>"button" %>

#5


1  

To add to Jeremy's answer - for a path, like so:

再加上杰里米的回答——一条小路,就像这样:

          <%= link_to edit_section_path(@section) do %>
              Edit
              <span class="fa fa-list pull-right"></span>
          <% end %>

#1


43  

You can use the block form of link_to for that:

您可以使用link_to的块形式:

<%= link_to "#", :class => "button small-button green-button" do %>
  Log in
  <span class="button-right"></span>
<% end %>

#2


4  

The simplest way to do it is by using html_safe or raw functions

最简单的方法是使用html_safe或raw函数。

<%= link_to 'Log In<span class="button-right"></span>'.html_safe %>

or using raw function (recommended)

或者使用原始函数(推荐)

<%= link_to raw('Log In<span class="button-right"></span>') %>

Simple as it can get !!

就这么简单!

Don’t use html_safe method unless you’re sure your string isn’t nil. Instead use the raw() method, which wont raise an exception on nil.

不要使用html_safe方法,除非您确定您的字符串不是nil。而是使用raw()方法,它不会在nil上引发异常。

#3


3  

Your snippet looks like a static link, that will never change when interpreted by Rails; I suppose its save to insert the raw HTML in your view.

您的代码片段看起来像一个静态链接,在Rails解释时不会改变;我想它的保存是为了在你的视图中插入原始的HTML。

However:

然而:

<%= link_to("#", :class=>"button small-button green-button") do %>
  Log in
  <span class="button-right"></span>
<% end %>

Reference.

参考。

#4


3  

The following worked for me. I don't know why other pieces of code didn't (different ruby version?).

下面的方法对我很有效。我不知道为什么其他代码块没有(不同的ruby版本?)

<%= link_to content_tag(:span, 'Register'), {:action => "register"}, :class=>"button" %>

#5


1  

To add to Jeremy's answer - for a path, like so:

再加上杰里米的回答——一条小路,就像这样:

          <%= link_to edit_section_path(@section) do %>
              Edit
              <span class="fa fa-list pull-right"></span>
          <% end %>