Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails ruby中的别名方法链调用自身_Ruby On Rails_Alias Method Chain - Fatal编程技术网

Ruby on rails ruby中的别名方法链调用自身

Ruby on rails ruby中的别名方法链调用自身,ruby-on-rails,alias-method-chain,Ruby On Rails,Alias Method Chain,我正在重写控制器的render方法,但是,我想在render\u to\u 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

我正在重写控制器的render方法,但是,我想在render\u to\u 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使用render(大概),我最终进入了一个无限循环。我怎样才能使它返回到旧的方法,只为那一行我的新渲染方法

我根据已接受的答案调整了代码,最终代码如下:

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行将一些唯一的值传递给options散列,然后在代码中检测并删除它

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

像这样:)

谢谢雷迪托同事,我会尝试一下,然后回来汇报=)