Fred Willmore
Fred Willmore

Reputation: 4604

Rails nested controller - error: No route matches

Rails 5.2.1 RSpec 3.8

I'm trying to build a REST API with nested controllers. I'm getting an error:

No route matches {:action=>"index", :controller=>"playlists/playlist_items", :format=>:json, :playlist=>60}

Here's my routes.rb:

Rails.application.routes.draw do
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
  resources :playlists do
    resources :playlist_items, controller: 'playlists/playlist_items'
  end
  resources :playlist_items
  resources :artists
  resources :tracks
end

Here is the output from rake routes:

                   Prefix Verb   URI Pattern                                                                              Controller#Action
  playlist_playlist_items GET    /playlists/:playlist_id/playlist_items(.:format)                                         playlists/playlist_items#index
                          POST   /playlists/:playlist_id/playlist_items(.:format)                                         playlists/playlist_items#create
   playlist_playlist_item GET    /playlists/:playlist_id/playlist_items/:id(.:format)                                     playlists/playlist_items#show
                          PATCH  /playlists/:playlist_id/playlist_items/:id(.:format)                                     playlists/playlist_items#update
                          PUT    /playlists/:playlist_id/playlist_items/:id(.:format)                                     playlists/playlist_items#update
                          DELETE /playlists/:playlist_id/playlist_items/:id(.:format)                                     playlists/playlist_items#destroy
... etc ...

Here is the test that's failing:

describe Playlists::PlaylistItemsController do
  describe "GET #index" do
    before do
      @playlist =  create :playlist, { date: '2000-01-01' } 
      get :index, params: { playlist: @playlist.id, format: :json }
    end
    it "returns http success" do
      expect(response).to have_http_status(:success)
    end
  end
end

The error complains that no route matches

{:action=>"index", :controller=>"playlists/playlist_items", :format=>:json, :playlist=>60}

but as far as I can tell this line from rake routes matches exactly:

  playlist_playlist_items GET    /playlists/:playlist_id/playlist_items(.:format)                                         playlists/playlist_items#index

What am I missing?

Upvotes: 0

Views: 298

Answers (1)

max
max

Reputation: 101811

The parameter is called playlist_id not playlist.

describe Playlists::PlaylistItemsController do
  describe "GET #index" do
    before do
      @playlist =  create :playlist, { date: '2000-01-01' } 
      get :index, params: { playlist_id: @playlist.id, format: :json }
    end
    it "returns http success" do
      expect(response).to have_http_status(:success)
    end
  end
end

Note that the controller should return a 401 CREATED response.

You can also nest resources in a module by adding the module option:

resources :playlists do
  resources :playlist_items, module: :playlists
end

Upvotes: 2

Related Questions