Ruby on Rails 3.2.13 - Getting errors with custom error pages using I18n

I have a Rails 3.2.13 application in which I originally used the routing filter gem for I18n. I plan on rewriting it in Rails 4. I removed the gem since there is no Rails 4 version available and I couldn't get the beta to work. I was able to get the routing setup for most applications. The only problem I am facing is my own error pages.

I created custom error pages when I used the routing filter gem. I need help on how I can set up routes for custom error pages.

This is how I have my config in config / application.rb

config.exceptions_app = self.routes

      

This 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

      

This is what I had in the routes file when I used the routing filter gem.

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

      

These statements are now in the scope: / locale "do statement . If I type in a route that should appear when errors are encountered, the correct page is displayed. For example, during development, if I display http://localhost:3000/fr/404

or http://localhost:3000/fr/500

display custom error pages in French. If I have there is / en, English custom error pages are expected.

What happens when I get an error, the language is assigned an error number. Instead of displaying the corresponding error page, the landing page is displayed where it tries to find translations for the error number, which of course doesn't exist. I don't understand how the locale is set to the error number. I would think that with what I have in application_controller.rb it should be set to I18n.locale. I've also tried changing the scope statement in config / routes.rb to the following code. They both gave the same results.

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

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

      

When that stopped, I added reply_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:00 CDT GMT-5

I have also tried the following on the scope statement and outside, but still get the same error. I've also used match assertions without the locale clause, but I'm getting 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've searched for posts or examples where people use their own error pages along with I18n, but I haven't found any so far.

Any help would be appreciated.

0


source to share


1 answer


Since I didn't get any data on this, I decided to try and execute custom error pages using I18n on the shared folder. I created this Rails 4 post - would like to make custom error pages on a shared folder using I18n . I was able to find a solution that works well and is detailed in my post.



0


source







All Articles