Ruby-on-rails 加载常量时自动加载常量时检测到循环依赖

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25460756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:52:26  来源:igfitidea点击:

Circular dependency detected while autoloading constant when loading constant

ruby-on-railsrubyruby-on-rails-4

提问by tomr

First of all: I googled and used the search here and found answers to the same error but on different setups. Maybe I broke sth different ;)

首先:我在谷歌上搜索并使用了这里的搜索,找到了相同错误但在不同设置上的答案。也许我打破了某种不同;)

Error:

错误:

RuntimeError at /admin/users
Circular dependency detected while autoloading constant Admin::UsersController

The structure is:

结构是:

App => controllers => admin => users_controller.rb

Routes:

路线:

namespace :admin do
  resources :stuff
  resources :users
end

Users controller:

用户控制器:

class UsersController < Admin::BaseController
  def new
    #code
  end

  def create
    #code
  end

  def index
    #code
  end

  private

  def user_params
   #code
  end
end

Admin base controller

管理基础控制器

class Admin::BaseController < ApplicationController
  layout 'admin'
  before_filter :require_login
end

Using: Rails 4.1.4, Ruby 2.1.2 What did I do wrong here?

使用:Rails 4.1.4、Ruby 2.1.2 我在这里做错了什么?

Thanks for your help!

谢谢你的帮助!

Edit:

编辑:

development.rb

发展.rb

Rails.application.configure do
 config.cache_classes = false
 config.eager_load = false
 config.consider_all_requests_local       = true
 config.action_controller.perform_caching = false
 config.action_mailer.raise_delivery_errors = false
 config.active_support.deprecation = :log
 config.active_record.migration_error = :page_load
 config.assets.debug = true
 config.assets.raise_runtime_errors = true
end

回答by pdobb

It looks like the primary issue may just be that you haven't namespaced your UsersController under the Adminnamespace, here:

看起来主要问题可能只是您没有在命名空间下为您的 UsersControllerAdmin命名,这里:

class UsersController < Admin::BaseController

Simple fix:

简单的修复:

class Admin::UsersController < Admin::BaseController

However, I suggest that you also break out your namespaces out into distinct parts to save future headache. So instead of the above, do this:

但是,我建议您也将命名空间分解为不同的部分,以节省将来的麻烦。因此,不要执行上述操作,而是执行以下操作:

# app/controllers/admin/users_controller.rb
module Admin
  class UsersController < Admin::BaseController
    # ...
  end
end

And do the same with all other namespaced controllers, such as:

并对所有其他命名空间控制器执行相同操作,例如:

# app/controllers/admin/base_controller.rb
module Admin
  class BaseController < ApplicationController
    # ...
  end
end

This way, as Rails is loading and autoloading and so forth it will always be sure to define the Admin module before attempting to load the classes under it. Sometimes you get unknown constant errors otherwise. The reasoning is a bit complex, but if you'd like to have a look check out this post.

这样,当 Rails 加载和自动加载等等时,它总是确保在尝试加载其下的类之前定义 Admin 模块。否则,有时您会遇到未知的常量错误。推理有点复杂,但如果你想看看这篇文章

UPDATE

更新

On Rails Edge, there is now an official Guide on the topic of Auto Loading of Constants.

在 Rails Edge 上,现在有一个关于 Auto Loading of Constants 主题的官方指南