how to handle errors like 404 / 500 in rails3

前端 未结 3 599
广开言路
广开言路 2021-02-09 12:22

Hey, I hope you can help me.

I am trying to find a way to direct the user to the default error pages 404.html and 500.html in my public folder.

So when there is

相关标签:
3条回答
  • 2021-02-09 12:44

    Have a look at this post to redirect all requests causing a Routing Error.

    0 讨论(0)
  • 2021-02-09 12:45

    Happens automatically if run in production mode - no need that you do that manually.

    0 讨论(0)
  • 2021-02-09 12:46

    Rails does this for you automatically when running in production mode. When you upload your application to a live server, Rails takes care of handling those exceptions and rendering the correct error pages with the correct header status. If you're trying to see what those pages look like (for testing or something), just access them directly via http://localhost:3000/404.html

    Whenever you set up your Rails application on a live server (let's use Apache as an example), you give the site root as the /public folder in your application. Then, whenever a request is made to that server address, Apache first looks in that public folder and tries to serve a static asset (this is a configurable option in [environment].rb). If it can't find the requested page, then the request is forwarded through the Ruby stack.

    When in production mode, if Rails encounters an error that isn't handled (i.e begin, rescue), it throws the error the whole way up to the stack, which then tells Apache (again, in my example) to render an appropriate error.

    Here are some common errors that you'll see in development mode and what they render in production mode:

    ActiveRecord::RecordNotFound => 404 (page not found)
    nil.method => 500 (server error) unless you turn off whiny nils
    ActionController::RoutingError => 404 (page not found)
    
    0 讨论(0)
提交回复
热议问题