【问题标题】:Controller spec failing to find routes defined by my Rails engine控制器规范找不到我的 Rails 引擎定义的路由
【发布时间】:2012-05-21 20:50:49
【问题描述】:

我正在 Rails 3.0.12 下构建 Rails 引擎,但 在尝试为引擎控制器编写规范时遇到路由问题。

上下文

我一直在关注Enginex 布局。该引擎被命名为Featuring,并且不是孤立的。它本身不声明路由:没有featuring/config/routes.rb 文件。相反,为主应用程序提供了一个routes_for_feature 方法来定义特定于引擎的路由。

##
# featuring/lib/featuring/rails.rb
#
require 'featuring/rails/routing'

module Featuring
  class Engine < ::Rails::Engine
  end
end

##
# featuring/lib/featuring/rails/routing.rb
#
module ActionDispatch::Routing
  class Mapper

    def routes_for_feature(feature_name)
      resource_name = feature_name.to_s.pluralize.to_sym
      resources resource_name, :controller => "featuring/features", :only => [:index, :show], :feature => feature_name.to_s
    end
  end
end

按照 Enginex 模板,我有一个 Dummy 应用程序,它定义了这样的路线:

# featuring/spec/dummy/config/routes.rb
Dummy::Application.routes.draw do
  routes_for_feature :feature_model
end

问题

当我为 Dummy 应用程序运行 rails 服务器时,一切正常。可以浏览到http://localhost:3000/feature_models,请求成功。

我想指定我的Featuring::FeaturesController,但我无法让它找到路线。

这是规格:

# featuring/spec/controllers/features_controller_spec.rb
require 'spec_helper'

describe Featuring::FeaturesController do

  context "feature_models" do
    it "GET index should be successful" do
      puts Rails.application.routes.routes
      get :index, { :use_route => "featuring", :feature => "feature_models" }
      response.should be_success
    end
  end
end

这是运行此规范的结果:

rspec spec/controllers/features_controller_spec.rb:7
Featuring::FeaturesController
  feature_models
GET    /feature_models(.:format)                {:action=>"index", :controller=>"featuring/features"}
GET    /feature_models/:id(.:format)            {:action=>"show", :controller=>"featuring/features"}
    GET index should be successful (FAILED - 1)

Failures:

  1) Featuring::FeaturesController feature_models GET index should be successful
     Failure/Error: get :index, { :use_route => "featuring", :feature => "feature_models" }
     ActionController::RoutingError:
       No route matches {:feature=>"feature_models", :controller=>"featuring/features"}
     # ./spec/controllers/features_controller_spec.rb:8:in `block (3 levels) in <top (required)>'

如您所见,即使路由定义正确,指定的控制器似乎也找不到它们。

RoutingError:No route matches {:feature=&gt;"feature_models", :controller=&gt;"featuring/features"} 让我感到惊讶。 action =&gt; "index" 不显示。

【问题讨论】:

  • 我在 rails 3.2.3 中也遇到了这个问题。我确定它与 :action => "index" 无关,但我仍然对为什么会收到此 RoutingError 感到困惑...

标签: ruby-on-rails ruby-on-rails-3 controller rspec-rails rails-engines


【解决方案1】:

我遇到了类似的错误,而且我也对路由选项中缺少 {:action => "index"} 感到困惑。然而,事实证明这不是问题。 ActionDispatch 将缺少 :action 视为等同于 {:action => "index"}。见:

https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/routing/route_set.rb#L545

您的规范中可能缺少请求参数,就像我一样。在浏览器中加载页面时检查服务器日志中的参数行。

【讨论】:

    【解决方案2】:

    诀窍是将routes { } 添加到您的规范中,如下所示:

    describe Featuring::FeaturesController do
      routes { Featuring::Engine.routes }
      # ...
    end
    

    另见No Route Matches ... Rails Engine

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-27
      • 1970-01-01
      • 2012-06-19
      • 2012-08-18
      • 2014-02-15
      • 1970-01-01
      相关资源
      最近更新 更多