只有在救援块中没有返回时,救援块结束后的所有代码才会被解释。所以你可以在你的救援块结束时调用 return 。
def index
begin
@user = User.find(params[:id])
rescue
flash[:notice] = "ERROR"
redirect_to(:action => 'index')
return
end
flash[:notice] = "OK"
redirect_to(:action => 'index')
end
或者
def index
@user = User.find(params[:id])
# after is interpret only if no exception before
flash[:notice] = "OK"
redirect_to(:action => 'index')
rescue
flash[:notice] = "ERROR"
redirect_to(:action => 'index')
end
但在您的情况下,最好使用rescue_from或rescue_in_public
像
class UserController < ApplicationController
def rescue_in_public(exception)
flash[:notice] = "ERROR"
redirect_to(:action => 'index')
end
def index
@user = User.find(params[:id])
flash[:notice] = "OK"
redirect_to(:action => 'index')
end
end
但是使用rescue_in_public并不是一个很好的建议