预计定义.在模块内调用类时 [英] Expected to define. When calling class inside a module
问题描述
我刚接触 Rails.我在 lib 目录中有一个设置,如下所示:
I new to rails. I have a setup in the lib directory like so:
lib/
blog/
core/
search/
base.rb
base.rb 也定义了 Base 类:
The base.rb defines the Base class as well:
module Blog
module Core
module Search
class Base
attr_accessor :properties
def initialize(params)
@properties = {}
end
end
end
end
end
我的 application.rb 中有以下代码
I have the following code in my application.rb
config.autoload_paths += Dir["#{config.root}/lib/**/"]
当我将它包含在帖子控制器中时,出现以下错误:
When I include it in posts controller I get following errors:
LoadError in PostsController#index
Expected /home/usr/code/blog/lib/blog/core/search/base.rb to define Base
有什么想法吗?我正在使用带有 RVM 的 rails 3.2.5.谢谢你的每一个建议.
Any idea? I'm using rails 3.2.5 with RVM. Thank you for every advice.
更新:添加了我的完整堆栈:
Started GET "/admin/posts" for 127.0.0.1 at 2012-06-08 21:06:18 +0800
LoadError (Expected /home/usr/code/blog/lib/blog/core/search/base.rb to define Base):
app/controllers/admin/base_controller.rb:5:in `<top (required)>'
app/controllers/admin/posts_controller.rb:6:in `<top (required)>'
Rendered /home/usr/.rvm/gems/ruby-1.9.3-p194@rails-3.2.5/gems/actionpack-3.2.5/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.9ms)
Rendered /home/usr/.rvm/gems/ruby-1.9.3-p194@rails-3.2.5/gems/actionpack-3.2.5/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.6ms)
Rendered /home/usr/.rvm/gems/ruby-1.9.3-p194@rails-3.2.5/gems/actionpack-3.2.5/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (7.0ms)
推荐答案
我遇到了同样的问题.这是因为您尝试使用 /lib/** 直接在 application.rb 中加载 /lib/blog/core/search/base.rb/
I had the same problem. It comes from the fact that you try to load /lib/blog/core/search/base.rb directly in application.rb with /lib/**/
我遇到的错误:
Expected /[...]/myapp/lib/durative/base.rb to define Base (LoadError)
目录结构:
lib/
--durative/
--base.rb
base.rb:
module Durative
class Base
def initialize(config)
@config = {}
end
#...
end
end
application.rb:
config.autoload_paths += Dir["#{config.root}/lib/**/"]
这是我为使其工作所做的更改
目录结构:
lib/
--durative.rb **(added)**
--durative/
--base.rb
durative.rb:
require 'durative/base'
base.rb(无变化)
application.rb(已更改):
config.autoload_paths += Dir["#{config.root}/lib/"]
告诉我们它是否也适合您.
Tell us if it worked for you too.
这篇关于预计定义.在模块内调用类时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!