有没有更好的方法来设置以下两条路线:
post '/song:id/favorite', to: 'songs#favorite', as: :favorite
delete '/song:id/favorite', to: 'favorites#destroy', as: :destroy
以下是我的所有路线:
Rails.application.routes.draw do
root "home#index"
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
post '/song:id/favorite', to: 'songs#favorite', as: :favorite
delete '/song:id/favorite', to: 'favorites#destroy', as: :destroy
namespace :admin do
resource :dashboard, only: [:show]
end
resources :users, only: [:new, :create]
resources :playlists
resources :genres
resources :albums
resources :artists
resources :playlist_songs
resources :favorites
resources :songs
end
答案 0 :(得分:0)
尝试这样的事情
resources :songs do
member do
post 'favorite', to: 'songs#favorite', as: :favorite
delete 'favorite', to: 'favorites#destroy', as: :destroy
end
end
答案 1 :(得分:0)
这些动作并不属于歌曲控制器。考虑到它,它没有为一首歌做CRUD动作(你不创作,编辑或删除一首歌)。
我会使用一个名为嵌套资源的概念(http://blog.8thcolor.com/en/2011/08/nested-resources-with-independent-views-in-ruby-on-rails/ - 这是一个很棒的介绍)。
基本上,我会将我的路线构造为:
resources :songs do
resources :favourites, only: [:create, destroy]
end