加载常量时自动加载常量时检测到循环依赖 [英] Circular dependency detected while autoloading constant when loading constant

查看:48
本文介绍了加载常量时自动加载常量时检测到循环依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 ;)

错误:

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

结构为:

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

路线:

namespace :admin do
  resources :stuff
  resources :users
end

用户控制器:

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

  def create
    #code
  end

  def index
    #code
  end

  private

  def user_params
   #code
  end
end

管理员基础控制器

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

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

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

感谢您的帮助!

development.rb

development.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

推荐答案

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

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

class UsersController < Admin::BaseController

简单修复:

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

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

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 Edge 上,现在有关于自动加载常量主题的官方指南.

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

这篇关于加载常量时自动加载常量时检测到循环依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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