2008-10-08 66 views
3

我想在我的轨道沿缺省段名在轨资源路由

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

,其中大熊猫,虎,狗都是固定链接(用于动物类)

行应用程序创建一个路由

这样

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

的方式,一般会沿着

线创建路线

但我不想第一个分段,因为它总是一样的。

我知道我可以通过手动路由来做到这一点,但我想知道如何使用rails资源,因为有动物和博客是我的需求。

回答

6

在Rails 3.x中,你可以添加path => ""任何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"}