Rails 4 - 想使用 I18n 在公共文件夹中创建自定义错误页面 [英] Rails 4 - Would Like To Do Custom Error Pages in Public Folder Using I18n

查看:46
本文介绍了Rails 4 - 想使用 I18n 在公共文件夹中创建自定义错误页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 config/application.rb 中使用 config.exceptions_app = self.routes 在我的 Rails 3.2.13 应用程序中有自定义错误页面,我的逻辑在 errors_controller 中,视图在 app/views/errors 中.我使用的是 Rails 3 版本的路由过滤器 gem,它负责路由.我删除了 gem,因为它不适用于 Rails 4.

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.

我使用本机 Rails 功能重写了路由逻辑,并在 Rails 4 中重写了应用程序.使用本机 Rails 功能破坏了我的自定义错误页面的逻辑.您可以在 Ruby on Rails 3.2.13 - 使用 I18n 获取自定义错误页面的错误.我已经做了很多关于这个的网络搜索,但我找不到任何关于这个的信息.我发现了多种解决方案,据说适用于尚未本地化的网站.但是我需要一个本地化网站.

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.

我现在决定尽可能通过将错误页面移回公共文件夹来尝试完成此操作.我可以将我的 html.erb 文件从 app/views/errors 移动到公共文件夹,然后页面在语言环境文件中找到我的翻译吗?我再次进行了许多网络搜索,到目前为止,我还没有找到任何使用 I18n 的 Rails 4 应用程序的自定义错误页面示例.Rails 指南中没有关于公共文件夹或其他任何地方的错误页面的内容.

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.

任何帮助将不胜感激.

推荐答案

在过去几天的搜索之后,我决定尝试一些我在 Stack Overflow 上的帖子中看到的内容.如何使用i18n for my error pages in Rails 3? 是在 Rails 2.3 中引入的.解决方案的一部分是为每个可用的语言环境创建一个 html 文件,例如 404.en.html.404.fr.html 等.它包括在 lib/tasks 中添加一个文件和其他我不想做的东西.

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.

我决定继续为每个语言环境创建 html 错误文件,而不做其他推荐的事情.逻辑很好.

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

看来 config.exceptions_app = self.routes 已被弃用.我不能正式这么说,因为我从未找到任何说明这一点的文件.我成功更新了所有本地化的 Rails 应用程序.

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.

更新 11/8/2013 中部标准时间上午 9:35

我在其他博客文章中发布了有关我的问题的问题,他们讨论了 Rails 3 中的自定义错误页面.在其中一篇博客文章作者确认,Rails 4 中已弃用 exceptions_app 的访问器.谢天谢地,自定义错误页面流程在 Rails 4 中进行了简化.

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.

这篇关于Rails 4 - 想使用 I18n 在公共文件夹中创建自定义错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆