首字母缩写词不会正确复数,导致循环依赖

时间:2014-03-14 15:57:36

标签: ruby-on-rails ruby-on-rails-4 circular-dependency

我想命名我的控制器ESCsControllerESC是有问题的首字母缩略词。我发现了rails变形文档,它们描述了实现这一目标的方法。

http://api.rubyonrails.org/classes/ActiveSupport/Inflector/Inflections.html

  

注意:将不再识别传递给复数的缩略语,因为缩写词不会作为复数结果中的分隔单位出现。要解决此问题,您还必须将复数形式指定为首字母缩略词:

acronym 'API'
camelize(pluralize('api')) #=> 'Apis'

acronym 'APIs'
camelize(pluralize('api')) #=> 'APIs'

我将控制器和模型添加到environment.rb

后创建了我的控制器和模型
ActiveSupport::Inflector.inflections { |i| 
   i.acronym 'ESC'
   i.acronym 'ESCs'
}

在控制台中进行测试,这些工作完美无缺。 'ESC'.pluralize()返回ESCs'ESCs'.singularize()按预期返回ESC

控制器和模型分别通过rails generate model ESCrails generate controller ESCs生成。这按预期创建了escs_controller.rb和模型esc.rb

在我的routes.rb中,我曾经有过

resources :ESCs, path: '/parts/escs'

返回了此错误:'ESCs' is not a supported controller name. This can lead to potential routing problems. See http://guides.rubyonrails.org/routing.html#specifying-a-controller-to-use

我最终将其更改为:

resources :ESCs, controller: 'escs', path: '/parts/escs'

但是,现在每当我尝试访问某个页面时,都会出现循环依赖性错误:

Circular dependency detected while autoloading constant EscsController

任何人都知道发生了什么事吗?看起来无论是什么尝试加载控制器都没有看到它应该是ESCsController而不是EscsController。我是rails的新手,所以这可能是一个简单的问题......

我正在使用Rails 4.0.2

这是完整堆栈跟踪的相关部分。

activesupport (4.0.2) lib/active_support/dependencies.rb:461:in `load_missing_constant'
activesupport (4.0.2) lib/active_support/dependencies.rb:184:in `const_missing'
activesupport (4.0.2) lib/active_support/inflector/methods.rb:226:in `const_get'
activesupport (4.0.2) lib/active_support/inflector/methods.rb:226:in `block in constantize'
activesupport (4.0.2) lib/active_support/inflector/methods.rb:224:in `each'
activesupport (4.0.2) lib/active_support/inflector/methods.rb:224:in `inject'
activesupport (4.0.2) lib/active_support/inflector/methods.rb:224:in `constantize'
activesupport (4.0.2) lib/active_support/dependencies.rb:535:in `get'
activesupport (4.0.2) lib/active_support/dependencies.rb:566:in `constantize'
actionpack (4.0.2) lib/action_dispatch/routing/route_set.rb:76:in `controller_reference'
actionpack (4.0.2) lib/action_dispatch/routing/route_set.rb:66:in `controller'
actionpack (4.0.2) lib/action_dispatch/routing/route_set.rb:44:in `call'
actionpack (4.0.2) lib/action_dispatch/journey/router.rb:71:in `block in call'
actionpack (4.0.2) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.0.2) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.0.2) lib/action_dispatch/routing/route_set.rb:680:in `call'

1 个答案:

答案 0 :(得分:4)

尝试移动代码,将environment.rb的新变量添加到config/initializers/inflections.rb,然后重新加载服务器。此外,您不需要为路线指定controller选项,只需将其设为resources :escs, path: '/parts/escs'即可正常使用。