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

后端 未结 5 1682
醉话见心
醉话见心 2020-12-31 03:29

I am basically trying to get this result:

        
            Log in
            

        
相关标签:
5条回答
  • 2020-12-31 03:42

    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.

    0 讨论(0)
  • 2020-12-31 03:45

    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.

    0 讨论(0)
  • 2020-12-31 03:46

    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 %>
    
    0 讨论(0)
  • 2020-12-31 04:04

    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" %>
    
    0 讨论(0)
  • 2020-12-31 04:05

    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 %>
    
    0 讨论(0)
提交回复
热议问题