Ruby on Rails 3.2.13 - Getting Error With Custom Error Pages Using I18n

瘦欲@ 提交于 2019-12-25 00:43:25

问题


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.

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.

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

config.exceptions_app = self.routes

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

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'

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.

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

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

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

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

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.

Any help would be appreciated.


回答1:


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.



来源:https://stackoverflow.com/questions/19440043/ruby-on-rails-3-2-13-getting-error-with-custom-error-pages-using-i18n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!