[Rails] AbstractController::DoubleRenderError

Ruby on Rails 3.1 にて、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”.

下記のように、複数 render 処理が実行させるとエラーになります。

render :index
render :show # ここまで処理されて render 重複エラー

render処理の後に、そこで動作を終了させたい場合は return を書かなければいけません。

render :index and return # ここで処理が終了
render :show # ここは処理されない

以上です。

【参考】

ruby on rails – AbstractController::DoubleRenderError that shouldn’t be – Stack Overflow はてなブックマーク - ruby on rails - AbstractController::DoubleRenderError that shouldn't be - Stack Overflow