ruby - Accessing gem methods in rails app -


i created own gem. basic gem 'hi' method in it. in lib directory of gem have 'mygem.rb' file has above method.

now have created simple rails app trying use method. how should use it?

i tried this:-

in app/controllers have file named hello_controller.rb looks this...

class hellocontroller < applicationcontroller      def index         @message= mygem.hi          @count=3         @bonus="this message came controller."     end end 

but on localhost giving me following error: nameerror in hellocontroller#index

uninitialized constant hellocontroller::mygem

please solve error..

you need add gem gemfile.

you can use this: gem 'mygem', path: 'path/to/gem'

it idea wrap gem code in class or module should be:

lib/mygem.rb

class mygem   def self.hi     'hi there!'   end end 

you call in controller mygem.hi


Comments