本文介绍了由于模块命名空间中的下划线,从 rails 3.1 升级到 rails 3.2 时出现路由错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

在使用 Rails 3.1 时运行良好,但在 Rails 3.2 中出现以下形式的路由错误:

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'

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

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

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

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.

推荐答案

曾经遇到过同样的事情.他们的 github 问题列表中有这个错误

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

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

然后是拉取请求

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

但是讨论没有得出任何解决方案.常见的语句就像

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

不喜欢模块中的下划线.

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

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

这篇关于由于模块命名空间中的下划线,从 rails 3.1 升级到 rails 3.2 时出现路由错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 10:40
查看更多