質問

私は、コントローラのrenderメソッドを書き換えています、しかし、私はとき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