Tag: 循环依赖

Rails 4控制器中的运行时错误:自动加载常量时检测到循环依赖性

如果我遗漏了任何东西,请告诉我。 我无法弄清楚为什么我的views / references /文件夹无法访问。 new.html.erb和index.html.erb都不可用。 当我去localhost:3000/references我的错误是: RuntimeError in ReferencesController#index Circular dependency detected while autoloading constant ReferencesController 我相信这是设置,它不应该是一个Rails问题,因为我的其他控制器工作正常。 我的路径文件有resources :references ,我的rake路由产生: references GET /references(.:format) references#index POST /references(.:format) references#create new_reference GET /references/new(.:format) references#new edit_reference GET /references/:id/edit(.:format) references#edit reference GET /references/:id(.:format) references#show PATCH /references/:id(.:format) references#update PUT /references/:id(.:format) references#update DELETE /references/:id(.:format) references#destroy 所以我试着通过这个到达我的索引页面,这应该是正确的路径。 我的模特: class Reference < ActiveRecord::Base […]

Ruby中的循环依赖关系

假设我们有两个类,Foo和Foo Sub,每个类分别位于不同的文件foo.rb和foo_sub.rb中。 foo.rb: require “foo_sub” class Foo def foo FooSub.SOME_CONSTANT end end foo_sub.rb: require “foo” class FooSub < Foo SOME_CONSTANT = 1 end 由于循环依赖性,这不起作用 – 我们不能定义任何一个没有另一个的类。 我见过各种各样的解决方案。 其中两个我想避免 – 即将它们放在同一个文件中并删除循环依赖。 所以,我发现的唯一其他解决方案是前向声明: foo.rb: class Foo end require “foo_sub” class Foo def foo FooSub.SOME_CONSTANT end end foo_sub.rb require “foo” class FooSub < Foo SOME_CONSTANT = 1 end […]