Ruby on rails Rails 4处理区域设置错误页面
大家好, 我使用的是Rails 4,在我的网站中,我使用以下链接来处理区域设置: en-Ruby on rails Rails 4处理区域设置错误页面,ruby-on-rails,ruby-on-rails-4,error-handling,locale,rails-i18n,Ruby On Rails,Ruby On Rails 4,Error Handling,Locale,Rails I18n,大家好, 我使用的是Rails 4,在我的网站中,我使用以下链接来处理区域设置: en- zh HK- zh CN- 此外,我使用自定义错误页方法来处理404、422和500个错误。 i、 e config/routes.rb scope "(:locale)", :locale => /en|zh-CN|zh-HK/ do root 'home#index' get '/' => 'home#index', as: :home_page get 'about' =>
zh HK-
zh CN- 此外,我使用自定义错误页方法来处理404、422和500个错误。 i、 e config/routes.rb
scope "(:locale)", :locale => /en|zh-CN|zh-HK/ do
root 'home#index'
get '/' => 'home#index', as: :home_page
get 'about' => 'home#about', as: :about_page
end
%w( 404 422 500 ).each do |code|
get code, :to => "errors#show", :code => code
end
get '/:locale' => 'home#index'
config/environment/production.rb
Rails.application.configure do
config.exceptions_app = self.routes
end
app/controller/application_controller.rb
class ApplicationController < ActionController::Base
before_filter :set_locale
def set_locale
I18n.locale = params[:locale] || I18n.default_locale
end
end
class ErrorsController < ApplicationController
def show
render status_code.to_s, :status => status_code, :layout => false
end
protected
def status_code
params[:code] || 500
end
end
class ApplicationController
app/controller/errors\u controller.rb
class ApplicationController < ActionController::Base
before_filter :set_locale
def set_locale
I18n.locale = params[:locale] || I18n.default_locale
end
end
class ErrorsController < ApplicationController
def show
render status_code.to_s, :status => status_code, :layout => false
end
protected
def status_code
params[:code] || 500
end
end
类错误控制器状态代码,布局=>错误
结束
受保护的
def状态代码
参数[:代码]| | 500
结束
结束
****************这是我的问题。*************************当我以如下模式键入错误链接时,
它可以显示自定义错误页面 但是,当我键入错误的链接时,例如
(没有区域设置(实际上应该是默认的区域设置:en)),
这将不会转到我的自定义错误页。
相反,它会给我一个错误页面,如下所示:
如何修复此问题,以便在使用默认区域设置(即链接中没有区域设置)时,可以使用自定义错误页面?谢谢大家! 尝试将所有不匹配的路由重定向到默认区域设置,如下所示:
scope "(:locale)", :locale => /en|zh-CN|zh-HK/ do
root 'home#index'
get '/' => 'home#index', as: :home_page
get 'about' => 'home#about', as: :about_page
end
%w( 404 422 500 ).each do |code|
get code, :to => "errors#show", :code => code
end
root to: redirect("/en", status: 302), as: :redirected_root
get "/*path",
to: redirect("/en/%{path}", status: 302),
constraints: {path: /(?!(en|zh-CN|zh-HK)})\/).*/},
format: false
你找到答案了吗?