IDにスラッグを使用しているので、/ Artists / radiohead / songs/karma-policeの代わりに/songs/ radiohead/karma-policeのようなURLが必要です。
スラッグは次の方法で実現できます。
def to_param
slug
end
しかし、標準のRESTful URLからモデル名(「曲」)を削除する方法はありますか?
IDにスラッグを使用しているので、/ Artists / radiohead / songs/karma-policeの代わりに/songs/ radiohead/karma-policeのようなURLが必要です。
スラッグは次の方法で実現できます。
def to_param
slug
end
しかし、標準のRESTful URLからモデル名(「曲」)を削除する方法はありますか?
:path
オプションをresources
呼び出しに渡すことにより、パスセグメントをオーバーライドできます。
resources :songs, path: "songs/:artist_id"
これにより、これらのルートが生成されます
songs GET /songs/:artist_id(.:format) {:action=>"index", :controller=>"songs"}
POST /songs/:artist_id(.:format) {:action=>"create", :controller=>"songs"}
new_song GET /songs/:artist_id/new(.:format) {:action=>"new", :controller=>"songs"}
edit_song GET /songs/:artist_id/:id/edit(.:format) {:action=>"edit", :controller=>"songs"}
song GET /songs/:artist_id/:id(.:format) {:action=>"show", :controller=>"songs"}
PUT /songs/:artist_id/:id(.:format) {:action=>"update", :controller=>"songs"}
DELETE /songs/:artist_id/:id(.:format) {:action=>"destroy", :controller=>"songs"}
これをあなたの中に入れれroutes.rb
ば、うまくいくはずです。
match 'artists/:artist_id/:id' => 'songs#show', :as => 'artist_song'
そうする場合は:as
、他のルートがこのルートよりも優先されないようにしてください。