Ruby 未初始化的常量Model1::Model2::Model3
我正在获取未初始化的常量项目::论坛::主题Ruby 未初始化的常量Model1::Model2::Model3,ruby,ruby-on-rails-3,ruby-on-rails-3.2,Ruby,Ruby On Rails 3,Ruby On Rails 3.2,我正在获取未初始化的常量项目::论坛::主题 app/controllers/home_controller.rb:46:in `discussions' 我有下面的代码,我正在从rails 2.3.x转换到rails 3.2.11,我认为路由设置有问题 知道我该怎么修吗 型号 class Project < ActiveRecord::Base # Relations under project model has_many :features, :depend
app/controllers/home_controller.rb:46:in `discussions'
我有下面的代码,我正在从rails 2.3.x
转换到rails 3.2.11
,我认为路由设置有问题
知道我该怎么修吗
型号
class Project < ActiveRecord::Base
# Relations under project model
has_many :features, :dependent => :destroy
has_many :forums, :class_name=>'Forum::Forum'
has_many :topics, :class_name=>'Forum::Topic', :through=>:forums
class Forum::Forum < Feature
# Relations under forum model
has_many :topics, :class_name => 'Forum::Topic', :dependent => :destroy
class Feature < ActiveRecord::Base
# Relations under feature model
belongs_to :project
class Forum::Topic < ActiveRecord::Base
# Relations under topic model
belongs_to :forum, :foreign_key => :forum_id, :class_name => 'Forum::Forum', :include => :project
uninitialized constant Project::Forum::Topic
app/controllers/home_controller.rb:46:in `discussions'
routes.rb
def discussions
@project ||= Project.find_by_name 'help'
@forums = @project.forums
@topics = @project.topics.recent # HERE I AM GETTING ERRORS
end
scope :home, :controller => "home", :activity => 'read' do
get :discussions, :path => '/forums', :service_type => 'public'
get :forums, :action => "discussions"
end
错误
class Project < ActiveRecord::Base
# Relations under project model
has_many :features, :dependent => :destroy
has_many :forums, :class_name=>'Forum::Forum'
has_many :topics, :class_name=>'Forum::Topic', :through=>:forums
class Forum::Forum < Feature
# Relations under forum model
has_many :topics, :class_name => 'Forum::Topic', :dependent => :destroy
class Feature < ActiveRecord::Base
# Relations under feature model
belongs_to :project
class Forum::Topic < ActiveRecord::Base
# Relations under topic model
belongs_to :forum, :foreign_key => :forum_id, :class_name => 'Forum::Forum', :include => :project
uninitialized constant Project::Forum::Topic
app/controllers/home_controller.rb:46:in `discussions'
如果您自动加载该类(也就是说,您不需要它的源文件),那么原因可能是Ruby自动加载
的错误,无法自动加载嵌套的常量3次或更多次
目前我在互联网上找不到我的信息来源;但是,我记得它应该用Ruby 2.0来解决,所以你可以修复它,从自动加载中删除你需要的文件,在你需要的地方添加需要的“project/forum/topic”,或者升级到Ruby 2.0
在项目模型类中,按以下方式更改
旧:有很多:主题,:class\u name=>'Forum::Topic',:through=>:forums
新:有很多:主题,:class\u name=>'::Forum::Topic',:through=>:forums
它应该可以工作您可以包括这些类在哪些文件中定义吗?我注意到您还缺少类名称之前的类调用。代码真的是这样吗?@RyanBigg,更新了我的问题我没听清楚,我使用的是Rails 3.2。xRails不属于我的问题,它是Ruby问题;您如何调用定义项目
、论坛
和主题
的文件?它们所在的文件夹结构是什么?