ruby 将 haml 标签放在 link_to 助手中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9618971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 04:52:42  来源:igfitidea点击:

put haml tags inside link_to helper

rubyhyperlinkhaml

提问by flavaflo

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

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

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>

回答by Micha? Szajbe

You should use block

你应该使用块

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

回答by Paul

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 =

如果有人仍在项目中使用 Rails 2.x,那么接受的答案似乎返回了块,从而复制了标记中的链接。非常简单的改变:使用-代替=

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

回答by Nikhil Nanjappa

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

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

= 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.

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