代码之家  ›  专栏  ›  技术社区  ›  Fred Willmore

Rails嵌套控制器-错误:没有路由匹配

  •  0
  • Fred Willmore  · 技术社区  · 7 年前

    轨道5.2.1 第3.8条

    我正在尝试用嵌套的控制器构建restapi。我得到一个错误:

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

    以下是我的路线.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
    

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

    以下是失败的测试:

    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
    

    错误抱怨没有路由匹配

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

    耙道 完全匹配:

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

    我错过了什么?

    1 回复  |  直到 7 年前
        1
  •  1
  •   max Mike Williams    7 年前

    参数被调用 playlist_id 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
    

    注意控制器应该返回 401 CREATED 回应。

    您还可以通过添加 module 选项:

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