开发者

Rails error messages and redirections

开发者 https://www.devze.com 2022-12-21 04:52 出处:网络
I created a webpage and a XML REST service. 0. I would like to know how errors are handled in Rails (general).

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 e.g. 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 tha开发者_高级运维t 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号