2015-08-31 6 views
10

Ich habe jede ähnliche Frage gelesen, die ich finden konnte und kann immer noch nicht mein Problem herausfinden.ActionController :: UrlGenerationError, keine Route stimmt überein

# 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 

Und der RSpec Fehler:

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

Die Ausgabe von 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 

Ich kann erfolgreich ein neues Element in einer vorhandenen Liste über curl erstellen, die mir sagt, dass die Route in Ordnung ist, muss ich etwas falsch in meinem Test tun.

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 

Ich bin wirklich verwirrt. Meine Routen sind korrekt eingerichtet. Eine ItemsController#create Methode existiert definitiv. Der Rest der Tests in items_controller_spec.rb bestanden ohne Problem.

Fehle ich etwas offensichtlich?

+0

Was sagt 'Rake-Routen' über deine Artikel-Routen? –

+0

Die Ausgabe von 'rake routes' (oben) wurde hinzugefügt. – RobertJoseph

Antwort

12

Hier sind die Korrekturen, die ich zu meinen Tests machen musste (items_controller_spec.rb). Ich habe den korrekten Hash nicht an post create: übergeben.

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

Soll Ihr 'ItemsController' nicht' Lists :: ItemsController' heißen und in einem 'lists'-Ordner landen? Es scheint, dass Sie nicht der Rails-Struktur Konvention folgen. Ich bin, und ich habe die gleichen Probleme. – Sebastialonso

+0

Der physische Speicherort der Controller-Datei hatte nichts mit meinem ursprünglichen Problem zu tun. – RobertJoseph

Verwandte Themen