Ruby on Rails -HTTP错误处理 [英] Ruby on Rails -HTTP error handling

查看:137
本文介绍了Ruby on Rails -HTTP错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在这里的第一篇文章,所以我希望我把这个问题发布在正确的地方。否则,请让我知道,以便我知道下次发布在这里:)



我正在一个RoR网站上工作,并希望处理服务器错误(400, 404,500等)。此外,由于网站是动态的,我想处理rails环境中的错误,而不是在服务器级别。
我想做的一个例子可能是当用户使用可选的材料或者搜索栏,当她碰到不能加载或根本不存在的页面或模板时。



我是RoR的新手,所以请放弃我,以防我明白了。



所以,我做了一些阅读我认为使用rescue_from异常处理程序是我的方式。 (如果您有任何意见,会非常高兴)。



我有一个简单的工作原型(见下面的代码)开始运行,但是,当我在代码中包含以下异常处理程序时,我收到错误:

  rescue_from ActionController :: MissingTemplate,with with => :not_found#404 

现在,我看不到我有一个拼写错误,我已经看到这行代码贴在网上。但是,当我包含它,我得到以下路由错误:

 路由错误没有路由匹配/ errorhandle与{:方法=>:get} 

我在rails 2.3.5上工作,这样做吗?



我希望你能帮助我解决这个问题。



干杯!
/ Maja

  class ApplicationController< ActionController :: Base 

帮助:所有#包括所有的帮助者,所有的时间

protect_from_forgery#看到ActionController :: RequestForgery保护细节

#ActiveRecord异常
rescue_from ActiveRecord :: RecordNotFound,:with => :not_found#400

#ActiveResource exceptions
rescue_from ActiveResource :: ResourceNotFound,:with => :not_found#404

#ActionView例外
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 with => :not_found#405

rescue_from ActionController :: InvalidAuthenticityToken,:with => :not_found#405

rescue_from ActionController :: UnknownAction,:with => :not_found#501

#这个特殊的异常导致所有其他的失败....为什么?
#rescue_from ActionController :: MissingTemplate,:with => :not_found#404

protected
def not_found
render:text => 错误,:status => 404
end

#从您的日志中清理敏感参数
#filter_parameter_logging:password
end


解决方案

快速查看这些:
http://www.ruby-forum.com/topic/47898



http://henrik.nyh.se/2008/09/404-invalid-rails-格式



特别是第一个链接中的帖子:


您不能使用常规的rescue关键字来拯救MissingTemplate
异常。



请改用rescue_action,例如:




  def rescue_action(exception)
if :: ActionController :: MissingTemplate === exception
render:text => '拯救'
else
超级
结束
结束



< blockquote>

肯特。



This is my first post here, so I hope that I am posting this question the right place. Otherwise, please let me know so that I know for next time I post here :)

I am working on a RoR website and would like to handle server errors (400, 404, 500, etc.) individually. Also, since the website is dynamic I would like to handle the errors within the rails environment rather than at the server level. An example of what I would like to do could be to present the user with optional material or a search bar when she bumps into a page or template that will not load or simply does not exist.

I am new at RoR so please bare with me in case I am asking the obvious.

So, I did a bit of reading and I think that using the rescue_from exception handler is the way to go in my case. (Would be more than happy to hear if any of you have a different opinion).

I have a simple working prototype (see code below) up and running, however, I get an error when I include the following exception handler to the code:

rescue_from ActionController::MissingTemplate,          :with => :not_found #404

Now, I can't see that I have a spelling error and I have seen this line in code posted on the web. However, when I include it I get the following routing error:

Routing Error No route matches "/errorhandle" with {:method=>:get}

I am working on rails 2.3.5, perhaps that has got something to do with it?

I hope that you can help me shed some light on this issue.

Cheers! /Maja

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

解决方案

Take a quick look at these: http://www.ruby-forum.com/topic/47898

http://henrik.nyh.se/2008/09/404-invalid-rails-format

In particular, a post in the first link:

You can't use a regular 'rescue' keyword to rescue MissingTemplate exception.

Use rescue_action instead, for example:

def rescue_action(exception)
  if ::ActionController::MissingTemplate === exception
     render :text => 'rescued'
  else
     super
  end
end

Kent.

这篇关于Ruby on Rails -HTTP错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆