gpt4 book ai didi

ruby-on-rails - Rails 4.0.0 应用程序中 ActiveSupport 的未定义方法 `all'

转载 作者:行者123 更新时间:2023-12-04 05:39:45 24 4
gpt4 key购买 nike

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

错误如下:

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

这是包含无方法调用的代码片段
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/那个错误弹出! (是的,我使用了嵌套路由)

所以我点击了 Rails 控制台来检查 Configuration.all 返回什么,瞧
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 View 中失败。我做错什么了吗?

另外,这里是 webrick 上的输出,以防有人寻找它
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 时,您实际上会得到哪个类取决于引用代码所在的位置。

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

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

# 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 的评论:

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

如果您尝试 Rails.logger.info(Configuration.name)在 Controller 内部,您可能会看到 ActiveSupport::Configurable::Configuration在日志中。

关于ruby-on-rails - Rails 4.0.0 应用程序中 ActiveSupport 的未定义方法 `all',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18117137/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com