本文介绍了rails 资源路由中的默认段名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在我的 rails 应用程序中创建一条路线
I want to create a route in my rails application along the lines of
/panda/blog
/tiger/blog
/dog/blog
熊猫、老虎和狗都是永久链接(对于动物类)
where panda, tiger, and dog are all permalinks (for an animal class)
通常的做法
map.resources :animals do |animal|
animal.resource :blog
end
将沿着
/animals/panda/blog
/animals/tiger/blog
/animals/dog/blog
但我不想要第一段,因为它总是一样的.
But i do not want the first segment, as it will always be the same.
我知道我可以通过手动路由来做到这一点,但我想知道如何使用 rails 资源,因为拥有动物和博客是我的要求.
I know I could do this by manual routing, but I want to know how to do using rails resources, as having animals and blogs is a requirement for me.
推荐答案
在 rails 3.x 中,可以添加 path =>""
到任何 resource
或 resources
调用以从生成的路径中删除第一段.
In rails 3.x, you can add path => ""
to any resource
or resources
call to remove the first segment from the generated path.
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"}
这篇关于rails 资源路由中的默认段名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!