rizidoro
rizidoro

Reputation: 13448

Rails "is not a module" error

I built a library on "lib" rails directory. The structure of library is something like this:

 lib/insurance/broker/fake_broker.rb

the class looks like the following example:

module Insurance
  module Broker
    class FakeBroker
      def initialize(user_id, user_secret)
        @user_id = user_id
        @user_secret = user_secret
      end
    end
  end
end

So, in my result_controller I'm doing this:

require 'insurance/broker/fake_broker'

 def show
   broker = Insurance::Broker::FakeBroker.new(1234,1234)
 end

but Rails is returning this error:

Insurance is not a module

What's wrong here?

Upvotes: 31

Views: 21794

Answers (2)

Sahil Verma
Sahil Verma

Reputation: 1

One of the possibilities can be for such error that in directory level, a class also exists with the same name, therefore it will be referring to that class. Please check the directory.

Upvotes: 0

Daniel Rikowski
Daniel Rikowski

Reputation: 72544

Ruby is telling you that it found an Insurance, but it is not a module. Perhaps you already have defined an Insurance class?

Depending on the surrounding code it might help if you "reset" the namespace by prepending a double colon:

broker = ::Insurance::Broker::FakeBroker.new(1234,1234)

Upvotes: 62

Related Questions