2010-11-04 40 views
1

我正在研究一個具有Pages控制器和兩個頁面的Rails3應用程序:pages#main和pages#status。主頁面有一個鏈接,點擊鏈接即可進入狀態。用戶已經有配置文件信息,如果該配置文件信息的一部分不存在,我希望狀態重定向到主。雖然我得到了一個神祕的雙重重定向,但我無法解決。這裏的頁面控制器:只要條件不滿足獲取Rails3加載不同視圖

def main 
    @current_user = current_user 
end 

def status 
    @current_user = current_user 
    if @current_user.address.blank? 
    redirect_to :action => "main" and return 
    end 
end 

一切工作順順當當,但只要它是,我得到的錯誤:

Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like "redirect_to(...) and return".

事實上,redirect_to的調用兩次(根據痕跡,同一行;不知道爲什麼)。我想知道這是否是路線問題。這裏是routes.rb:

match '/main', :to => 'pages#main' 
match '/status', :to => 'pages#status' 

任何建議將不勝感激。

這裏是開發日誌:

Started GET "/status" for 127.0.0.1 at Wed Nov 03 21:28:15 -0700 2010 
    Processing by PagesController#status as HTML 
    User Load (0.3ms) SELECT "users".* FROM "users" WHERE ("users"."id" = 3) LIMIT 1 
Before redirect 
Before redirect 
Redirected to 
Redirected to 
Completed in 32ms 

AbstractController::DoubleRenderError (Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like "redirect_to(...) and return".): 
app/controllers/pages_controller.rb:15:in `status' 
app/controllers/pages_controller.rb:15:in `status' 

試圖解決這個問題,我已經添加了兩個調試線的狀態的方法,在希望它會提供一些線索:

logger.debug "Before redirect" 
    redirect_to :action => "main" and return 
    logger.debug "After redirect" 

因此,在「重定向到」之前,「重定向前」行被擊中。然後「重定向到」出現兩次,沒有目標。順便說一句,這發生在有或沒有「並返回」的重定向線上。我真的不知道發生了什麼事。

另外,有趣的是,我在主方法中添加了一條調試線。它從未被觸發。

+0

你能解決這個問題嗎?我面對或多或少相同... – Markus 2011-08-04 12:54:07

回答

0

Rav,你可以粘貼這個請求的開發日誌嗎?特別是,第一次請求/ status會導致重定向到/ main,還是會因上述錯誤而失敗?

具體而言,請在log/development.log中查找最近的請求塊或塊。

+0

我已粘貼在日誌中,以及上面的一些更多信息。謝謝你回到我身旁。 – 2010-11-04 04:35:00

+0

我認爲redirect_to不起作用。嘗試使用「redirect_to(main_url)」並將「 – hybernaut 2010-11-04 05:23:03

+0

」重定向爲「空白」,而不是指定動作,這會導致我相信您的redirect_to無法正常工作。 – hybernaut 2010-11-04 05:24:11