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.