Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何重新命名路线?_Ruby On Rails_Ruby On Rails 3_Routes_Url Routing - Fatal编程技术网

Ruby on rails 如何重新命名路线?

Ruby on rails 如何重新命名路线?,ruby-on-rails,ruby-on-rails-3,routes,url-routing,Ruby On Rails,Ruby On Rails 3,Routes,Url Routing,我在我的站点上为部分和页面设置了嵌套路由 resources :sections do resources :pages end 这是一个示例URL: sitename.com/sections/5/pages/22 我不喜欢“章节”这个名字,更喜欢“章节” sitename.com/chapters/5/pages/22 我假设重新命名模型会非常复杂,那么如何轻松地重新命名路由?将所需的URL段名称作为值传递给path参数: resources :sections, :p

我在我的站点上为部分和页面设置了嵌套路由

  resources :sections do
    resources :pages
  end
这是一个示例URL:

sitename.com/sections/5/pages/22
我不喜欢“章节”这个名字,更喜欢“章节”

sitename.com/chapters/5/pages/22

我假设重新命名模型会非常复杂,那么如何轻松地重新命名路由?

将所需的URL段名称作为值传递给
path
参数:

resources :sections, :path => :chapters do
  resources :pages
end
这将导致以下路径:

           section_pages GET    /chapters/:section_id/pages(.:format)          pages#index
                          POST   /chapters/:section_id/pages(.:format)          pages#create
         new_section_page GET    /chapters/:section_id/pages/new(.:format)      pages#new
        edit_section_page GET    /chapters/:section_id/pages/:id/edit(.:format) pages#edit
             section_page GET    /chapters/:section_id/pages/:id(.:format)      pages#show
                          PUT    /chapters/:section_id/pages/:id(.:format)      pages#update
                          DELETE /chapters/:section_id/pages/:id(.:format)      pages#destroy
                 sections GET    /chapters(.:format)                            sections#index
                          POST   /chapters(.:format)                            sections#create
              new_section GET    /chapters/new(.:format)                        sections#new
             edit_section GET    /chapters/:id/edit(.:format)                   sections#edit
                  section GET    /chapters/:id(.:format)                        sections#show
                          PUT    /chapters/:id(.:format)                        sections#update
                          DELETE /chapters/:id(.:format)                        sections#destroy

这与嵌套的routes块如何匹配?嵌套关系保持不变。像平常一样申报。好的,谢谢。如果我想将旧路径重定向到新路径,我可以使用
get'/sections',to:redirect('/chapters')
吗?或者我需要更多的东西来处理嵌套路由?实际上,如果您查看正在输出的路由,您会注意到命名路由实际上保持不变。因此,对名为
节的路径的任何引用将自动路由到更新的路由。您不需要声明任何其他路由来重新路由现有的路径/链接。但是在我的网站或其他地方的硬编码URL呢?我希望他们重定向到新的模块路径。