2010-12-20 53 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