Ruby on Rails 3.2.13 - 使用 I18n 获取自定义错误页面的错误 [英] Ruby on Rails 3.2.13 - Getting Error With Custom Error Pages Using I18n

查看:44
本文介绍了Ruby on Rails 3.2.13 - 使用 I18n 获取自定义错误页面的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Rails 3.2.13 应用程序,我最初在其中使用了 I18n 的路由过滤器 gem.我打算在 Rails 4 中重写它.我删除了 gem,因为没有可用的 Rails 4 生产版本,我无法让测试版工作.我成功地为大多数应用程序设置了路由.我遇到的唯一问题是我的自定义错误页面.

I have a Rails 3.2.13 application where I originally used the routing-filter gem for I18n. I plan to rewrite it in Rails 4. I removed the gem since there is no Rails 4 production version available and I could not get the beta version to work. I was successful in getting the routing set up for most of the application. The only problem I am having is with my custom error pages.

我在使用路由过滤器 gem 时创建了自定义错误页面.我需要有关如何为自定义错误页面设置路由的帮助.

I created custom error pages when I was using the routing-filter gem. I need help on how I should set up my routes for the custom error pages.

这是我在 config/application.rb 中的配置

Here is how I have the configuration in config/application.rb

config.exceptions_app = self.routes

这是我在 application_controller.rb 中的内容

Here is what I have in application_controller.rb

  before_filter :set_locale

  def default_url_options(options={}) 
    { :locale => I18n.locale }
  end

  private
    def set_locale
      I18n.locale = (params[:locale] if params[:locale].present?) || cookies[:locale] || 'en'
      cookies[:locale] = I18n.locale if cookies[:locale] != I18n.locale.to_s
    end

这是我使用路由过滤器 gem 时路由文件中的内容.

Here is what I had in my routes file when I was using the routing-filter gem.

match "/404",                   to: 'errors#error_404'
match "/500",                   to: 'errors#error_500'

现在这些语句在 scope "/:locale" do 语句内.如果我输入发现错误时应出现的路线,则会显示正确的页面.例如在开发中,如果我显示 http://localhost:3000/fr/404http://localhost:3000/fr/500 法语自定义错误页面显示适当地.如果我有/en 预期会显示英文自定义错误页面.

Now those statements are within the scope "/:locale" do statement. If I type the route that should appear when errors are found the correct page displays. For example in development if I display http://localhost:3000/fr/404 or http://localhost:3000/fr/500 the French custom error pages display properly. If I have /en the English custom error pages displays expected.

发生的情况是,当我收到错误时,会将语言环境分配给错误编号.着陆页不是显示正确的错误页面,而是显示在它试图找到错误编号的翻译的地方,而这些翻译当然不存在.我不明白语言环境是如何设置为错误号的.我认为我在 application_controller.rb 中的内容应该设置为 I18n.locale.我还尝试将 config/routes.rb 中的 scope 语句更改为下面的代码.两者都产生了相同的结果.

What happens is when I get an error the locale is assigned to the error number. Instead of displaying the proper error page the landing page is display where it attempts to find the translations for the error number which of course do not exist. I'm not understanding how the locale is getting set to the error number. I would think that with what I have in application_controller.rb that it should be set to I18n.locale. I also tried changing the scope statement in config/routes.rb to the code below. Both of them produced the same results.

scope "/:locale", defaults: { :locale => I18n.locale.to_s } do

scope "/:locale", defaults: { :locale => I18n.locale } do

当它停止工作时,我将 respond_to 语句添加到了 errors_controller.rb.但是我仍然遇到相同的错误.

When this quit working I added the respond_to statements to errors_controller.rb. However I still get the same errors.

  def error_404
    @page_title     = t(:error_title_404)
    respond_to do |format|
      format.html { render status: 404 }
      format.any  { render text: "#{t :error_title_404_alt}", status: 404 }
    end
  end

  def error_500
    @page_title     = t(:error_title_500)
    respond_to do |format|
      format.html { render status: 500 }
      format.any  { render text: "#{t :error_title_500_alt}", status: 500 }
    end
  end

更新 10/21/2013 10 am CDT GMT-5

UPDATE 10/21/2013 10 am CDT GMT-5

我还在范围语句内和外尝试了以下操作,但仍然出现相同的错误.我还使用了没有 locale 子句的 match 语句,但得到了同样的错误.

I have also tried the following within the scope statement and outside but still get the same error. I have also used the match statements without the locale clause but get the same error.

match "/404",                           to: redirect('/:locale/error_404'), locale: I18n.locale
match "/500",                           to: redirect('/:locale/error_500'), locale: I18n.locale

发生错误时如何设置语言环境?我一直在寻找人们使用自定义错误页面和 I18n 的帖子或示例,但到目前为止我还没有找到.

How do I set the locale when errors occur? I have been searching for posts or examples where people are using custom error pages along with I18n but so far I have not found any.

任何帮助将不胜感激.

推荐答案

由于我没有收到有关此问题的任何输入,因此我决定尝试使用公共文件夹中的 I18n 执行自定义错误页面.我创建了这篇文章 Rails 4 - 想使用 I18n 在公共文件夹中创建自定义错误页面.我能够在我的帖子中找到一个运行良好且详细的解决方案.

Since I did not receive any input on this question I decided to attempt to do custom error pages using I18n in the public folder. I created this post Rails 4 - Would Like To Do Custom Error Pages in Public Folder Using I18n. I was able to find a solution which works well and detail in my post.

这篇关于Ruby on Rails 3.2.13 - 使用 I18n 获取自定义错误页面的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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