我試圖從我的Post模型創建API端點。它應該能夠閱讀,更新,刪除和創建帖子。嵌套API路線
但是一篇文章需要與主題相關聯才能創建,所以我將它嵌套在主題下。這裏是我的路線
namespace :api do
namespace :v1 do
resources :users, only: [:index, :show, :create, :update]
resources :posts, only: [:show, :update, :destroy]
resources :topics, except: [:edit, :new] do
resources :posts, only: [:create]
end
end
end
api_v1_post GET /api/v1/posts/:id(.:format) api/v1/posts#show
PATCH /api/v1/posts/:id(.:format) api/v1/posts#update
PUT /api/v1/posts/:id(.:format) api/v1/posts#update
DELETE /api/v1/posts/:id(.:format) api/v1/posts#destroy
api_v1_topic_posts POST /api/v1/topics/:topic_id/posts(.:format) api/v1/posts#create
api_v1_topics GET /api/v1/topics(.:format) api/v1/topics#index
POST /api/v1/topics(.:format) api/v1/topics#create
api_v1_topic GET /api/v1/topics/:id(.:format) api/v1/topics#show
PATCH /api/v1/topics/:id(.:format) api/v1/topics#update
PUT /api/v1/topics/:id(.:format) api/v1/topics#update
DELETE /api/v1/topics/:id(.:format) api/v1/topics#destroy
我的問題是即時得到一個「沒有匹配的路由」錯誤,當我跑我的Rspec的
我不知道我的頭頂上的答案,但我不認爲嵌套,這實際上是一個好主意。相反,我只想將該主題作爲發佈數據中的參數。如果主題是以「新」形式預先確定的,那麼您可以在隱藏輸入中獲得該值。 –
我在嵌套,如果因爲我遵循我正在參加的課程中的說明,我對API非常陌生,所以現在我幾乎失去了。 「爲POST api/v1/topics /:topic_id/create_post創建一個端點,因爲一個帖子必須與一個主題相關聯,所以將這個端點嵌套到路由主題下,並將該操作添加到'Api :: V1 :: TopicsController'。」這是我被指示要做的事 –
你確定這些是正確的路線映射嗎?我期望看到類似於:'POST/api/v1/topics /:id/post'的嵌套在那裏的帖子路徑。嘗試再次檢查...運行耙路線並顯示結果。你使用的是什麼rails版本? Rails 4? –