Rails 3 小部件助手部分

发布于 2024-09-19 06:30:25 字数 612 浏览 3 评论 0原文

我在将应用程序迁移到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事未完 2024-09-26 06:30:25
def widget(widget, options={})
  begin
    unless options[:fullpath]
      raw render(:partial => widget_path(widget, options[:global]))
    else
      raw render("widgets/#{widget}"))
    end
  rescue ActionView::MissingTemplate
    raw "<!-- widget: #{widget.inspect}, #{options.inspect} -->"
  end
end

Rails 3 的 raw 方法与 Rails 2 中的 h 方法相反。在 Rails 2 上使用 h 方法转义字符串。在 Rails 3 上,默认情况下会对视图输出的字符串进行转义,并且可以通过 raw 方法禁用转义。

def widget(widget, options={})
  begin
    unless options[:fullpath]
      raw render(:partial => widget_path(widget, options[:global]))
    else
      raw render("widgets/#{widget}"))
    end
  rescue ActionView::MissingTemplate
    raw "<!-- widget: #{widget.inspect}, #{options.inspect} -->"
  end
end

The raw method of Rails 3 does the reverse of h method in Rails 2. Escaping a string was done with the h method on Rails 2. On Rails 3, strings output from a view are escaped by default, and the escaping can be disabled by the raw method.

翻身的咸鱼 2024-09-26 06:30:25

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文