I am attempting to access a function in a module that is located in the lib directory of my app. (lib/search.rb)
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
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)
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/**/"]
For calling a module method directly include it in a class and then call it on class instance.
Class call_module_method
include Search
end
Now
call_module_method.new.zip_code_perimeter_search(zip, radius)
will evaluate the code inside method zip_code_perimeter_search(zip, radius)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.