2010-06-09 24 views
0

我正在重寫控制器渲染方法,但是,我想在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

,你可以在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 

那樣:)

+0

謝謝同行redditor,我會試試看並回報=) – Jaryl 2010-06-09 14:06:26