【发布时间】:2018-06-20 16:55:05
【问题描述】:
这是我在这里的第一篇文章,所以我希望我在正确的地方发布这个问题。否则,请让我知道,以便我下次在这里发帖时知道:)
我正在开发一个 RoR 网站,并希望单独处理服务器错误(400、404、500 等)。此外,由于网站是动态的,我想在 rails 环境而不是服务器级别处理错误。 我想做的一个例子是,当用户碰到一个不会加载或根本不存在的页面或模板时,向用户展示可选材料或搜索栏。
所以,我做了一些阅读,我认为使用 rescue_from 异常处理程序是我的方法。 (如果你们中的任何人有不同的意见,我们会很高兴听到)。
我有一个简单的工作原型(请参阅下面的代码)启动并运行,但是,当我在代码中包含以下异常处理程序时出现错误:
rescue_from ActionController::MissingTemplate, :with => :not_found #404
现在,我看不到我有拼写错误,而且我在网上发布的代码中看到了这一行。但是,当我包含它时,会出现以下路由错误:
Routing Error No route matches "/errorhandle" with {:method=>:get}
我正在开发 Rails 2.3.5,也许这与它有关?
我希望你能帮助我阐明这个问题。
干杯! /玛雅
class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time
protect_from_forgery #See ActionController::RequestForgeryProtection for details
#ActiveRecord exceptions
rescue_from ActiveRecord::RecordNotFound, :with => :not_found #400
#ActiveResource exceptions
rescue_from ActiveResource::ResourceNotFound, :with => :not_found #404
#ActionView exceptions
rescue_from ActionView::TemplateError, :with => :not_found #500
#ActionController exceptions
rescue_from ActionController::RoutingError, :with => :not_found #404
rescue_from ActionController::UnknownController, :with => :not_found #404
rescue_from ActionController::MethodNotAllowed, :with => :not_found #405
rescue_from ActionController::InvalidAuthenticityToken, :with => :not_found #405
rescue_from ActionController::UnknownAction, :with => :not_found #501
# This particular exception causes all the rest to fail.... why?
# rescue_from ActionController::MissingTemplate, :with => :not_found #404
protected
def not_found
render :text => "Error", :status => 404
end
# Scrub sensitive parameters from your log
# filter_parameter_logging :password
end
【问题讨论】:
-
你能告诉我们来自 routes.rb 的相关路线,如果有的话?
-
当然Trrevoke,感谢您的快速回复。我没有在 routes.rb 中添加任何东西,所以我想这几乎是标准。文件:ActionController::Routing::Routes.draw 做 |map| map.resources :errorhandlers # ... # 很多行被注释掉了 # ... map.connect ':controller/:action/:id' map.connect ':controller/:action/:id.:format' end
-
呃,不知道如何让评论看起来美观整洁uark。希望它无论如何都可以阅读......
-
编辑您的问题,让路线看起来整洁有序
-
Veger:现在似乎无法编辑,但我会在下一篇文章中研究如何编辑。干杯。
标签: ruby-on-rails web-applications error-handling http-headers