我想呈现一个在找不到Plan id时发生的错误。我在用失眠症。问题是,它甚至还没有到达if语句。
class Api::V1::PlansController < Api::V1::BaseController
before_action :authorize_request
def show
@plan = Plan.find(params[:id])
if @plan.errors.any?
render json 'wrong'
else
@accounts = @plan.accounts
render json: @plan, status: :created
end
end
end

发布于 2020-11-05 04:50:49
如果找不到if,ActiveRecord::FinderMethods#find将引发ActiveRecord::RecordNotFound异常。当一个异常被抛出时,它会停止执行。
您可以使用rescue_from来处理异常
# Do not use the scope resolution operator when declaring classes and modules
# @see https://github.com/rubocop-hq/ruby-style-guide#namespace-definition
module Api
module V1
class PlansController < BaseController
before_action :authorize_request
rescue_from ActiveRecord::RecordNotFound, with: :not_found
def show
@plan = Plan.find(params[:id])
render json: @plan
end
private
def not_found
render json: { error: 'not found' }, status: :not_found
end
end
end
end最初,使用find_by的建议听起来可能是个好主意,直到您意识到异常确实很有用,因为它会暂停操作的执行并防止出现零错误。
module Api
module V1
class PlansController < BaseController
# ...
before_action :set_plan
def update
# this would create a nil error if we had used 'find_by'
if @plan.update(plan_params)
# ...
else
# ...
end
end
private
def set_plan
@plan = Plan.find(params[:id])
end
end
end
end使用rescue_from也是一个非常强大的模式,因为它允许您在继承链中向上移动错误处理,而不是重复自己:
module Api
module V1
class BaseController < ::ActionController::Api
rescue_from ActiveRecord::RecordNotFound, with: :not_found
private
def not_found
render json: { error: 'not found' }, status: :not_found
end
end
end
end但最有可能的是,你根本就不需要它。Rails通过发送404 - Not Found响应在框架级别上拯救ActiveRecord::RecordNotFound。客户端应该不需要更多的上下文,在这种情况下,状态码和返回完全不必要的JSON错误消息响应是一种反模式。
https://stackoverflow.com/questions/64684101
复制相似问题