gpt4 book ai didi

ruby-on-rails - Rails 资源路由中的默认段名称

转载 作者:行者123 更新时间:2023-12-01 10:16:49 25 4
gpt4 key购买 nike

我想在我的 rails 应用程序中创建一条路线

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

其中 Pandas 、老虎和狗都是永久链接(对于动物类别)

正常的做法

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

将沿着

的路线创建路线
/animals/panda/blog
/animals/tiger/blog
/animals/dog/blog

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

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

最佳答案

在 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"}

关于ruby-on-rails - Rails 资源路由中的默认段名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/182040/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com