Rails 3 rescue_from, with and working with custom modules
I am writing a Rails app that I am wanting to DRY up just a tad bit and instead of calling my custom error class at the top of each controller I need it in, I placed it inside of a Module and just included that module.
Working code (Module):
module ApiException
class EmptyParameter < StandardError
end
end
Working code (Controller):
# 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>M开发者_运维问答alformed URL. Exception: #{e.message}</error>"
end
def active_record_error(e)
render :xml => "<error>No records found. Exception: #{e.message}</error>"
end
Here is my question, using the :with
command, how would I call a method inside my custom module?
Something like this: rescue_from EmptyParameter, :with => :EmptParameter.custom_class
You could try something like this:
rescue_from EmptyParameter do |exception|
EmptyParameter.custom_class_method
end
精彩评论