首先:我在谷歌上搜索并使用了这里的搜索,找到了相同错误但在不同设置上的答案.也许我打破了某种不同;)
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.
更多推荐
加载常量时自动加载常量时检测到循环依赖
发布评论