Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7823697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:20:30+00:00 2026-06-02T08:20:30+00:00

I am having a normal HTML frontend and a JSON API in my Rails

  • 0

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 there any way to change this to something like {"error": "not_found"} while leaving the original 404 page for the HTML frontend intact?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T08:20:31+00:00Added an answer on June 2, 2026 at 8:20 am

    A friend pointed me towards a elegant solution that does not only handle 404 but also 500 errors. In fact, it handles every error. The key is, that every error generates an exception that propagates upwards through the stack of rack middlewares until it is handled by one of them. If you are interested in learning more, you can watch this excellent screencast. Rails has it own handlers for exceptions, but you can override them by the less documented exceptions_app config option. Now, you can write your own middleware or you can route the error back into rails, like this:

    # In your config/application.rb
    config.exceptions_app = self.routes
    

    Then you just have to match these routes in your config/routes.rb:

    get "/404" => "errors#not_found"
    get "/500" => "errors#exception"
    

    And then you just create a controller for handling this.

    class ErrorsController < ActionController::Base
      def not_found
        if env["REQUEST_PATH"] =~ /^\/api/
          render :json => {:error => "not-found"}.to_json, :status => 404
        else
          render :text => "404 Not found", :status => 404 # You can render your own template here
        end
      end
    
      def exception
        if env["REQUEST_PATH"] =~ /^\/api/
          render :json => {:error => "internal-server-error"}.to_json, :status => 500
        else
          render :text => "500 Internal Server Error", :status => 500 # You can render your own template here
        end
      end
    end
    

    One last thing to add: In the development environment, rails usally does not render the 404 or 500 pages but prints a backtrace instead. If you want to see your ErrorsController in action in development mode, then disable the backtrace stuff in your config/enviroments/development.rb file.

    config.consider_all_requests_local = false
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that generates normal random number matrix having normal distribution using
I have a JSP page that renders a block of HTML. In normal usage,
I'm having some trouble with adding tips to a Request.HTML function. I have a
I'm having trouble with my emails with attached file. The headers, including html(parsed as
I am using the following jQuery plug-in, i.e: http://flowplayer.org/tools/scrollable.html The issue I am having
I'm working on a simple HTML site and having trouble with the navigation not
There is already the html() function in jQuery. The problem I am having with
I'm having a problem implementing custom 404 error pages on my Windows/IIS/PHP webhost. Whenever
How to post a message text having HTML codes in it, in FB wall
Having trouble getting to this span tag to add and remove HTML content for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.