Цепочка метода псевдонимов в Рубин
-
25-09-2019 - |
Вопрос
Я переписываю метод Render Controller, однако я хочу использовать старый метод, когда в методе 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
как это :)
Не связан с StackOverflow