0

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

1 に答える 1

2

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

そのように:)

于 2010-06-09T13:05:03.123 に答える