将haml标签放在link_to帮助器中

时间:2012-03-08 14:13:45

标签: ruby hyperlink haml

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

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

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

预期产出:

<a href="path/to/page.html">Other Page<span class="icon">Arrow</span></a>

3 个答案:

答案 0 :(得分:106)

你应该使用块

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

答案 1 :(得分:11)

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

- link_to "path/to/page.html" do
  Other page
  %span.icon Arrow

答案 2 :(得分:5)

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

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

或使用原始功能(推荐)

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

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

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

相关问题