5 error-handling web-applications ruby-on-rails http-headers
这是我在这里的第一篇文章,所以我希望我将这个问题发布到正确的位置.否则,请让我知道,以便我知道下次我在这里发帖:)
我正在开发一个RoR网站,并希望单独处理服务器错误(400,404,500等).此外,由于网站是动态的,我想在rails环境中而不是在服务器级别处理错误.我想要做的一个例子是,当她碰到一个不会加载或根本不存在的页面或模板时,向用户显示可选材料或搜索栏.
所以,我做了一些阅读,我认为使用rescue_from异常处理程序是我的理由.(如果你们中有人有不同的意见,我们将非常高兴听到).
我有一个简单的工作原型(见下面的代码)启动并运行,但是,当我在代码中包含以下异常处理程序时出现错误:
rescue_from ActionController::MissingTemplate, :with => :not_found #404
Run Code Online (Sandbox Code Playgroud)
现在,我看不到我有拼写错误,我在网上发布的代码中看到了这一行.但是,当我包含它时,我收到以下路由错误:
Routing Error No route matches "/errorhandle" with {:method=>:get}
Run Code Online (Sandbox Code Playgroud)
我正在使用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
Run Code Online (Sandbox Code Playgroud)
快速浏览一下: http://www.ruby-forum.com/topic/47898
http://henrik.nyh.se/2008/09/404-invalid-rails-format
特别是第一个链接中的帖子:
您不能使用常规的“rescue”关键字来挽救 MissingTemplate 异常。
请改用rescue_action,例如:
def rescue_action(exception)
if ::ActionController::MissingTemplate === exception
render :text => 'rescued'
else
super
end
end
Run Code Online (Sandbox Code Playgroud)
肯特。
| 归档时间: |
|
| 查看次数: |
3233 次 |
| 最近记录: |