put haml tags inside link_to helper

后端 未结 3 1704
故里飘歌
故里飘歌 2020-12-13 17:42

is it possible to add html-content inside a link_to helper in HAML?

i tried this, but all i get is a syntax error:

= link_to \"Other page\", \"path/t         


        
相关标签:
3条回答
  • 2020-12-13 17:48

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

    = link_to 'Other Page<span class="icon"></span>'.html_safe, "path/to/page.html"
    

    or using raw function (recommended)

    = link_to raw('Other Page<span class="icon"></span>'), "path/to/page.html"
    

    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-13 18:00

    If anyone is still using Rails 2.x on a project, it looks like the accepted answer returns the block, thus duplicating the link in the markup. Very simple change: use - instead of =

    - link_to "path/to/page.html" do
      Other page
      %span.icon Arrow
    
    0 讨论(0)
  • 2020-12-13 18:09

    You should use block

    = link_to "path/to/page.html" do
      Other page
      %span.icon Arrow
    
    0 讨论(0)
提交回复
热议问题