【问题标题】:Have I namespaced my controllers properly? Getting a Circular Dependency Error我是否正确命名了我的控制器?得到一个循环依赖错误
【发布时间】:2014-07-23 04:31:46
【问题描述】:

您好,我遇到了这个错误

自动加载常量时检测到循环依赖 子域::Settings::ThemesController

每当我尝试访问所述控制器时,在我的 Rails 4.0 应用程序中。事实上,对于与主题控制器命名空间的任何其他控制器,我都会遇到类似的错误。

我在设置下有以下控制器的名称空间,这些控制器本身在子域下的名称空间。

这些控制器的定义是否正确?谁能发现为什么会出现这种循环依赖错误?

# app/controllers/subdomain/settings/security_controller.rb
module Subdomain
  class Settings::SecurityController < BaseController  
    def edit
      ...
    end
  end
end

# app/controllers/subdomain/settings/themes_controller.rb
module Subdomain
  class Settings::ThemesController < BaseController
    def edit
       ...
    end
  end
end

# app/controllers/subdomain/settings/profiles_controller.rb
module Subdomain
  class Settings::ProfilesController < BaseController
    def edit
      ...
    end
  end
end

# app/controllers/subdomain/base_controller.rb
class Subdomain::BaseController < ApplicationController 
   ...
end

以及以下路由配置

MyApp::Application.routes.draw do

  constraints(Constraints::SubdomainRequired) do 

    scope :module => :subdomain do 
      namespace 'settings' do
        root to: 'security#edit'
        resource :theme,    only: [:create, :edit, :update], :controller => 'themes'
        resource :profile,  only: [:edit, :update], :controller => 'profiles'
        resource :security,  only: [:edit, :update], :controller => 'security'
      end
    end

  end
end

【问题讨论】:

  • 您可以在您的操作中发布一些代码吗?
  • # app/controllers/subdomain/settings/settings_controller.rb ...不应该是profiles_controller.rb吗?
  • 对不起,文件名只是 stackoverflow 上的一个错字。代码库是正确的。生病在这里更新。稍后我会在旅途中添加更多代码

标签: ruby-on-rails


【解决方案1】:

解决方案是我需要像这样重写每个控制器

module Subdomain
  module Settings
    class ProfileController 
      ...

而不是

module Subdomain
  class Settings::ProfileController

【讨论】:

    猜你喜欢
    • 2016-08-20
    • 2023-03-11
    • 2021-10-17
    • 2011-04-26
    • 2016-04-06
    • 2016-05-16
    • 1970-01-01
    • 2020-05-25
    • 2016-02-05
    相关资源
    最近更新 更多