gpt4 book ai didi

ruby-on-rails - Controller 规范无法找到由我的 Rails 引擎定义的路由

转载 作者:行者123 更新时间:2023-12-04 06:31:16 25 4
gpt4 key购买 nike

我正在 Rails 3.0.12 下构建 Rails 引擎,但是 在尝试为我的引擎 Controller 编写规范时,我遇到了路由问题。

上下文

我一直在关注 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)>'

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

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

最佳答案

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

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

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

关于ruby-on-rails - Controller 规范无法找到由我的 Rails 引擎定义的路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10582165/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com