从Rails 3升级后,Rails 4中的可安装引擎路由错误

时间:2015-09-30 20:32:21

标签: ruby-on-rails ruby-on-rails-4 routing

我在Rails 4.2.4应用程序中遇到一些可安装的引擎时出现问题。我们最近(我知道,很遗憾我们对比赛有多晚)从Rails 3.2.23升级。我们的引擎路由在Rails 3.2.23中运行良好,但是当我尝试现在访问路由时(使用Paw或cURL),我得到一个RoutingError,没有路由匹配。但是,当我查看rake routeshttp://localhost:3000/rails/info/routes的输出时,路径确实存在正确的动词和所有内容。

我调查过的事情:

  • 路由不会被文件中的其他内容覆盖,因为我们将所有内容都包含在子域约束中。
  • 我使用最新版本的Rails重新创建了可安装引擎,并比较了差异。

另外,我正在使用versionist宝石。我不知道这是不是造成它的原因,但似乎相关。

的Gemfile

gem 'api', path: 'engines/api'

配置/ routes.rb中

constraints subdomain: /^api/, format: :json do
  mount Api::Engine, at: "/"
end

引擎/ API / LIB / API / engine.rb

module Api
  class Engine < ::Rails::Engine
    isolate_namespace Api

    config.generators do |g|
      g.template_engine :rabl
      g.test_framework  :rspec, view_specs: false
    end
  end
end

引擎/ API /配置/ routes.rb中

Api::Engine.routes.draw do
  api_version(:module => "V1", :parameter => { :name => "version", :value => "1" }) do
    resources :users
  end
end

示例cURL请求

curl -X "GET" "http://api.myapp.dev:3000/users/new?api_key=foobar&version=1" \
-H "Accept: application/json" \
-H "Content-Type: application/json"

日志/ development.log

ActionController::RoutingError (No route matches [GET] "/users/new"):
  actionpack (4.2.4) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  rollbar (2.3.0) lib/rollbar/middleware/rails/show_exceptions.rb:22:in `call_with_rollbar'
  newrelic_rpm (3.13.2.302) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
  web-console (2.2.1) lib/web_console/middleware.rb:39:in `call'
  newrelic_rpm (3.13.2.302) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
  actionpack (4.2.4) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  newrelic_rpm (3.13.2.302) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
  railties (4.2.4) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.2.4) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.2.4) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.2.4) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.2.4) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.2.4) lib/rails/rack/logger.rb:20:in `call'
  quiet_assets (1.1.0) lib/quiet_assets.rb:27:in `call_with_quiet_assets'
  newrelic_rpm (3.13.2.302) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
  request_store (1.2.0) lib/request_store/middleware.rb:8:in `call'
  newrelic_rpm (3.13.2.302) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
  actionpack (4.2.4) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  newrelic_rpm (3.13.2.302) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
  rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
  newrelic_rpm (3.13.2.302) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
  rack (1.6.4) lib/rack/runtime.rb:18:in `call'
  newrelic_rpm (3.13.2.302) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
  activesupport (4.2.4) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
  newrelic_rpm (3.13.2.302) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
  rack (1.6.4) lib/rack/lock.rb:17:in `call'
  newrelic_rpm (3.13.2.302) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
  rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
  newrelic_rpm (3.13.2.302) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
  railties (4.2.4) lib/rails/engine.rb:518:in `call'
  railties (4.2.4) lib/rails/application.rb:165:in `call'
  railties (4.2.4) lib/rails/railtie.rb:194:in `public_send'
  railties (4.2.4) lib/rails/railtie.rb:194:in `method_missing'
  newrelic_rpm (3.13.2.302) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
  passenger (5.0.20) src/ruby_supportlib/phusion_passenger/rack/thread_handler_extension.rb:94:in `process_request'
  passenger (5.0.20) src/ruby_supportlib/phusion_passenger/request_handler/thread_handler.rb:151:in `accept_and_process_next_request'
  passenger (5.0.20) src/ruby_supportlib/phusion_passenger/request_handler/thread_handler.rb:112:in `main_loop'
  passenger (5.0.20) src/ruby_supportlib/phusion_passenger/request_handler.rb:415:in `block (3 levels) in start_threads'
  passenger (5.0.20) src/ruby_supportlib/phusion_passenger/utils.rb:112:in `block in create_thread_and_abort_on_exception'

1 个答案:

答案 0 :(得分:3)

所以我想出了答案,这是约束中接受的语法的一个微妙的变化。我最终删除了mount调用之上的格式约束,并将其移动到引擎路由中。代码现在看起来像这样:

<强>配置/ routes.rb中

mount Api::Engine, at: '/', constraints: { subdomain: /^api/ }

<强>引擎/ API /配置/ routes.rb中

Api::Engine.routes.draw do
  api_version(module: "V1", parameter: { name: "version", value: "1" }) do
    constraints format: :json do # Added the format constraint here.
      resources :users
    end
  end
end

希望这对某人有所帮助,因为我花了一些时间来解决这个问题。