コントローラーの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 は 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