Cannot include module in model

前端 未结 3 958
清酒与你
清酒与你 2020-12-28 20:13

I\'m using

Ruby version              1.8.7
Rails version             3.0.3

I have a method called alive in every model of my rails app:

3条回答
  •  一向
    一向 (楼主)
    2020-12-28 20:28

    Just put this line in application.rb file

    config.autoload_paths += Dir["#{config.root}/lib/**/"]
    

    Edited:

    This line is working fine for me. I want to suggest one more thing, ruby 1.8.x is not compatible with rails 3.x. So just update your ruby for version 1.9.2

    Following is my POC

    In lib folder:
    lib/test_lib.rb
    
    module TestLib
     def print_sm
       puts "Hello World in Lib Directory"
     end
    end
    
    
    In model file:
    
    include TestLib
    def test_method
      print_sm
    end
    
    And In application.rb
    
    config.autoload_paths += Dir["#{config.root}/lib/**/"]
    
    
    Now you can call test_method like this in controller:
    
    ModelName.new.test_method #####Hello World in Lib Directory
    
    

提交回复
热议问题