Rails 3 小部件助手部分
我在将应用程序迁移到 Rails 3 时遇到了一个大问题。我有一个视图助手,我称之为“WidgetHelper”。当我引用它时,它有助于自动渲染一些部分。例如
<%= widget('loginbox', :global => true) %>
但它无法正常工作。它按照我想要的方式呈现 HTML 代码,但转义了返回值,这不是预期的。我如何告诉渲染(或某些东西)不为我转义返回值?
这是我的代码:
def widget(widget, options={})
begin
unless options[:fullpath]
render :partial => widget_path(widget, options[:global])
else
render "widgets/#{widget}"
end
rescue ActionView::MissingTemplate
"<!-- widget: #{widget.inspect}, #{options.inspect} -->"
end
end
I ran into a big problem in moving my application to Rails 3. I have a view helper which i call 'WidgetHelper'. It helps to render some partials automatically when i refer to it. For example
<%= widget('loginbox', :global => true) %>
But it not works correctly. It renders HTML code as I want, but escapes the return value, what is not expected. How can I tell to render (or to something) to not escape the return value for me?
Here is my code:
def widget(widget, options={})
begin
unless options[:fullpath]
render :partial => widget_path(widget, options[:global])
else
render "widgets/#{widget}"
end
rescue ActionView::MissingTemplate
"<!-- widget: #{widget.inspect}, #{options.inspect} -->"
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Rails 3 的
raw
方法与 Rails 2 中的h
方法相反。在 Rails 2 上使用h
方法转义字符串。在 Rails 3 上,默认情况下会对视图输出的字符串进行转义,并且可以通过raw
方法禁用转义。The
raw
method of Rails 3 does the reverse ofh
method in Rails 2. Escaping a string was done with theh
method on Rails 2. On Rails 3, strings output from a view are escaped by default, and the escaping can be disabled by theraw
method.Rails 3 改变了内容过滤的工作方式 - 它默认假设您希望过滤所有内容。
您可以使用
html_safe
更正此问题:"".html_safe
请参阅:http://asciicasts.com/episodes/204-xss-protection -in-rails-3
Rails 3 changed the way that content filtering works - it by default assumes you want everything filtered.
You can correct this using
html_safe
:"<!-- widget: #{widget.inspect}, #{options.inspect} -->".html_safe
See: http://asciicasts.com/episodes/204-xss-protection-in-rails-3