简体   繁体   English

如何在Ruby on Rails的lib文件夹中创建模块?

[英]How to create a module in lib folder in Ruby on Rails?

I want to create a module in /lib folder & access that form my controllers. 我想在/lib文件夹中创建一个模块并访问构成我的控制器的模块。 There are several solutions available in different websites, but modules are not loading. 在不同的网站上有几种解决方案可用,但是模块未加载。

You need to add: 您需要添加:

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

to your config/application.rb 到您的config/application.rb

And then: 接着:

require 'file_name'

in your controller. 在您的控制器中。 That works for me (Rails 4). 这对我有用(路轨4)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在lib文件夹中生成controller / models / views? (Ruby on Rails) - How to generate controller/models/views inside lib folder? (Ruby on Rails) Ruby on Rails,如何为lib文件夹设置热插拔? - Ruby on Rails, how to setup hot swap for lib folder? (ruby on rails)如何覆盖放置在lib /中的模块方法 - (ruby on rails) how to override module method placed in lib/ 如何在sidekiq worker类内的lib文件夹中包含rails模块 - How to include rails module in lib folder inside sidekiq worker class Ruby Rails Lib文件夹命名约定 - Ruby Rails Lib Folder Naming Convention 访问lib目录中的模块(Ruby on rails) - Accessing module in lib directory (Ruby on rails) 如何从我的Rails 3应用程序的/ lib /文件夹中的ruby脚本中访问我的某个模型? - How do I access one of my models from within a ruby script in the /lib/ folder in my Rails 3 app? 在ruby on rails中使用rubypython gem,如何从lib文件夹调用python脚本? - Using the rubypython gem in ruby on rails, how do you call a python script from the lib folder? 如何使lib模块方法在项目范围内可用,并且还特定于Ruby on Rails中的类? - How do I make a lib module method available project-wide and also specific to a class in Ruby on Rails? 将ruby rails模型类转换为lib文件夹中的类 - convert a ruby rails model class into a class in the lib folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM