我已經完成了構建玩具應用程序以更好地學習Rails,但我對目前的路由實現不滿意。我想幹掉它們,但是我不知道如何去做,因爲Rails文檔只包含帶有資源的REST路由。這裏是我的routes.rb
:我該如何重構我的RESTless路線?
root 'index#home'
# Avatars
get '/:user/change_avatar' => 'avatars#edit', as: :change_avatar
post '/:user/change_avatar' => 'avatars#update', as: :update_avatar
# Users
get '/:user' => 'users#show', as: :show_user
get '/:user/edit' => 'users#edit', as: :edit_user
delete '/:user' => 'users#destroy', as: :destroy_user
# Playlists
post '/:user/playlists' => 'playlists#create', as: :playlists
get '/:user/:playlist' => 'playlists#show', as: :show_playlist
get '/:user/:playlist/edit' => 'playlists#edit', as: :edit_playlist
patch '/:user/:playlist' => 'playlists#update', as: :update_playlist
delete '/:user/:playlist' => 'playlists#destroy', as: :destroy_playlist
# Tracks
post '/:user/:playlist/tracks' => 'tracks#create', as: :tracks
get '/:user/:playlist/:track' => 'tracks#show', as: :show_track
get '/:user/:playlist/:track/edit' => 'tracks#edit', as: :edit_track
patch '/:user/:playlist/:track' => 'tracks#update', as: :update_track
delete '/:user/:playlist/:track' => 'tracks#destroy', as: :destroy_track
# Photos
delete '/:user/:playlist/:track/photos/:id' => 'photos#destroy', as: :destroy_photo
如可以注意到,在:user
,:playlist
,並:track
動態段出現在幾乎所有的航線。我該如何改進?
使用範圍可以幫助 – Kiloreux
看看HTTPS://www.airpair。 com/ruby-on-rails/posts/building-a-restful-api-in-a-rails-application – Kiloreux
感謝您的提示。很有幫助。你可以看看我的答案,看看我最終結束了什麼。 – Duos