【问题标题】:undefined method `boo' for Static:Class静态:类的未定义方法“boo”
【发布时间】:2012-08-08 20:43:07
【问题描述】:

我在lib目录中有一个类:

lib/static.rb

在我拥有的里面:

class Static
  def self.boo
    return 'what'
  end
end

我有一个控制器:

class PensionsController < ApplicationController
  layout 'page'
  def guides
    render :text => Static.boo
  end
end

我收到了这个错误:

undefined method `boo' for Static:Class

有什么想法我在这里做错了吗?

【问题讨论】:

    标签: ruby-on-rails ruby methods static-methods


    【解决方案1】:

    您使用的是 Rails 2 还是 3?在 Rails 3 中,lib 目录中的类默认不加载。

    我在 config/application.rb 的底部有这一行来启用该行为:

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

    【讨论】:

    • Rails 3,我尝试检测该类是否存在并返回 true。不过,这确实解决了问题。
    • 嗯...我刚刚尝试了类似的代码,它对我有用。也许有一些名为静态的冲突类?我现在能想到的就这些了
    • Controller 顶部是否有require 'static'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-17
    • 1970-01-01
    相关资源
    最近更新 更多