Rails 3:用户输入转义在视图和邮件程序中的工作方式不同
我在视图和邮件程序中使用以下代码集:
<%= simple_format(auto_link(h(user_input))) %>
我首先在 user_input 上调用 html_safe (h),以逃避任何危险代码。然后,我调用 auto_link 来启用输入中的任何链接,然后调用 simple_format 来启用换行符等。
在我看来,这完美地工作,并正确显示以下内容,完全转义,但具有工作链接:
" http://google.com "
但是,当在 ActionMailer 电子邮件中显示完全相同的内容时,我会看到所有特殊字符,包括我的自动链接,都是双重的转义(例如 "
结果无法正确显示):
&quot; <a href=3D"http://google.com">http://google.=com</a> &quot;
出于某种原因,我需要再次将其重新标记为 html_safe 才能使其正常工作:
<%= simple_format(auto_link(h(user_input))).html_safe %>
这正确输出:
" <a href=3D"http://google.com">http://google.com</a> "
任何为什么 ActionView 和 ActionMailer 会以不同的方式对待相同的代码?
I'm using the following set of code in both my views and the mailer:
<%= simple_format(auto_link(h(user_input))) %>
I begin by calling html_safe (h) on the user_input, in order to escape any dangerous code. I then call auto_link to enable any links in their input, and then I call simple_format to enable line breaks and such.
This works perfectly in my view, and properly displays the following, fully escaped, yet with a working link:
" http://google.com "
However, when the exact same is displayed in an ActionMailer email, I'm seeing all of the special characters, including my autolink, doubly escaped (the "
for example doesn't display correctly as a result) :
" <a href=3D"http://google.com">http://google.=com</a> "
For some reason, I need to re-mark it as html_safe again to get it working:
<%= simple_format(auto_link(h(user_input))).html_safe %>
This correctly outputs:
" <a href=3D"http://google.com">http://google.com</a> "
Any ideas on why ActionView and ActionMailer treat the same code differently?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您从电子邮件模板中调用 simple_format (以呈现换行符),则您得到的行为非常不寻常,并且事实证明此帮助器被私有方法覆盖。
无论如何,您可以使用此 hack 来访问电子邮件模板中的 simple_format:
希望在另一个 Rails 版本中可以修复此问题。
If you call simple_format from the email template (to render out line breaks), the behavior you get is terribly unusual, and it turns out this helper is overwritten with a private method.
Anyways, you can access simple_format in the email template by using this hack:
Hopefully in another rails release this will be fixed.