Put haml tags inside link_to helper

Is it possible to add html content inside link_to helper in HAML?

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

= link_to "Other page", "path/to/page.html" %span.icon Arrow 

expected output:

 <a href="path/to/page.html">Other Page<span class="icon">Arrow</span></a> 
+45
ruby hyperlink haml
Mar 08 2018-12-12T00:
source share
3 answers

You must use block

 = link_to "path/to/page.html" do Other page %span.icon Arrow 
+104
Mar 08 2018-12-12T00:
source share

If someone is still using Rails 2.x in a project, it seems that the accepted answer returns a 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 
+10
Nov 29 '12 at 5:26
source share

The easiest way to do this is to use html_safe or raw functions

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

or using the raw function (recommended)

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

Just as soon as possible!

Do not use the html_safe method unless you are sure your string is non-zero. Instead, use the raw () method, which will not throw an exception on nil.

+5
Feb 11 '14 at 15:08
source share



All Articles