里面视图
问题描述:
标记文本将字符串所以我有这个字符串:里面视图
"<p> Here it is: </p> <p> <a href="http://localhost:3000/" class="button">Open</a> </p>"
在控制器内部生成此字符串,具体取决于几个参数。
我希望能够展示这个字符串在视图中,解释为标记文本:
<div class="row">
<div class="twelve columns panel">
<%= @string_with_markup %>
</div>
</div>
但结果却是,这并不奇怪,在显示的字符串,如,所以它显示了一个一堆标记文本在呈现的页面中。 虽然render_to_string或yield方法,但它不工作,我想我错过了一些东西。
附加信息:
整个目的是要表明由系统发送的电子邮件的正文。 电子邮件是使用ActionMailer生成的,当用户想要查看发送给他的电子邮件时,控制器将调用ActionMailer的适当方法,并提取电子邮件的正文。我谈到的那个string_with_markup其实就是class:Mail :: Body
谢谢!
答
您可以标记该字符串(HTML)的安全在你的控制器(或助手):
@string_with_markup = '<p>Here it is ...</p>'.html_safe
,并使其使用:
<%= @string_with_markup %>
如果您不想在控制器中标记该字符串安全,请使用raw
或<%==
[R比在你看来呼吁html_safe
:
<%= raw @string_with_markup %>
到这相当于:
<%== @string_with_markup %>
见http://guides.rubyonrails.org/v3.2.9/active_support_core_extensions.html#output-safety
答
试试这个:
<div class="row">
<div class="twelve columns panel">
<%= @string_with_markup.html_safe %>
</div>
</div>
快速的答案,但我会添加到它的解释 - html_safe设置“字符串”为HTML安全。这样,您可以随意从助手或模型返回HTML安全字符串。 – David 2013-05-11 18:09:11
太棒了! 由于变量@string_with_markup是类Mail :: Body,我不得不打电话给: '@ string_with_markup.to_s.html_safe' 谢谢! – 2013-05-11 18:12:01