异常处理不适用于 rails 中的 find_by 查询,代码如下:
ab = User.find_by_uniq_token(params[:uniq_token])
Run Code Online (Sandbox Code Playgroud)
不提高例外...而:
ab = User.find(:id) # is working fine...
Run Code Online (Sandbox Code Playgroud)
我在应用程序控制器中有异常处理代码,例如:
rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found
def record_not_found
if current_user
flash[:notice] = "Record not found "
redirect_to authenticated_root_url
else
flash[:notice] = "Record not found ."
redirect_to unauthenticated_root_url
# Assuming you have a template named 'record_not_found'
end
end
Run Code Online (Sandbox Code Playgroud)