简体   繁体   中英

Rails error messages and redirections

I created a webpage and a XML REST service.

0. I would like to know how errors are handled in Rails (general).

1. Let say that a controller does not exists and I would like to redirect a user to a default "Page not found" address (website) or show a default error status code (REST).

2. What is a best practice for handling controller action errors eg when saving but a record is not saved or some param[] field does not exists or else. Usually I use rescue command and I repeat this in every create or update action (would be cool to have some kind of general error handling for that case).

Thank you!

  1. Rails handles 404 and 500 errors out-of-the-box. In development mode you will see detailed error messages. However, in production Rails will use the 404.html and 500.html pages in the public directory.

  2. If the route is not recognised, Rails will return a 404 error.

  3. I generally don't handle errors at the controller level unless they are expected - so I don't wrap everything in begin...rescue. Rails will return the 500 error page if something fatal has occurred. If I expect a particular set of parameters I validate before proceeding and in this case have a redirection or return result to indicate the incomplete parameters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM