简体   繁体   中英

How to use module in lib from controller with Rails?

I created a module in my lib directory:

# lib/a/b/c.rb
 module A
   module B
     class C
       def say
         p 'Hi!'
       end
     end
   end
 end

Now I want to call C class from this module file.

# app/controllers/c_controller.rb
@data = A::B::C.new.say

And add this to config file:

# config/application.rb
config.autoload_paths += %W(#{config.root}/lib)

But it gave me an error:

NameError in CController#index
uninitialized constant CController::say

Was my source wrong?

I know the reason. It is necessary to add require in the first line of the controller:

require 'a/b/c'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM