2017-04-08 25 views
0

更新到Rails 5.0後,我發現了以下錯誤:Rails的5 - 「渲染和/或重定向在這次行動中被多次」

「一個AbstractController :: DoubleRenderError在RegistrationsController#創建

在此動作中多次調用渲染和/或重定向,請注意,您只能調用渲染或重定向,並且每個動作最多調用一次,同時請注意,重定向和渲染都不會終止執行動作,因此如果您想要在重定向後退出動作,您需要執行類似「reditect_to(...)並返回」。「

這是我的代碼,我更新前的工作:

def create 
    # save record 
    if params[:stuff].nil? 
    respond_to do |format| 
     format.js 
    end 
    else 
    redirect_to root_path 
    end 
end 

我已經嘗試了很多不同的語法,例如:

redirect_to(root_path) and return 

redirect_to(root_path) 
return 

return and redirect_to(root_path) 

return redirect_to(root_path) 

但是,一切都將返回相同的錯誤。任何人都知道正確的語法?

+0

你的動作中是否有其他'redirect_to'或'render'?試試這個:'return redirect_to(root_path)'。 – Gerry

+0

@Gerry我加了全部動作。該代碼也不起作用。 –

+0

由於沒有其他'redirect_to'或'render'存在,我還會建議使用'byebug'來定位代碼調用第二次渲染的位置。 – Gerry

回答

2

您可能已經渲染或重定向了您顯示#的位置。

試試這個:

  • 添加gem byebug到您的Gemfile,如果你不已經安裝,運行bundle安裝它,並重新啓動的Rails
  • 添加byebugcreate方法
  • 的開始
  • 從瀏覽器或命令行調用create,並通過ns單步執行,以進入其他功能。您可能會注意到渲染或重定向被調用兩次
+0

我沒有在我的動作中使用任何其他'render'或'redirect_to'實例。 –

0

您可以使用performed?來測試或調試雙渲染/重定向。