由于模块名称空间中的下划线,从rails 3.1升级到rails 3.2时出现路由错误

我们有一个rails应用程序,我们有一个API命名空间,定义为Api :: V1,Api :: V1_2,Api :: V1_3等。

在routes.rb中,我们设置了以下内容

namespace :api do ["v1", "v1.2", "v1.3", "latest"].each do |name| namespace name, {:module => name.gsub(".","__"), :as => name.gsub(".","_") } do constraints :format => /json/ do # user scoped views resources :some_endpoint, :only => [:create, :index, :show, :update, :delete], :path => "scheduler" do member do put 'edit' post 'duplicate' end end end end end 

它在使用Rails 3.1运行时工作正常,但我们在表单的rails 3.2中遇到路由错误:

 [INFO pid: 17025: 14-07-28 19:06:15 ] Started GET "/api/v1.2/commands/1" for 192.168.1.130 at Mon Jul 28 19:06:15 -0700 2014 [FATAL pid: 17025: 14-07-28 19:06:15 ] ActionController::RoutingError (uninitialized constant Api::V12): activesupport (3.2.17) lib/active_support/inflector/methods.rb:219:in `constantize' activesupport (3.2.17) lib/active_support/inflector/methods.rb:218:in `each' activesupport (3.2.17) lib/active_support/inflector/methods.rb:218:in `constantize' actionpack (3.2.17) lib/action_dispatch/routing/route_set.rb:69:in `controller_reference' actionpack (3.2.17) lib/action_dispatch/routing/route_set.rb:54:in `controller' actionpack (3.2.17) lib/action_dispatch/routing/route_set.rb:32:in `call' journey (1.0.4) lib/journey/router.rb:68:in `call' journey (1.0.4) lib/journey/router.rb:56:in `each' journey (1.0.4) lib/journey/router.rb:56:in `call' actionpack (3.2.17) lib/action_dispatch/routing/route_set.rb:608:in `call' omniauth (1.1.1) lib/omniauth/strategy.rb:177:in `call!' omniauth (1.1.1) lib/omniauth/strategy.rb:157:in `call' sass (3.2.6) lib/sass/./sass/plugin/rack.rb:54:in `call' warden (1.2.1) lib/warden/manager.rb:35:in `call' warden (1.2.1) lib/warden/manager.rb:34:in `catch' warden (1.2.1) lib/warden/manager.rb:34:in `call' actionpack (3.2.17) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call' rack (1.4.5) lib/rack/etag.rb:23:in `call' rack (1.4.5) lib/rack/conditionalget.rb:25:in `call' 

当我们使用rails 3.1运行它时,上面转换为Api :: V1_2。

知道这里可能是什么错误,我该如何解决? 代码在所有其他情况下都能正常工作,包括渲染资产等。只有在这种情况下,rails才会抛出错误。

曾经遇到过同样的事情。 他们的github问题列表中存在此错误

https://github.com/rails/rails/issues/5849

然后一个拉请求

https://github.com/rails/rails/pull/6105

但讨论没有找到任何解决方案。 共同声明就像

不喜欢模块中的不受欢迎的内容。

因此,您必须从模块名称中删除下划线