Question

Je réécris cependant, la méthode rend le contrôleur, je veux utiliser l'ancienne méthode lorsque la méthode de render_to_string. Ce sont mes codes actuels:

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

Qu'est-ce qui se passe est que, depuis render_to_string fait usage de rendre (sans doute), je finis dans une boucle infinie. Comment puis-je faire revenir à l'ancienne méthode juste pour cette ligne ma nouvelle méthode render?

J'ai modifié les codes de la réponse acceptée, le code final est ci-dessous:

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
Était-ce utile?

La solution

vous pourriez à la ligne 2 passe une valeur unique pour les options hachage, puis détecter dans votre code et supprimer

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

comme ça:)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top