2015-08-31 44 views
10

我已经通读了所有我能找到的类似问题,但仍无法找出我的问题。ActionController :: UrlGenerationError,没有路由匹配

# routes.rb 
Rails.application.routes.draw do 
    resources :lists, only: [:index, :show, :create, :update, :destroy] do 
    resources :items, except: [:new] 
    end 
end 

# items_controller.rb (excerpt) 
class ItemsController < ApplicationController 
    ... 

    def create 
    @list = List.find(params[:list_id]) 
    ... 
    end 
    ... 
end 

# items_controller_spec.rb (excerpt) 
RSpec.describe ItemsController, type: :controller do 
    ... 

    let!(:list) { List.create(title: "New List title") } 

    let(:valid_item_attributes) { 
    { title: "Some Item Title", complete: false, list_id: list.id } 
    } 

    let!(:item) { list.items.create(valid_item_attributes) } 
    describe "POST #create" do 
    context "with valid params" do 
     it "creates a new item" do 
     expect { 
      post :create, { item: valid_item_attributes, format: :json } 
     }.to change(Item, :count).by(1) 
     end 
    end 
    end 
    ... 
end 

而RSpec的错误:

1) ItemsController POST #create with valid params creates a new item 
    Failure/Error: post :create, { item: valid_item_attributes, format: :json } 
    ActionController::UrlGenerationError: 
     No route matches {:action=>"create", :controller=>"items", :format=>:json, :item=>{:title=>"Some Item Title", :complete=>false, :list_id=>1}} 

rake routes输出:

list_items  GET /lists/:list_id/items(.:format)   items#index 
       POST /lists/:list_id/items(.:format)   items#create 
edit_list_item GET /lists/:list_id/items/:id/edit(.:format) items#edit 
    list_item GET /lists/:list_id/items/:id(.:format)  items#show 
       PATCH /lists/:list_id/items/:id(.:format)  items#update 
       PUT /lists/:list_id/items/:id(.:format)  items#update 
       DELETE /lists/:list_id/items/:id(.:format)  items#destroy 

我可以通过curl成功在现有列表中创建一个新项目,该项目告诉我路线没问题,我必须在测试中做错事。

curl -i -X POST -H "Content-Type:application/json" -H "X-User-Email:[email protected]" -H "X-Auth-xxx" -d '{ "item": { "title": "new item", "complete": "false"} }' http://localhost:3000/lists/5/items 

我真的很困惑。我的路线设置正确。确定存在ItemsController#create方法。 items_controller_spec.rb的其余测试通过没有问题。

我错过了一些明显的东西吗?

+0

什么'耙路线'说你的物品路线? –

+0

添加'rake routes'的输出(上面)。 – RobertJoseph

回答

12

以下是我必须对我的测试所做的修复(items_controller_spec.rb)。我没有将正确的散列传递给post create:

describe "POST #create" do 
    context "with valid params" do 
     it "creates a new item" do 
     expect { 
      post :create, { list_id: list.id, item: valid_item_attributes, format: :json } 
     }.to change(Item, :count).by(1) 
     end 

     it "assigns a newly created item as @item" do 
     post :create, { list_id: list.id, item: valid_item_attributes, format: :json } 

     expect(assigns(:item)).to be_a(Item) 
     expect(assigns(:item)).to be_persisted 
     end 
    end # "with valid params" 

    context "with invalid params" do 
     it "assigns a newly created but unsaved item as @item" do 
     post :create, { list_id: list.id, item: invalid_item_attributes, format: :json } 

     expect(assigns(:item)).to be_a_new(Item) 
     end 

     it "returns unprocessable_entity status" do 
     put :create, { list_id: list.id, item: invalid_item_attributes, format: :json } 

     expect(response.status).to eq(422) 
     end 
    end # "with invalid params" 
    end # "POST #create" 
+0

Shoud''t你的'ItemsController'被称为'Lists :: ItemsController',并住在'lists'文件夹中?看来你没有遵循Rails结构约定。我是,我也有同样的问题。 – Sebastialonso

+0

控制器文件的物理位置与我的原始问题无关。 – RobertJoseph

相关问题