i am creating some custom exceptions as follows
lib/exceptions.rb
module Exceptions
c开发者_Python百科lass MemberOverFlow < StandardError
end
rescue_from MemberOverFlow do |exception|
redirect_to root_url, :alert => exception.message
end
end
I use to raise the exception like this.
raise Exception::MemberOverFlow"member count overflow"
It giving the following error
NoMethodError in MembersController#create
undefined method `MemberOverFlow' for Exception:Class
can anyone tell me what is problem
thanks
Did you require the module in the controller where you are trying to raise the exception?
require "exception" #or wherever you have placed the module file
Use:
raise Exception::MemberOverFlow.new("member count overflow")
and if it still does not work, try changing the name of the module "Exception" because Exception is an existing exception class defined in Ruby.
No one specifically called out what was wrong in the original post. The following:
raise Exceptions::MemberOverFlow"member count overflow"
is treating MemberOverFlow
as a METHOD and not the class
that it is. You need to call the new
method on your MemberOverFlow
class, which is what amit_saxena's answer points out.
That answer solves the problem, but I just felt it was worth pointing out what you were doing syntactically so that if you ever had a similar problem in the future you would know what was going on.
The contents of your lib directory are not automatically loaded in rails 3, you need to specify them like so in config/application.rb:
config.autoload_paths += %W(#{config.root}/lib)
Perhaps you have not done that?
精彩评论