Auto-loading lib files in Rails 4

前端 未结 4 1760
陌清茗
陌清茗 2020-11-22 16:20

I use the following line in an initializer to autoload code in my /lib directory during development:

config/initializers/custom.rb:

4条回答
  •  眼角桃花
    2020-11-22 16:29

    This might help someone like me that finds this answer when searching for solutions to how Rails handles the class loading ... I found that I had to define a module whose name matched my filename appropriately, rather than just defining a class:

    In file lib/development_mail_interceptor.rb (Yes, I'm using code from a Railscast :))

    module DevelopmentMailInterceptor
      class DevelopmentMailInterceptor
        def self.delivering_email(message)
          message.subject = "intercepted for: #{message.to} #{message.subject}"
          message.to = "myemail@mydomain.org"
        end
      end
    end
    

    works, but it doesn't load if I hadn't put the class inside a module.

提交回复
热议问题