سلسلة طريقة الاسم المستعار في روبي تسمي نفسها

StackOverflow https://stackoverflow.com/questions/3005905

  •  25-09-2019
  •  | 
  •  

سؤال

سأعيد كتابة طريقة عرض وحدة التحكم ، ومع ذلك ، أريد استخدام الطريقة القديمة عندما تكون في طريقة 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

مثل هذا :)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top