我只想給出有關redirect_to行爲的確認。redirect_to!= return
我有代碼看起來像:
if some_condition
redirect_to(path_one)
end
redirect_to(path_two)
如果some_condition =真正的我得到錯誤:
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.
看來這個方法繼續redirect_to的調用後執行。我是否需要創建如下代碼:
if some_condition
redirect_to(path_one)
return
end
redirect_to(path_two)
?
+1 for RTFM ;-) – spume 2014-04-09 16:37:34
如果爲了更好地構建代碼,將重定向放在控制器中的私有'helper'方法中。我假設私人方法內的返回形式不會完成這項工作,對嗎?處理這個問題的慣用方法是什麼?或者是否必須將所有重定向放在控制器操作的頂層? – pitosalas 2014-04-29 22:49:45
@pitosalas請參閱http://guides.rubyonrails.org/action_controller_overview.html#filters。它說'如果之前「過濾器呈現或重定向之前,行動將不會運行。' – 2014-05-01 17:35:49