Ruby on Rails 3.2.13 – 使用I18n获取自定义错误页面的错误

我有一个Rails 3.2.13应用程序,我最初使用路由filtergem为I18n。 我计划在Rails 4中重写它。我删除了gem,因为没有可用的Rails 4生产版本​​,我无法让beta版本工作。 我成功地为大多数应用程序设置了路由。 我唯一的问题是我的自定义错误页面。

我在使用routing-filter gem时创建了自定义错误页面。 我需要有关如何为自定义错误页面设置路由的帮助。

以下是我在config / application.rb中进行配置的方法

config.exceptions_app = self.routes 

这是我在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 

这是我在使用路由filtergem时在路由文件中的内容。

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

现在这些语句都在“/:locale”do语句的范围内 。 如果我键入发现错误时应显示的路径,则会显示正确的页面。 例如,在开发中,如果我显示http://localhost:3000/fr/404http://localhost:3000/fr/500 ,则会正确显示法语自定义错误页面。 如果我有/ en英文自定义错误页面显示预期。

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

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

当这个退出工作时,我将respond_to语句添加到errors_controller.rb。 但是我仍然得到同样的错误。

  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点CDT GMT-5

我也在范围声明和外部尝试了以下但仍然得到相同的错误。 我也使用了没有locale子句的match语句,但得到了同样的错误。

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

发生错误时如何设置区域设置? 我一直在搜索人们使用自定义错误页面和I18n的post或示例,但到目前为止我还没有找到任何内容。

任何帮助,将不胜感激。

由于我没有收到关于这个问题的任何意见,我决定尝试在公共文件夹中使用I18n进行自定义错误页面。 我创建了这篇文章Rails 4 – 希望使用I18n在公用文件夹中执行自定义错误页面 。 我能够找到一个在我的post中运作良好且细节的解决方案。

Interesting Posts