ruby-on-rails - rails : TypeError: wrong argument type Class (expected Module)

标签 ruby-on-rails activesupport-concern

在 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:错误的参数类型类(预期的模块))。
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'

最佳答案

这不是 OPs 问题的正确答案,但我认为它可以帮助人们在搜索错误消息时找到这个问题。
对我来说,问题是我有一个这样定义的助手:

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

关于ruby-on-rails - rails : TypeError: wrong argument type Class (expected Module),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22823804/

相关文章:

mysql - 基于Rails Active Record中两列的组合进行查询

ruby-on-rails - 在rails中测试数据库种子文件的有效性

ruby-on-rails - Rails 关注如何从 InstanceMethod 调用 ClassMethods

ruby-on-rails - 如何使用 ActiveSupport::Concern 继承类方法

css - 如何根据rails中的用户设置动态更改tailwind-config.js

jquery - 将 jQuery Datepicker 集成到 Rails 中

ruby-on-rails - 如何使用 Ubuntu 启动延迟作业?

ruby - 在模块前添加 ActiveSupport::Concern ? ruby 2+

ruby-on-rails - 自动加载常量 Concerns::<NameOfConcern> 时检测到循环依赖