2010-11-04 76 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