开发者

How do you include a class from /lib into your code in app/controllers

开发者 https://www.devze.com 2023-04-10 00:44 出处:网络
I have a class sitting in /lib folder. It\'s in a file called mailing.rb And I would like to use this c开发者_StackOverflow中文版lass in codes from app/controller.

I have a class sitting in /lib folder. It's in a file called mailing.rb

And I would like to use this c开发者_StackOverflow中文版lass in codes from app/controller.

How do i do this?


Rails 3 no longer automatically loads the files from lib.

In your application.rb file, you can add lib to your autoload_paths:

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

This way, your mailer.rb and all other files in lib will be available to the rest of your application.


I believe you need to add an initializer file with the require statement in it, for example if your lib file is /lib/some_module.rb you would need to create an initialiser file in /config/initializers/require_libs.rb...

# /config/initializers/require_libs.rb
require 'some_module'
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号