相关疑难解决方法(0)

捕获rails控制器中的所有异常

有没有办法在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"
end
Run Code Online (Sandbox Code Playgroud)

谢谢

ruby-on-rails

87
推荐指数
4
解决办法
9万
查看次数

需要在Rails中返回JSON格式的404错误

我在我的Rails应用程序中有一个普通的HTML前端和一个JSON API.现在,如果有人调用/api/not_existent_method.json它,则返回默认的HTML 404页面.有没有办法将此更改为类似于{"error": "not_found"}保留HTML前端的原始404页面完整的内容?

ruby-on-rails

76
推荐指数
4
解决办法
4万
查看次数

标签 统计

ruby-on-rails ×2