访问 lib 目录中的模块(Ruby on rails) [英] Accessing module in lib directory (Ruby on rails)
问题描述
我试图访问位于我的应用程序的 lib 目录中的模块中的函数.(lib/search.rb)
I am attempting to access a function in a module that is located in the lib directory of my app. (lib/search.rb)
我实际上是在尝试从以下位置进行邮政编码搜索:http://joshhuckabee.com/simple-zip-code-perimeter-search-rails
I am actually trying to get zip code searching working from: http://joshhuckabee.com/simple-zip-code-perimeter-search-rails
lib/search.rb
module Search
def zip_code_perimeter_search(zip, radius)
#code
end
end
我试图从 rails 控制台或我的控制器调用 zip_code_perimeter_search 函数,这两次我都得到了未定义的方法.有什么想法吗?
I am trying to call the zip_code_perimeter_search function from the rails console or from my controller, both times I get undefined method. Any ideas?
推荐答案
在您的控制台/控制器中:
In your console/controller:
include Search
zip_code_perimeter_search(zip, radius)
如果它没有在 Rails 3 中自动加载,在您的 config/application.rb 文件中,您可以这样做:
In case it doesn't auto-load in Rails 3, in your config/application.rb file, you can do this:
# Custom directories with classes and modules you want to be autoloadable.
config.autoload_paths += Dir["#{config.root}/lib/**/"]
这篇关于访问 lib 目录中的模块(Ruby on rails)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!