将haml标签放在link_to帮助器中

fla*_*flo 46 ruby haml hyperlink

是否可以在HAML中的link_to帮助器中添加html内容?

我试过这个,但我得到的只是语法错误:

= link_to "Other page", "path/to/page.html"
    %span.icon Arrow
Run Code Online (Sandbox Code Playgroud)

预期产量:

<a href="path/to/page.html">Other Page<span class="icon">Arrow</span></a>
Run Code Online (Sandbox Code Playgroud)

Mic*_*jbe 106

你应该使用块

= link_to "path/to/page.html" do
  Other page
  %span.icon Arrow
Run Code Online (Sandbox Code Playgroud)


Pau*_*aul 11

如果有人仍在项目中使用Rails 2.x,则看起来接受的答案会返回该块,从而复制标记中的链接.非常简单的改变:使用-而不是=

- link_to "path/to/page.html" do
  Other page
  %span.icon Arrow
Run Code Online (Sandbox Code Playgroud)


Nik*_*ppa 5

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

= link_to 'Other Page<span class="icon"></span>'.html_safe, "path/to/page.html"
Run Code Online (Sandbox Code Playgroud)

或使用原始功能(推荐)

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

简单,因为它可以得到!!

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