开发者

RSpec RoutingError for nested controller

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) {:ac开发者_如何转开发tion=>"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"}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜