加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 19:32:51 所属栏目:百科 来源:网络整理
导读:我想在我的rails应用程序中创建一条路线 /panda/blog/tiger/blog/dog/blog 熊猫,老虎和狗都是永久性的(动物类) 这样做的正常方式 map.resources :animals do |animal| animal.resource :blogend 会沿着线路创建路线 /animals/panda/blog/animals/tiger/blog/
我想在我的rails应用程序中创建一条路线
/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资源,因为有动物和博客是我的要求.

解决方法

在rails 3.x中,您可以添加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"}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读