2015-08-31 10 views
10

मैंने हर समान प्रश्न के माध्यम से पढ़ा है जिसे मैं पा सकता हूं और अभी भी मेरी समस्या का पता नहीं लगा सकता।एक्शनकंट्रोलर :: 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

'रेक मार्ग' (उपरोक्त) से आउटपुट जोड़ा गया। – 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

अपने 'आइटम नियंत्रक' को चौंकाने वाला नहीं 'सूची :: आइटम नियंत्रक' कहा जाता है, और 'सूचियों' फ़ोल्डर के अंदर रहते हैं? ऐसा लगता है कि आप रेल संरचना सम्मेलन का पालन नहीं कर रहे हैं। मैं हूं, और मुझे एक ही समस्या है। – Sebastialonso

+0

नियंत्रक फ़ाइल के भौतिक स्थान के मेरे मूल मुद्दे से कोई लेना देना नहीं था। – RobertJoseph

संबंधित मुद्दे