有没有办法在rails控制器中捕获所有未捕获的异常,如下所示:
def delete
schedule_id = params[:scheduleId]
begin
Schedules.delete(schedule_id)
rescue ActiveRecord::RecordNotFound
render :json => "record not found"
rescue ActiveRecord::CatchAll
#Only comes in here if nothing else catches the error
end
render :json => "ok"
endRun Code Online (Sandbox Code Playgroud)
谢谢
我在我的Rails应用程序中有一个普通的HTML前端和一个JSON API.现在,如果有人调用/api/not_existent_method.json它,则返回默认的HTML 404页面.有没有办法将此更改为类似于{"error": "not_found"}保留HTML前端的原始404页面完整的内容?