Need to return JSON-formatted 404 error in Rails

前端 未结 4 1767
闹比i
闹比i 2020-12-12 11:52

I am having a normal HTML frontend and a JSON API in my Rails App. Now, if someone calls /api/not_existent_method.json it returns the default HTML 404 page. Is

4条回答
  •  再見小時候
    2020-12-12 12:31

    Try to put at the end of your routes.rb:

    match '*foo', :format => true, :constraints => {:format => :json}, :to => lambda {|env| [404, {}, ['{"error": "not_found"}']] }
    

提交回复
热议问题