我们有一个 Rails 应用程序,并且有一个定义为 Api::V1、Api::V1_2、Api::V1_3 等的 API 命名空间。
在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 才会抛出错误。