Rails 4.0.0 应用程序中 ActiveSupport 的未定义方法“all" [英] Undefined method `all' for ActiveSupport in Rails 4.0.0 app

查看:51
本文介绍了Rails 4.0.0 应用程序中 ActiveSupport 的未定义方法“all"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是开发 rails 应用程序的新手,所以在这里的大部分时间里,我只严重依赖 rails generate 命令来为整个应用程序制作脚手架.虽然我的应用程序的所有其他部分都可以正常工作,但在自动生成后,这个部分会立即引发错误.

I'm new to developing rails apps, so for the most part here, I've relied heavily only on the rails generate command to make scaffolds for the entire app. While every other part of my app works fine, this one raises an error right after being automatically generated.

错误如下:

NoMethodError in ConfigurationsController#index 
undefined method `all' for ActiveSupport::Configurable::Configuration:Class

这是包含无方法调用的代码片段

here is the code snippet that contains said no method call

def index
  @configurations = Configuration.all
end

这是有问题的模型

class Configuration < ActiveRecord::Base
  belongs_to :users, class_name: 'User', foreign_key: 'user_id'
end

我添加了belongs_to部分......无论如何,当我通过http://运行它时localhost.localhost:3000/users/1/configurations/ 弹出那个错误!(是的,我使用了嵌套路由)

I added the belongs_to part... anyway, when I ran it through http://localhost.localhost:3000/users/1/configurations/ that error pops up! (yes, I used nested routes)

所以我点击了 Rails 控制台来检查 Configuration.all 返回什么,瞧

So i hit the rails console to check what Configuration.all returns and lo and behold

2.0.0-p247 :004 > Configuration.all
  Configuration Load (0.3ms)  SELECT "configurations".* FROM "configurations"
 => #<ActiveRecord::Relation [#<Configuration id: 1, user_id: 1, port: 3000, host: "example.org", annoy: 5000, version: "1", machines: nil, created_at: "2013-08-08 02:15:32", updated_at: "2013-08-08 02:15:32">]>

我有点不明白为什么该方法在 rails 控制台中有效,然后在浏览器的 html 视图中失败.我做错了什么吗?

I'm kind of lost as to why that method works in rails console then fails in the html view on my browser. Did i do something wrong?

此外,这是 webrick 上的输出,以防有人查找

also, here is the output on webrick in case someone looks for it

Started GET "/users/1/configurations/" for 192.168.1.105 at 2013-08-08 10:24:59 +0800
Processing by ConfigurationsController#index as HTML
  Parameters: {"user_id"=>"1"}
Completed 500 Internal Server Error in 1ms

NoMethodError (undefined method `all' for ActiveSupport::Configurable::Configuration:Class):
  app/controllers/configurations_controller.rb:8:in `index'

推荐答案

不幸的是,您创建了一个类,其名称也是 Rails API 的一部分(尽管在不同的命名空间中).您的类恰好位于顶级命名空间,而 Rails 类嵌套在 ActiveSupport::Configurable 命名空间中.当您引用 Configuration 时,您实际获得的类取决于引用代码所在的位置.

Unfortunately, you created a class whose name is also part of the Rails API (though in a different namespace). Your class happens to be at the top-level namespace, while the Rails class is nested in the ActiveSupport::Configurable namespace. Which class you'll actually get when you reference Configuration depends on where the referencing code is located.

要指定您希望该类位于顶级命名空间,您可以将您的类称为 ::Configuration.

To specify that you want the class at top-level namespace, you can refer to your class as ::Configuration.

这是一个简单的测试来演示发生了什么:

Here's a simplified test to demonstrate what's happening:

# Let's say this module & class is defined by a library (e.g. Rails)
module Configurable  # analogous to ActiveSupport::Configurable
  class Configuration  # analogous to ActiveSupport::Configurable::Configuration
  end
end
class ControllerBase  # analogous to ActionController::Base
  # ActionController::Base includes ActiveSupport::Configurable.
  # You can confirm this in Rails 4 that this returns true:
  #   ActionController::Base.included_modules.include? ActiveSupport::Configurable
  include Configurable
end

# This is your class and constant
class Configuration
end

class MyController < ControllerBase
  # This is analogous to code inside your Controller.
  # Inheriting gives you access to the constants in the parent
  def wrong_class
    Configuration  # oops, we wanted the top-level
  end
  def correct_class
    ::Configuration
  end
end

# top-level scope
p(top_level_access: Configuration.name)  # 'Configuration'
p(nested_access: MyController.new.wrong_class.name)  # 'Configurable::Configuration'
p(scoped_nested_access: MyController.new.correct_class.name)  # 'Configuration'

编辑 - 回复@maru 的评论:

EDIT - Reply to @maru's comment:

在大多数情况下,您可以只使用 Configuration.name 来获取类的全命名空间名称.在控制台试试这个.

In most cases you can just use Configuration.name to get the fully-namespaced name of the class. Try this at the console.

如果您在控制器中尝试 Rails.logger.info(Configuration.name),您可能会在日志中看到 ActiveSupport::Configurable::Configuration.

If you try Rails.logger.info(Configuration.name) inside the controller, you'll probably see ActiveSupport::Configurable::Configuration in the log.

这篇关于Rails 4.0.0 应用程序中 ActiveSupport 的未定义方法“all"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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