我有以下文件夹结构:
- models/
- mappings/
- user.rb
- mapping.rb
- service.rb
- user.rb
- ...
在mappings / user.rb中,我有以下内容:
module Mappings
class User < Mapping
end
end
而mapping.rb是:
class Mapping < ActiveRecord::Base
end
在config / application.rb中,我按如下方式自动加载模型:
config.autoload_paths += %W(#{config.root}/lib #{config.root}/app/presenters)
很遗憾,我收到此load_missing_constant
错误:
/home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/dependencies.rb:481:in `load_missing_constant': Unable to autoload constant User, expected /vagrant/app/models/mappings/user.rb to define it (LoadError)
非常感谢任何帮助。我不确定我错过了什么!
更新:
完整追踪:
16:53:48 jobs.1 |从 /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/dependencies.rb:180:in
const_missing' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/inflector/methods.rb:238:in
const_get&#39; 16:53:48就业1从 /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/inflector/methods.rb:238:inblock in constantize' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/inflector/methods.rb:236:in
各&#39; 16:53:48就业1从 /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/inflector/methods.rb:236:ininject' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/inflector/methods.rb:236:in
constantize&#39; 16:53:48就业1从 /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/dependencies.rb:552:inget' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/dependencies.rb:583:in
constantize&#39; 16:53:48就业1从 /home/vagrant/.rvm/gems/ruby-2.1.1/gems/devise-3.4.1/lib/devise.rb:302:inget' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/devise-3.4.1/lib/devise/mapping.rb:80:in
,以&#39; 16:53:48就业1从 /home/vagrant/.rvm/gems/ruby-2.1.1/gems/devise-3.4.1/lib/devise/mapping.rb:75:inmodules' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/devise-3.4.1/lib/devise/mapping.rb:92:in
路由&#39; 16:53:48就业1从 /home/vagrant/.rvm/gems/ruby-2.1.1/gems/devise-3.4.1/lib/devise/mapping.rb:159:indefault_used_route' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/devise-3.4.1/lib/devise/mapping.rb:69:in
初始化&#39; 16:53:48就业1从 /home/vagrant/.rvm/gems/ruby-2.1.1/gems/devise-3.4.1/lib/devise.rb:336:innew' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/devise-3.4.1/lib/devise.rb:336:in
add_mapping&#39; 16:53:48就业1从 /home/vagrant/.rvm/gems/ruby-2.1.1/gems/devise-3.4.1/lib/devise/rails/routes.rb:224:inblock in devise_for' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/devise-3.4.1/lib/devise/rails/routes.rb:223:in
各&#39; 16:53:48就业1从 /home/vagrant/.rvm/gems/ruby-2.1.1/gems/devise-3.4.1/lib/devise/rails/routes.rb:223:indevise_for' 16:53:48 jobs.1 | from /vagrant/config/routes.rb:93:in
阻止&#39; 16:53:48就业1从 /home/vagrant/.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.9/lib/action_dispatch/routing/route_set.rb:344:ininstance_exec' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.9/lib/action_dispatch/routing/route_set.rb:344:in
eval_block&#39; 16:53:48就业1从 /home/vagrant/.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.9/lib/action_dispatch/routing/route_set.rb:322:indraw' 16:53:48 jobs.1 | from /vagrant/config/routes.rb:1:in
&#39; 16:53:48就业1从 /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/dependencies.rb:241:inload' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/dependencies.rb:241:in
阻止加载&#39; 16:53:48就业1从 /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/dependencies.rb:232:inload_dependency' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/dependencies.rb:241:in
load&#39; 16:53:48就业1从 /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/application/routes_reloader.rb:40:inblock in load_paths' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/application/routes_reloader.rb:40:in
每个&#39; 16:53:48就业1从 /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/application/routes_reloader.rb:40:inload_paths' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/application/routes_reloader.rb:16:in
重新加载!&#39; 16:53:48就业1从 /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/application/routes_reloader.rb:26:inblock in updater' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/file_update_checker.rb:75:in
来电&#39; 16:53:48就业1从 /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/file_update_checker.rb:75:inexecute' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/application/routes_reloader.rb:27:in
更新&#39; 16:53:48就业1从 /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/application/routes_reloader.rb:7:inexecute_if_updated' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/application/finisher.rb:71:in
阻止&#39; 16:53:48就业1从 /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/initializable.rb:30:ininstance_exec' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/initializable.rb:30:in
运行&#39; 16:53:48就业1从 /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/initializable.rb:55:in tsort_each中的block in run_initializers' 16:53:48 jobs.1 | from /home/vagrant/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/tsort.rb:226:in
阻止&#39; 16:53:48就业1从 /home/vagrant/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/tsort.rb:348:inblock (2 levels) in each_strongly_connected_component' 16:53:48 jobs.1 | from /home/vagrant/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/tsort.rb:427:in
each_strongly_connected_component_from&#39; 16:53:48就业1从 /home/vagrant/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/tsort.rb:347:inblock in each_strongly_connected_component' 16:53:48 jobs.1 | from /home/vagrant/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/tsort.rb:345:in
各&#39; 16:53:48就业1从 /home/vagrant/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/tsort.rb:345:incall' 16:53:48 jobs.1 | from /home/vagrant/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/tsort.rb:345:in
each_strongly_connected_component&#39; 16:53:48就业1从 /home/vagrant/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/tsort.rb:224:intsort_each' 16:53:48 jobs.1 | from /home/vagrant/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/tsort.rb:205:in
tsort_each&#39; 16:53:48就业1从 /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/initializable.rb:54:inrun_initializers' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/application.rb:300:in
初始化&#39!; 16:53:48就业1从 /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/railtie.rb:194:inpublic_send' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/railtie.rb:194:in
的method_missing&#39; 16:53:48就业1从 /vagrant/config/environment.rb:8:in<top (required)>' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/dependencies.rb:247:in
要求&#39; 16:53:48就业1从 /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/dependencies.rb:247:inblock in require' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/dependencies.rb:232:in
load_dependency&#39; 16:53:48就业1从 /home/vagrant/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.9/lib/active_support/dependencies.rb:247:inrequire' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/application.rb:276:in
require_environment!&#39; 16:53:48就业1从 /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/commands/runner.rb:52:in<top (required)>' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/commands/commands_tasks.rb:128:in
需要&#39; 16:53:48就业1从 /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/commands/commands_tasks.rb:128:inrequire_command!' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/commands/commands_tasks.rb:95:in
runner&#39; 16:53:48就业1从 /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/commands/commands_tasks.rb:40:inrun_command!' 16:53:48 jobs.1 | from /home/vagrant/.rvm/gems/ruby-2.1.1/gems/railties-4.1.9/lib/rails/commands.rb:17:in
&#39; 16:53:48就业1来自bin / rails:8:require' 16:53:48 jobs.1 | from bin/rails:8:in
&#39;
所以看起来问题是因为我必须使用名为User
的类,即使其中一个在模块内被命名空间。
答案 0 :(得分:0)
我相信它是因为您在User
模块中定义了Mapping
类。它查看了user.rb
,没有看到User
类和错误。尝试删除模块调用,看看是否有效。
编辑:为了回应评论,或许您应该在类名中明确表示。 Mappings::User
。您可能需要相应地继承它。