-->

Rails 4 - Would Like To Do Custom Error Pages in P

2019-08-11 11:43发布

问题:

I had custom error pages in my Rails 3.2.13 application using config.exceptions_app = self.routes in config/application.rb with my logic in errors_controller and views in app/views/errors. I was using the Rails 3 version of the routing-filter gem which took care of the routing. I removed the gem because it does not work with Rails 4.

I rewrote the routing logic using the native Rails functionality and rewrote the application in Rails 4. Using the native Rails functionality broke the logic I had for my custom error pages. You can read the details of what I attempted to do to correct the logic on Ruby on Rails 3.2.13 - Getting Error With Custom Error Pages Using I18n. I have done many web searches about this but I cannot find any information about this. I have found multiple solutions that supposedly work for websites that have not been localized. However I need one for localized websites.

I have decided for now to attempt to accomplish this by moving the error pages back to the public folder if possible. Can I just move my html.erb files from app/views/errors to the public folder and the pages find my translations in the locale files? Again I have done many web searches and as of now I have not found any examples of custom error pages for Rails 4 applications using I18n. There is nothing in the Rails Guides about error pages in the public folder or anywhere else.

Any help would be appreciated.

回答1:

After searching the past few days I decided to try something that I read on a post on Stack Overflow. The solution presented on How can I use i18n for my error pages in Rails 3? was introduced in Rails 2.3. A part of the solution was to create a html file for each available locale such as 404.en.html. 404.fr.html, etc. It included adding a file in lib/tasks and other stuff that was more than I wanted to do.

I decided to go ahead and create html error files for each locale without doing the other stuff recommended. The logic works beautifully.

It appears that config.exceptions_app = self.routes has been deprecated. I cannot say this officially because I never found any documentation stating this. I successfully updated all my localized Rails applications.

UPDATE 11/8/2013 9:35 am CST

I posted questions about my issue on other blog posts where they discussed custom error pages in Rails 3. On one of them the blog post author confirmed that the accessor for exceptions_app has been deprecated in Rails 4. Thankfully the custom error page process has been simplified in Rails 4.