Ruby on rails 3 Rails 3 rescue_来自、使用和使用自定义模块
我正在编写一个Rails应用程序,我只想稍微干涸一下,我没有在需要它的每个控制器的顶部调用我的自定义错误类,而是将它放在一个模块中,并将其包含在该模块中 工作代码(模块):Ruby on rails 3 Rails 3 rescue_来自、使用和使用自定义模块,ruby-on-rails-3,Ruby On Rails 3,我正在编写一个Rails应用程序,我只想稍微干涸一下,我没有在需要它的每个控制器的顶部调用我的自定义错误类,而是将它放在一个模块中,并将其包含在该模块中 工作代码(模块): 模块异常 类EmptyParameter:param\u错误 #未找到带有自定义XML响应的重新压缩记录 从ActiveRecord解救\u::RecordNotFound,:with=>:active\u记录\u错误 def参数错误(e) render:xml=>“格式错误的URL。异常:#{e.message}” 结束
模块异常
类EmptyParameter<标准错误
结束
结束
工作代码(控制器):
#包括自定义错误异常类
包括ApiException
从EmptyParameter中解救\u,:with=>:param\u错误
#未找到带有自定义XML响应的重新压缩记录
从ActiveRecord解救\u::RecordNotFound,:with=>:active\u记录\u错误
def参数错误(e)
render:xml=>“格式错误的URL。异常:#{e.message}”
结束
def活动记录错误(e)
render:xml=>“未找到任何记录。异常:#{e.message}”
结束
下面是我的问题,使用:使用命令,我将如何在自定义模块内调用方法
类似于这样的内容:rescue\u from EmptyParameter,:with=>:EmptParameter.custom\u class
您可以尝试以下内容:
rescue_from EmptyParameter do |exception|
EmptyParameter.custom_class_method
end
仅供参考,Rails 3中的rescue_仍然不起作用。我自己也在尝试做类似的事情:
# 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
rescue_from EmptyParameter do |exception|
EmptyParameter.custom_class_method
end