Routing errors when upgrading from rails 3.1 to ra

2019-03-01 11:48发布

问题:

We have a rails application and we have a API namespace defined as Api::V1, Api::V1_2, Api::V1_3 etc.

In routes.rb, we have set the following

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

It works fine when running with Rails 3.1, but we get a routing error in rails 3.2 of the form:

[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'

The above translates to Api::V1_2 when we run it with rails 3.1.

Any idea what might be the error here and how do I fix it? The code works fine in all other cases including rendering of assets etc. Only in this case does rails throws an error.

回答1:

Came across same thing once. There was this bug on their github issue list

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

Then a pull request

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

But the discussion did not come to any solution. The common statement was like

Don't like undescores in Modules.

So you'll have to remove your underscores from the Module names