Ruby on rails Rails:在使用内容_for with raw后无法转义

Ruby on rails Rails:在使用内容_for with raw后无法转义,ruby-on-rails,escaping,Ruby On Rails,Escaping,在助手方法中,我有以下内容: content_for(:title, raw(page_title)) 在我看来,在调用helper方法后,我得到了以下结果: <%= h(content_for(:title)) %> 但是,当我这样做时,h()不会对内容进行HTML转义吗?我还尝试了(:title)的content\u.html\u safe和(:title)的html\u escape(content\u)),但没有成功 我将内容保存为原始内容,因为我希望在单独的视图中访

在助手方法中,我有以下内容:

content_for(:title, raw(page_title))
在我看来,在调用helper方法后,我得到了以下结果:

<%= h(content_for(:title)) %>

但是,当我这样做时,h()不会对内容进行HTML转义吗?我还尝试了(:title)的
content\u.html\u safe
和(:title)的
html\u escape(content\u))
,但没有成功

我将内容保存为原始内容,因为我希望在单独的视图中访问原始(未缩放)内容。
我使用的是Rails 3.0.17。

经过一些调查,我的结论是:这都是关于html\u安全的

让我用一些代码来解释:

page_title.html_safe? #false
raw(page_title).html_safe? #true - that's all that raw does
content_for(:title, raw(page_title)) #associates :title with page_title, where page_title.html_safe? returns true
现在,当视图调用helper方法时,会发生以下情况:

content_for(:title) #no escaping. Since page_title was stored and html_safe is true, conetnt_for will not escape
h(content_for(:title)) #no escaping. Since the last line returned a string where html_safe? returns true, this will also not escape.
<%= h(content_for(:title)) %> #no escaping. Same reason as above
(:title)的
content_#无法转义。由于已存储页面标题且html安全为true,因此不会转义
h(内容(用于(:标题))#不能转义。因为最后一行返回了一个字符串,html\u安全吗?返回true,这也不会转义。
#不能逃跑。原因同上
简而言之,
raw
只是在string/SafeBuffer上设置
html\u safe
属性。转义仅在
string.html\u safe?
返回
false
的字符串上执行。由于字符串在每次转义时都返回
true
,因此该字符串永远不会转义

分辨率:

通过插值或连接创建一个新字符串-这将再次将html_safe设置为false,字符串将被转义


更多信息,请查看本指南并阅读。

经过一些调查,我的结论是:这都是关于html\u安全的

让我用一些代码来解释:

page_title.html_safe? #false
raw(page_title).html_safe? #true - that's all that raw does
content_for(:title, raw(page_title)) #associates :title with page_title, where page_title.html_safe? returns true
现在,当视图调用helper方法时,会发生以下情况:

content_for(:title) #no escaping. Since page_title was stored and html_safe is true, conetnt_for will not escape
h(content_for(:title)) #no escaping. Since the last line returned a string where html_safe? returns true, this will also not escape.
<%= h(content_for(:title)) %> #no escaping. Same reason as above
(:title)的
content_#无法转义。由于已存储页面标题且html安全为true,因此不会转义
h(内容(用于(:标题))#不能转义。因为最后一行返回了一个字符串,html\u安全吗?返回true,这也不会转义。
#不能逃跑。原因同上
简而言之,
raw
只是在string/SafeBuffer上设置
html\u safe
属性。转义仅在
string.html\u safe?
返回
false
的字符串上执行。由于字符串在每次转义时都返回
true
,因此该字符串永远不会转义

分辨率:

通过插值或连接创建一个新字符串-这将再次将html_safe设置为false,字符串将被转义

有关更多信息,请参阅上的本指南并阅读