Pet*_*ich 8 routing ruby-on-rails
按照这篇文章:
http://blog.plataformatec.com.br/2012/01/my-five-favorite-hidden-features-in-rails-3-2/
处理错误的最新方法如下所示:
# application.rb:
config.exceptions_app = self.routes
#routes.rb
match "/404", to: "site#not_found"
Run Code Online (Sandbox Code Playgroud)
但是,他没有说明rails错误应用程序还处理500个错误,422个错误(以及可能还有其他错误传递给这两个页面的事实?)
所以我一起攻击了一个看起来像这样的解决方案:
# routes.rb
rack_error_handler = ActionDispatch::PublicExceptions.new('public/')
match "/422" => rack_error_handler
match "/500" => rack_error_handler
Run Code Online (Sandbox Code Playgroud)
它的好处在于它使我的500页轻量级.
我还应该抓住其他错误吗?我的理解是,虽然500页现在将使用两个机架应用程序,但它仍然可以安全地与主Rails应用程序隔离.这很强吗?
谢谢!
我在应用程序控制器中添加了救援
if Rails.env.production?
rescue_from ActiveRecord::RecordNotFound, :with => :render_not_found
rescue_from ActionController::RoutingError, :with => :render_not_found
rescue_from ActionController::UnknownController, :with => :render_not_found
rescue_from ActionController::UnknownAction, :with => :render_not_found
rescue_from ActionView::MissingTemplate, :with => :render_not_found
end
def render_not_found(exception)
logger.info("render_not_found: #{exception.inspect}")
redirect_to root_path, :notice => 'The page was not found.'
end
Run Code Online (Sandbox Code Playgroud)
然后添加一个errors_controller来挽救路由错误,并将其添加到我的路由文件的底部
match "*path", :to => "errors#routing_error"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2279 次 |
| 最近记录: |