我有一个Rails应用,它使用一个名为:Competitions的引擎。当我启动Rails服务器并第一次加载页面时,它显示:LoadError: Unable to autoload constant Tournament, expected /home/stein/RubyProjects/my_app/engines/competitions/app/models/competitions/tournament.rb to define it
Rails似乎不知道文件中有一个Competitions::Tournament类,它只查找Tournament。如何让Rails搜索Competitions::Tournament而不是Tournament
Ruby版本: 2.1.1
Rails版本: 4.1.0
操作系统: Ubuntu 14.04 LTS
发布于 2014-05-10 03:19:26
我使用的是相同版本的Ruby/Rails,而且我还有一个带有自动加载模型的引擎。您是否在定义Engine Gem的模块中定义了您的模型?即:
假设你的引擎叫做竞赛:
module Competitions
class Tournament < ActiveRecord::Base
end
end您还可以将模块内联放置:
class Competitions::Tournament < ActiveRecord::Base
end这是因为Rails在您的文件系统的层次结构中查找模块/类,所以如果您在app/models/competitions/another_namespace/tournament.rb中有一个类,那么您必须将您的模型包装在由目录competitions和another_namespace命名为Competitions::AnotherNamespace::Tournament的模块中。
尝试一下,然后重启你的虚拟或主机应用程序。
https://stackoverflow.com/questions/23570059
复制相似问题