我重写控制器渲染方法,但是,我想,当使用老方法在render_to_string方法。这是我的当前代码:

def render_with_xhr(options = {}, extra_options = {}, xhr_check = true, &block)
  if xhr_check && request.xhr?
    template = render_to_string(options)
    render_without_xhr(:update) {|page| page.replace_html("#popup .dialog", template)}
  else
    render_without_xhr(options, extra_options, &block)
  end
end

alias_method_chain :render, :xhr

所发生的是,由于render_to_string利用渲染(大概),我在无限循环告终。我怎样才能使它退回到旧的方法,只是该行我的新的渲染方法?

我调整从接受的答案代码,最终代码如下:

def render_to_string(options = {}, &block)
  render(options, {}, false, &block)
ensure
  response.content_type = nil
  erase_render_results
  reset_variables_added_to_assigns
end

def render_with_xhr(options = nil, extra_options = {}, xhr_check = true, &block)
  if xhr_check && request.xhr?
    template = render_to_string(options)
    render_without_xhr :update do |page|
      page.replace_html("#popup .dialog", template)
    end
  else
    render_without_xhr(options, extra_options, &block)
  end
end

alias_method_chain :render, :xhr
有帮助吗?

解决方案

您可以在第2行通过一些独特的价值选择哈希,然后发现它在你的代码,并删除

def render_with_xhr(options = {}, extra_options = {}, xhr_check = true, &block)
  if xhr_check && request.xhr? && !options.delete(:bacon)
    template = render_to_string(options.merge(:bacon => true))
    render_without_xhr(:update) {|page| page.replace_html("#popup .dialog", template)}
  else
    render_without_xhr(options, extra_options, &block)
  end
end

alias_method_chain :render, :xhr

这样:)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top