嵌套控制器的 RSpec RoutingError

发布于 2024-11-29 13:09:19 字数 2305 浏览 0 评论 0原文

我正在测试嵌套控制器并收到以下错误:

  1) Checklists::ItemsController index action should render index template
     Failure/Error: get :index, :checklist_id => checklist.id
     ActionController::RoutingError:
       No route matches {:checklist_id=>1, :controller=>"checklists/items"}

在浏览器中加载 /checklists/1/items 加载正常。

我在规范中遗漏了什么吗?

路由:

  resources :checklists do
    resources :items, :controller => "Checklists::Items"
  end

位于命名空间文件夹中的控制器 (/app/controllers/checklists/items_controller.rb):

class Checklists::ItemsController < ApplicationController
  respond_to :html, :json

  def index
    @checklist_items = @checklist.items
    respond_with @checklist_items
  end
end

规范 (/spec/controllers/checklists/items_controller_spec.rb) :

describe Checklists::ItemsController do
  let(:user) { Factory :user, :role => 'admin' }
  let(:checklist) { Factory(:checklist) }
  let(:checklist_item) { Factory(:checklist_item) }

  before(:each) do
    sign_in_to(controller, user)
    Checklist.stub(:find => checklist)
  end

  it "index action should render index template" do
    get :index, :checklist_id => checklist.id
    response.should render_template(:index)
  end
end

更新:清单项目的路线

checklist_items GET    /checklists/:checklist_id/items(.:format) {:action=>"index", :controller=>"Checklists::Items"}
                POST   /checklists/:checklist_id/items(.:format) {:action=>"create", :controller=>"Checklists::Items"}
new_checklist_item GET    /checklists/:checklist_id/items/new(.:format) {:action=>"new", :controller=>"Checklists::Items"}
edit_checklist_item GET    /checklists/:checklist_id/items/:id/edit(.:format) {:action=>"edit", :controller=>"Checklists::Items"}
 checklist_item GET    /checklists/:checklist_id/items/:id(.:format) {:action=>"show", :controller=>"Checklists::Items"}
                PUT    /checklists/:checklist_id/items/:id(.:format) {:action=>"update", :controller=>"Checklists::Items"}
                DELETE /checklists/:checklist_id/items/:id(.:format) {:action=>"destroy", :controller=>"Checklists::Items"}

I'm testing a nested controller and get the following error:

  1) Checklists::ItemsController index action should render index template
     Failure/Error: get :index, :checklist_id => checklist.id
     ActionController::RoutingError:
       No route matches {:checklist_id=>1, :controller=>"checklists/items"}

In the browser loading /checklists/1/items loads fine.

Am I missing something in the spec?

The routes:

  resources :checklists do
    resources :items, :controller => "Checklists::Items"
  end

The controller located in namespaced folder (/app/controllers/checklists/items_controller.rb):

class Checklists::ItemsController < ApplicationController
  respond_to :html, :json

  def index
    @checklist_items = @checklist.items
    respond_with @checklist_items
  end
end

The spec (/spec/controllers/checklists/items_controller_spec.rb):

describe Checklists::ItemsController do
  let(:user) { Factory :user, :role => 'admin' }
  let(:checklist) { Factory(:checklist) }
  let(:checklist_item) { Factory(:checklist_item) }

  before(:each) do
    sign_in_to(controller, user)
    Checklist.stub(:find => checklist)
  end

  it "index action should render index template" do
    get :index, :checklist_id => checklist.id
    response.should render_template(:index)
  end
end

Update: Routes for checklist items

checklist_items GET    /checklists/:checklist_id/items(.:format) {:action=>"index", :controller=>"Checklists::Items"}
                POST   /checklists/:checklist_id/items(.:format) {:action=>"create", :controller=>"Checklists::Items"}
new_checklist_item GET    /checklists/:checklist_id/items/new(.:format) {:action=>"new", :controller=>"Checklists::Items"}
edit_checklist_item GET    /checklists/:checklist_id/items/:id/edit(.:format) {:action=>"edit", :controller=>"Checklists::Items"}
 checklist_item GET    /checklists/:checklist_id/items/:id(.:format) {:action=>"show", :controller=>"Checklists::Items"}
                PUT    /checklists/:checklist_id/items/:id(.:format) {:action=>"update", :controller=>"Checklists::Items"}
                DELETE /checklists/:checklist_id/items/:id(.:format) {:action=>"destroy", :controller=>"Checklists::Items"}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最近可好 2024-12-06 13:09:19

事实证明问题的解决方案就在路线中:

我更改

resources :items, :controller => "Checklists::Items"

resources :items, :controller => "checklists/items"

并且现在可以使用

It turns out the solution to the problem was in the routes:

I changed

resources :items, :controller => "Checklists::Items"

to

resources :items, :controller => "checklists/items"

and it works now

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文