Ruby-on-rails 如何从任何地方要求一些 lib 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17951037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to require some lib files from anywhere
提问by Zoz
I'll explain my situation.
我会说明我的情况。
Here is my file tree in my rails application :
这是我的 rails 应用程序中的文件树:
lib/my_module.rb
库/my_module.rb
require 'my_module/my_file'
module My_module
end
lib/my_module/my_file.rb
lib/my_module/my_file.rb
class Tweetag::Collector
(...)
end
I've made a ruby script that I've put in config/jobs/
我制作了一个 ruby 脚本,放在 config/jobs/
I really don't understand how I am supposed to require the file my_file.rb in this file.
我真的不明白我应该如何在这个文件中要求文件 my_file.rb 。
require '../../my_module/my_file.rb'
It gives me `require': cannot load such file
它给了我“要求”:无法加载这样的文件
Same error with just require 'my_module' which is what I do in my controllers...
同样的错误,只需要'my_module',这是我在我的控制器中所做的......
Someone here to explain to me ? Thanks a lot
有人在这里向我解释吗?非常感谢
回答by Vecchia Spugna
You can autoinclude everything under the lib folderand avoid these problems:
您可以自动包含 lib 下的所有内容folder并避免这些问题:
Type this your file in config/application.rb
输入您的文件 config/application.rb
config.autoload_paths += %W(#{config.root}/lib)
config.autoload_paths += Dir["#{config.root}/lib/**/"]
回答by Sachin Singh
If you want to require only a specific file then, do something relative to Rails root like this
如果你只想需要一个特定的文件,那么做一些与 Rails root 相关的事情,就像这样
for example: --
lib/plan.rb
module Plan
...some code...
end
and if you want to require it only in some model, say app/models/user.rb
如果您只想在某些模型中需要它,请说 app/models/user.rb
do in user model
在用户模型中做
require "#{Rails.root}/lib/plan"
class User < ActiveRecord::Base
include Plan
end
if you want it to be available everywhere
如果您希望它随处可用
one solution is given by @VecchiaSpugna
@VecchiaSpugna 给出了一种解决方案
or you can create a ruby file in config/initializers folder
或者您可以在 config/initializers 文件夹中创建一个 ruby 文件
and require all file over there one by one
并一一要求所有文件在那里
OR
或者
try this
require '../../my_module/my_file'
instead of
require '../../my_module/my_file.rb'
You don't need to specify extension for a file in require.
您不需要在 require 中为文件指定扩展名。
回答by knut
I think there are two solutions.
我认为有两种解决方案。
1) Add the lib path to the search path. In ruby:
1)将lib路径添加到搜索路径中。在红宝石中:
$:.unshift('../../my_module/lib')
Then you can require 'my_module.rb'
然后你可以 require 'my_module.rb'
I think Vecchia Spugna answeris the rails-version of my ruby-answer. (I'm not familiar with rails).
我认为Vecchia Spugna 的答案是我的 ruby-answer 的 rails 版本。(我不熟悉导轨)。
2) Another solution:
2)另一种解决方案:
In your lib/my_module.rbyou require my_file. This file is located relative to your my_module.rb? Then use require_relative:
在你的lib/my_module.rb你需要my_file. 此文件相对于您的my_module.rb? 然后使用require_relative:
require_relative './my_module/my_file'
回答by konyak
Similar to require_relative,
类似于 require_relative,
# inside lib/my_module.rb
require File.expand_path('./my_module/my_file', File.dirname(__FILE__))
This expands the path of current file directory and add the relative file path to be required.
这将扩展当前文件目录的路径并添加所需的相对文件路径。
回答by stephennelson
Just chiming in because it took me forever to figure this out because very little solutions worked.
只是插话,因为我花了很长时间才弄清楚这一点,因为很少有解决方案有效。
? I had to use plain old require. I put it in the config/application.rbfile.
? 我不得不使用普通的旧需求。我把它放在config/application.rb文件里。
patching_file_path = File.expand_path("./lib", Dir.pwd)
Dir[patching_file_path+'/*.rb'].each {|file| require file }
patching_file_path = File.expand_path("./lib", Dir.pwd)
Dir[patching_file_path+'/*.rb'].each {|file| require file }
? I also put a temporary puts "I'm Working!in the file I'm trying to require so I can check the console to see if it's actually loading.
? 我还在puts "I'm Working!我试图要求的文件中放置了一个临时文件,以便我可以检查控制台以查看它是否正在实际加载。
? Also, if you're using spring loader, before you start your console you should do bin/spring stopin your terminal before you start your rails console. Otherwise, it won't load new files.
? 此外,如果您使用的是 spring 加载程序,则在启动控制台之前,您应该bin/spring stop在启动 rails 控制台之前在终端中执行此操作。否则,它不会加载新文件。

