Rails 4: 在 API 控制器中使用异常处理程序模块
Rails 4: Using an exception handler module in API controller
我正在尝试在我的控制器中使用异常处理程序,但它似乎没有做任何事情:
class API::PaymentsController < ApplicationController
before_filter :set_client
def index
@foo = @client.foo
end
...
def set_client
rescue ActiveRecord::RecordNotFound do |e|
render json: e.message, status: :not_found
end
@client = Client.find(params[:client_id])
end
end
当我转到 localhost:3000/clients/[invalid_id]/payments
时,我仍然看到错误:
ActiveRecord::RecordNotFound (Couldn't find Client with 'id'=1):
app/controllers/payments_controller.rb:58:in `set_client'
当我希望 RecordNotFound
处理程序处理此异常时。我错过了什么?
救援块在失败后出现。
def set_client
@client = Client.find(params[:client_id])
rescue ActiveRecord::RecordNotFound => err
render json: { message: err.message}, status: :not_found
end
我正在尝试在我的控制器中使用异常处理程序,但它似乎没有做任何事情:
class API::PaymentsController < ApplicationController
before_filter :set_client
def index
@foo = @client.foo
end
...
def set_client
rescue ActiveRecord::RecordNotFound do |e|
render json: e.message, status: :not_found
end
@client = Client.find(params[:client_id])
end
end
当我转到 localhost:3000/clients/[invalid_id]/payments
时,我仍然看到错误:
ActiveRecord::RecordNotFound (Couldn't find Client with 'id'=1):
app/controllers/payments_controller.rb:58:in `set_client'
当我希望 RecordNotFound
处理程序处理此异常时。我错过了什么?
救援块在失败后出现。
def set_client
@client = Client.find(params[:client_id])
rescue ActiveRecord::RecordNotFound => err
render json: { message: err.message}, status: :not_found
end