我试图遵循以下关于在 Rails 中使用自定义函数的答案中的简短示例:
http://stackoverflow.com/questions/2879679/where-to-put-code-snippets-in-rails
在 lib/math.rb 中的 math.rb 中
module Math
class << self
def cube_it(num)
num*3
end
end
end
在 Rails 控制台中我尝试过
include Math
Math.cube_it(2)
但我收到错误:
NoMethodError: undefined method 'cube_it' for Math:module
检查 config/application.rb 的下一行
# Custom directories with classes and modules you want to be autoloadable.
config.autoload_paths += %W(#{config.root}/lib)
因此,如果您仍然有无法加载的扩展,您可以输入
require 'math'
并重新检查
您可以创建 config/initializers/lib.rb 而不是调用 require
with
Dir[File.join(Rails.root, "lib", "*.rb")].each {|l|需要 l }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)