3

次の行に沿って、レールアプリケーションでルートを作成したい

/panda/blog
/tiger/blog
/dog/blog

パンダ、トラ、犬はすべてパーマリンクです (動物クラスの場合)

これを行う通常の方法

map.resources :animals do |animal|
 animal.resource :blog
end

の線に沿ってルートを作成します

/animals/panda/blog
/animals/tiger/blog
/animals/dog/blog

しかし、常に同じであるため、最初のセグメントは必要ありません。

手動ルーティングでこれを行うことができることは知っていますが、動物とブログが必要なため、Rails リソースを使用して行う方法を知りたいです。

4

2 に答える 2

6

Rails 3.x では、path => ""any に追加するかresourceresources生成されたパスから最初のセグメントを削除するために呼び出すことができます。

resources :animals, :path => ""

$ rake routes

    animals GET    /                   {:action=>"index", :controller=>"animals"}
            POST   /                   {:action=>"create", :controller=>"animals"}
 new_animal GET    /new(.:format)      {:action=>"new", :controller=>"animals"}
edit_animal GET    /:id/edit(.:format) {:action=>"edit", :controller=>"animals"}
     animal GET    /:id(.:format)      {:action=>"show", :controller=>"animals"}
            PUT    /:id(.:format)      {:action=>"update", :controller=>"animals"}
            DELETE /:id(.:format)      {:action=>"destroy", :controller=>"animals"}
于 2011-06-01T13:11:55.770 に答える
1

このプラグインを使用できます:

http://github.com/caring/default_routing/tree/master

于 2008-10-08T10:45:24.810 に答える