更新到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)
但是,一切都將返回相同的錯誤。任何人都知道正確的語法?
你的動作中是否有其他'redirect_to'或'render'?試試這個:'return redirect_to(root_path)'。 – Gerry
@Gerry我加了全部動作。該代碼也不起作用。 –
由於沒有其他'redirect_to'或'render'存在,我還會建議使用'byebug'來定位代碼調用第二次渲染的位置。 – Gerry