Can I use a link_to to generate a link with a span inside?

好久不见. 提交于 2019-11-29 11:48:17

问题


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 ?


回答1:


You can use the block form of link_to for that:

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



回答2:


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

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




回答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.

However:

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

Reference.




回答4:


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

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



回答5:


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


来源:https://stackoverflow.com/questions/4837824/can-i-use-a-link-to-to-generate-a-link-with-a-span-inside

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!