ruby-on-rails – 没有资源名称的Rails路由
发布时间:2020-12-17 03:06:57 所属栏目:百科 来源:网络整理
导读:我的rails应用程序有一个Section模型和一个Page模型.一节有很多页面. # section.rbclass Section ActiveRecord::Base has_many :pages end# page.rbclass Page ActiveRecord::Base belongs_to :section end 假设我有一个带有’about’的段,并且该段有三个带
我的rails应用程序有一个Section模型和一个Page模型.一节有很多页面.
# section.rb class Section < ActiveRecord::Base has_many :pages end # page.rb class Page < ActiveRecord::Base belongs_to :section end 假设我有一个带有’about’的段,并且该段有三个带有slugs’intro’,’people’,’history’的页面,具有典型路由的url可能看起来像这样: http://example.com/sections/about/pages/intro http://example.com/sections/about/pages/people http://example.com/sections/about/pages/history 设置路线的最佳方法是什么,以便我可以使用这些网址: http://example.com/about/intro http://example.com/about/people http://example.com/about/history 解决方法
为了从部分和页面的所有路径中删除“部分”和“页面”,您可以使用:
resources :sections,path: '' do resources :pages,path: '' end 重要提示:请务必将其放在路线页面的底部.例如,你有一个示例控制器,并说你的routes.rb如下所示: resources :sections,path: '' end resources :examples root 'home#index' 使用上面的设置,转到http://example.com/examples会将您发送到“示例”部分,而不是示例#index,然后转到http://example.com/会将您发送到部分#索引而不是home #index.所以,上面的配置应该如下所示: resources :examples root 'home#index' resources :sections,path: '' end (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容