嵌套资源错误:自动加载常量时检测到循环依赖性

时间:2014-02-12 21:55:04

标签: ruby-on-rails ruby ruby-on-rails-4 resources nested

我正在尝试在Rails 4中使用嵌套资源,但收到以下错误:

RuntimeError (Circular dependency detected while autoloading constant Client::Website::ClientWebsitesController)

所以我有一个由设计创建的客户端模型,我有一个网站模型。这种关系是一对多的。

数据库:

    create_table(:clients) do |t|
  ## Database authenticatable
  t.string :email,              :null => false, :default => ""
  t.string :encrypted_password, :null => false, :default => ""

  ## Recoverable
  t.string   :reset_password_token
  t.datetime :reset_password_sent_at

  ## Rememberable
  t.datetime :remember_created_at

  ## Trackable
  t.integer  :sign_in_count, :default => 0, :null => false
  t.datetime :current_sign_in_at
  t.datetime :last_sign_in_at
  t.string   :current_sign_in_ip
  t.string   :last_sign_in_ip

  ## Confirmable
  # t.string   :confirmation_token
  # t.datetime :confirmed_at
  # t.datetime :confirmation_sent_at
  # t.string   :unconfirmed_email # Only if using reconfirmable

  ## Lockable
  # t.integer  :failed_attempts, :default => 0, :null => false # Only if lock strategy is :failed_attempts
  # t.string   :unlock_token # Only if unlock strategy is :email or :both
  # t.datetime :locked_at


  t.timestamps
end

    create_table :websites do |t|
  t.string :host
  t.string :name
  t.text :description
  t.text :code #integration code, that field will be filled after the website is created
  t.integer :client_id

  t.timestamps
end

型号:

class Client < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_many :websites
  has_many :partner_profits
  belongs_to :subscription_plan
end

class Website < ActiveRecord::Base
    belongs_to :client
    has_many :questions
    has_one :popup_skin
end

路线:

  devise_for :admins
  devise_for :partners
  devise_for :clients

    resources :clients do
        resources :websites, controller: 'client/website/client_websites'
    end

  root to: 'frontend#index'

我以前从未使用嵌套资源,任何想法我在这里缺少什么?试图解决问题已经第二天了:(

提前致谢! ;)

1 个答案:

答案 0 :(得分:0)

可能发生的一件事(虽然我在这里看不到)是你可能直接在他们的超类中引用类。

我遇到了这个问题,并修复了这个问题:

class SDMObject < ActiveRecord::Base
  # simplified for example
  LOCAL_CLASSES = [Tenant] # WRONG! Causes circular dependency
  LOCAL_CLASSES = ['Tenant'] # OK! Just evaluate to the class when needed
end

class Tenant < SDMObject
end