“未初始化的常量"包含模块时出错 [英] "Uninitialized constant" error when including a module

查看:45
本文介绍了“未初始化的常量"包含模块时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试引用关联扩展,但出现以下错误:

I am trying to reference an association extension but it errors with:

NameError (uninitialized constant User::ListerExtension):
  app/models/user.rb:2:in `<class:User>'

这是我的实现:

class User < ActiveRecord::Base
  include ListerExtension

  has_and_belongs_to_many :roles, :uniq => true, :extend => Lister

lib/lister.rb

module ListerExtension
  def lister
    self.map(&:to_s).join(', ')
  end
end

我使用的是 Rails v3.1.3.

I am using Rails v3.1.3.

推荐答案

Andrew Marshall 对自动加载设置有一个很好的观点(有关更多信息,请参阅他链接的问题),而且:因为您将类命名为 ListerExtension,Rails 将查找名为 lister_extension.rb - not lister.rb 的文件.它很聪明,但没有那么聪明.

Andrew Marshall has an excellent point about the auto-load setup (see the question he links for more on that), but also: Because you named your class ListerExtension, Rails will be looking for a file named lister_extension.rb - not lister.rb. It's smart, but it's not that smart.

希望有帮助!

这篇关于“未初始化的常量"包含模块时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆