1

IDにスラッグを使用しているので、/ Artists / radiohead / songs/karma-policeの代わりに/songs/ radiohead/karma-policeのようなURLが必要です。

スラッグは次の方法で実現できます。

def to_param
  slug
end

しかし、標準のRESTful URLからモデル名(「曲」)を削除する方法はありますか?

4

2 に答える 2

1

: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"}
于 2012-01-08T21:41:44.560 に答える
0

これをあなたの中に入れれroutes.rbば、うまくいくはずです。

match 'artists/:artist_id/:id' => 'songs#show', :as => 'artist_song'

そうする場合は:as、他のルートがこのルートよりも優先されないようにしてください。

次に、このルーティングmatchリファレンスを確認してください

于 2012-01-07T23:51:59.857 に答える