【问题标题】:Rails: TypeError: wrong argument type Class (expected Module)Rails:TypeError:错误的参数类型类(预期模块)
【发布时间】:2014-04-02 21:50:45
【问题描述】:

在 app/models/abc/xyz.rb 内

module Abc::Xyz
  extend ActiveSupport::Concern
end

在 app/models/abc.rb 中

class Abc < ActiveRecord::Base
  include Abc::Xyz 
end

当我尝试从 Abc.where(id: id) 获取数据时,有时它会起作用,有时它会返回此错误(TypeError: wrong argument type Class (expected Module))。

TypeError: wrong argument type Class (expected Module)
      app/models/abc.rb:2:in `include'
      app/models/abc.rb:2:in `<class:Abc>'
      app/models/abc.rb:1:in `<top (required)>'
      activesupport (3.2.17) lib/active_support/dependencies.rb:469:in `load'
      activesupport (3.2.17) lib/active_support/dependencies.rb:469:in `block in load_file'
      activesupport (3.2.17) lib/active_support/dependencies.rb:639:in `new_constants_in'
      activesupport (3.2.17) lib/active_support/dependencies.rb:468:in `load_file'
      activesupport (3.2.17) lib/active_support/dependencies.rb:353:in `require_or_load'
      activesupport (3.2.17) lib/active_support/dependencies.rb:502:in `load_missing_constant'
      activesupport (3.2.17) lib/active_support/dependencies.rb:192:in `block in const_missing'
      activesupport (3.2.17) lib/active_support/dependencies.rb:190:in `each'
      activesupport (3.2.17) lib/active_support/dependencies.rb:190:in `const_missing'
      activesupport (3.2.17) lib/active_support/inflector/methods.rb:230:in `block in constantize'
      activesupport (3.2.17) lib/active_support/inflector/methods.rb:229:in `each'
      activesupport (3.2.17) lib/active_support/inflector/methods.rb:229:in `constantize'
      activesupport (3.2.17) lib/active_support/core_ext/string/inflections.rb:54:in `constantize'

【问题讨论】:

  • 你能显示完整的堆栈跟踪吗?
  • 我想在错误堆栈中看到(TypeError: wrong argument type Class (expected Module)),这很重要
  • 我会尝试module Xyz。关注点应该在您的模型之间共享,将它们命名为现有类是没有意义的。
  • @depa 是的,Abc 作为 moduleclass 令人困惑.. 对我和 Rails 来说也是如此......
  • 如果你尝试做class X ; end class Y include X end ----你会得到同样的错误..

标签: ruby-on-rails activesupport-concern


【解决方案1】:

我认为你最好还是遵守 Rails 关于关注点的约定:

  1. 使用模块来定义您的关注点
  2. 将问题放在app/models/concerns

如果您想像 Abc::Xyz 一样命名您的关注点,请确保将其放置在正确的路径中:app/models/concerns/abc/xyz.rb

另外一点就是

module Abc::Xyz

假设模块Abc 已经被定义。如果没有,它将失败。所以也许你最好使用

class Abc
  module Xyz
    #...
  end
end 

请注意,我使用了class Abc,而不是您的代码所暗示的模块。您不能同时拥有同名的类和模块。这也可能是您偶尔出现错误的原因。请参阅Exploring Concerns 上的这篇精彩文章。

Rails 在(自动)加载常量时会尝试各种不同的东西,但最好还是遵守约定。由于常量的定义/加载/执行顺序,我见过很多相同的sn-ps代码在不同时间点或不同环境中失败的情况。

【讨论】:

  • 非常感谢您的详细解释。我没有模块 Abc。我想使用 Abc 作为命名空间。因为我的 xyz 模块在 abc 文件夹中。
  • @krunalshah 您是否按照建议将模块声明更改为“class Abc; Module Xyz ...`?因为这可以解决您的问题恕我直言。如果是:请接受答案或提供反馈。
【解决方案2】:

这不是 OPs 问题的正确答案,但我认为它可以帮助人们在搜索错误消息时找到这个问题。

对我来说,问题是我有一个这样定义的助手:

module Api
  class MyControllerHelper
  end
end

但是 Rails 期望 helper 是一个模块而不是一个类,所以它应该是这样的:

module Api
  module MyControllerHelper
  end
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-13
    • 2018-08-11
    • 2021-07-19
    • 2019-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多