小编Gui*_*ume的帖子

如何转义yield返回的值

我最近有一个问题是逃避模板中的yield返回的值.

在我的布局中,我产生了元描述,以便我可以从我的模板中定义它

<meta name="description" content="<%= yield :html_description %>" />
Run Code Online (Sandbox Code Playgroud)

这是我的模板,不幸的是,并没有像预期的那样逃避价值:

<% content_for :html_description, 'hello "you" guy' %>
<meta name="description" content="hello "you" guy" />
Run Code Online (Sandbox Code Playgroud)

我试图用h()escaper逃脱它,但它不起作用:

<meta name="description" content="<%= h(yield :html_description) %>" />
<meta name="description" content="hello "you" guy" />
Run Code Online (Sandbox Code Playgroud)

我也尝试使用escape_once(),但它做得太多了:

<meta name="description" content="<%= escape_once(yield :html_description) %>" />
<meta name="description" content="hello &amp;quot;you&amp;quot; guy" />
Run Code Online (Sandbox Code Playgroud)

但是,通过将返回的值与字符串连接起来,它可以解决问题:

<meta name="description" content="<%= '' + (yield :html_description) %>" />
<meta name="description" content="hello &quot;you&quot; guy" />
Run Code Online (Sandbox Code Playgroud)

有谁理解这种行为?

你有一个比这个串联更好的解决方案,巧合吗?

我正在使用Rails 2.3.8 - 谢谢!

html yield escaping ruby-on-rails

6
推荐指数
2
解决办法
1327
查看次数

标签 统计

escaping ×1

html ×1

ruby-on-rails ×1

yield ×1