kurenn / market_place_api

The API on Rails tutorial application
142 stars 68 forks source link

Chapter 6.2.6: ActionController::UrlGenerationError: No route matches {:action=>"destroy", :controller=>"api/v1/products" ... #90

Closed ruta0 closed 6 years ago

ruta0 commented 6 years ago

I am getting a routing problem with this destroy action from productsController, here is my routes:

namespace :api, defaults: { format: :json }, constraints: { subdomain: 'api' }, path: '/' do
    scope module: :v1, constraints: ApiConstraints.new(version: 1, default: true) do
      # list of resources here
      devise_for :users
      resources :users, only: [:show, :create, :update, :destroy] do
        resources :products, only: [:create, :update, :destroy]
      end
      resources :sessions, only: [:create, :destroy]
      resources :products, only: [:show, :index]
    end
  end

and this is my action from my ProductsController.rb:

def destroy
    product = current_user.products.find(params[:id])
    product.destroy
    head 204
  end

and this is my test case:

describe 'DELETE #destroy' do
    before(:each) do
      @user = FactoryBot.create :user
      @product = FactoryBot.create :product, user: @user
      api_authorization_header(@user.auth_token)
      delete :destroy, params: { userd_id: @user.id, id: @product.id }
    end

    it { should respond_with 204 }
  end

Any ideas why this happened?

kurenn commented 6 years ago

You have an typo error on the test:

delete :destroy, params: { userd_id: @user.id, id: @product.id }

Specifically the userd_id param name, which should be user_id, without the d

Please let me know!