Domanda

sto riscrivendo il controller metodo Render, però, voglio usare il vecchio metodo quando nel metodo render_to_string. Questi sono i miei codici attuali:

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

Succede che poiché marchi render_to_string uso di rendering (presumibilmente), finisco in un ciclo infinito. Come posso fare cadere indietro al vecchio metodo solo per quella linea il mio nuovo metodo render?

I ottimizzato codici della risposta accettata, codice finale è qui sotto:

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
È stato utile?

Soluzione

si potrebbe alla linea 2 passare un valore unico per le opzioni di hash, e poi rilevarla nel codice e rimuovere

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

così:)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top