2010-12-20 87 views
3

我正在写一个Rails应用程序,我只想干一点点,而不是调用我需要它在每个控制器的顶部我的自定义错误类,I将其放置在模块内部并包含该模块。Rails 3 rescue_from,并与自定义模块

工作代码(模块):

module ApiException 
    class EmptyParameter < StandardError 
    end 
end 

工作代码(控制器):

# include custom error exception classes 
    include ApiException 

    rescue_from EmptyParameter, :with => :param_error 

    # rescure record_not_found with a custom XML response 
    rescue_from ActiveRecord::RecordNotFound, :with => :active_record_error 

    def param_error(e) 
     render :xml => "<error>Malformed URL. Exception: #{e.message}</error>" 
    end 

    def active_record_error(e) 
     render :xml => "<error>No records found. Exception: #{e.message}</error>" 
    end 

这里是我的问题,使用:with命令,怎么会我叫我的自定义模块内部的方法?

事情是这样的:rescue_from EmptyParameter, :with => :EmptParameter.custom_class

+0

仅供参考,这种rescue_from仍然没有对Rails 3的工作尝试做同样的事情自己:https://开头的轨道。 lighthouseapp.com/projects/8994/tickets/4444-can-no-longer-rescue_from-actioncontrollerroutingerror – JohnMetta 2011-05-18 19:20:24

回答

2

你可以尝试这样的事:

rescue_from EmptyParameter do |exception| 
    EmptyParameter.custom_class_method 
end