如何在HAML中的文本字符串中放置一个链接?

时间:2016-03-02 16:22:12

标签: ruby-on-rails haml

这应该比它更容易。我只想在HTML段落元素中添加一个链接。

%p{class: "answer"}="Please upload your data to this portal in text (tab-separated) format. Download our template #{raw(link_to 'here', '/templates/upload_template.xlsx')} for sample data and a description of each column."

Rails正在编码标签信息。我不希望标签被编码。我希望它们成为标签。

2 个答案:

答案 0 :(得分:3)

你可以在任何一个块中使用多行,为了解决你的问题我们会有这样的事情:

%p{class: "answer"}
  Please upload your data to this portal in text (tab-separated) format. Download our template 
  = link_to 'here', '/templates/upload_template.xlsx'
  for sample data and a description of each column."

答案 1 :(得分:2)

您可以使用interpolation directly in Haml,这样做似乎可以解决此问题。

所以不要这样做:

%p= "Text with #{something_interpolated} in it."

你可以做到

%p Text with #{something_interpolated} in it.

即。您不需要=或引号,因为您只是添加一个字符串。您也不需要使用raw

(另请注意,您可以%p.answer设置class属性,如果未动态设置该值,则该属性可能更清晰。)

为什么这是在这里被转义是另一回事。我原以为这两种情况(%p= "#{foo}"%p #{foo})的行为方式相同。但是,经过一些研究后,这种行为似乎与Rails与Erb的行为方式相符。

相关问题