2011-08-15 44 views
1

我測試嵌套的控制器,並得到了以下錯誤:RSpec的RoutingError嵌套控制器

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"} 

在瀏覽器中加載/清單/ 1 /項加載的罰款。

我錯過了規範中的東西嗎?

的路線:

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 

該規範(/規格/控制器/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"} 
+0

耙路線的輸出是什麼? – apneadiving

+0

已更新帖子以包含路線 – dMix

回答

2

原來的解決問題的方法是在路線:

我改變

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

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

現在它的作品