繁体   English   中英

Rails路由的范围为“:locale”和浅层嵌套资源

[英]Rails routes with scope “:locale” and shallow nested resources

所以我想让Rails为我处理特定于语言环境的路由,例如

/en/companies
/nl/companies

这对路线定义很有用:

scope "(:locale)", :locale => /en|nl/ do
  resources :companies
end

但与此同时,公司拥有浅层嵌套资源,如下所示:

scope "(:locale)", :locale => /en|nl/ do
  resources :companies, :shallow => true do
    resources :pages
  end
end

这允许像/en/companies/1/pages这样的路径,但不允许像/en/pages/1/edit这样的路径。 由于“浅”也剥离了路径的“语言环境”部分,似乎我坚持使用/pages/1/edit?locale=en 有没有办法让Rails以这样的方式处理浅层嵌套资源,我可以使用/en/pages/1/edit

没错! 我在API文档中找到了答案。 神奇的是:shallow_path关键字,在上面的示例中,它的工作原理如下:

scope :path => "(:locale)", :shallow_path => "(:locale)", :locale => /en|nl/ do
  resources :companies, :shallow => true do
    resources :pages
  end
end

现在,像/en/pages/1/edit这样的URL可以完美运行!

非常感谢Pascal,这对我来说非常有用。 我在设置嵌套资源时注意到了类似的行为。

我会添加一些东西,使用块语句替代浅参数的选项。 现在使用您提供的语法,只有直接后代(:pages)将是浅的。

如果您想要更深层次地嵌套一个级别(让我们跳过关于这是否是最佳实践的论点),使用浅块将根据需要进行深度挖掘:

resources :users do 
  shallow do
    resources :categories do
      resources :sections do
        resources :pages
      end
    end
    resources :news
  end
end

以下是嵌套在其中的所有资源的可用路由助手的示例:users

new_category_section  GET    (/:locale)(/:locale)/categorys/:category_id/sections/new(.:format)     {:locale=>/fr|en/, :action=>"new", :controller=>"sections"}
edit_section          GET    (/:locale)(/:locale)/sections/:id/edit(.:format)                       {:locale=>/fr|en/, :action=>"edit", :controller=>"sections"}
section               GET    (/:locale)(/:locale)/sections/:id(.:format)                            {:locale=>/fr|en/, :action=>"show", :controller=>"sections"}
                      PUT    (/:locale)(/:locale)/sections/:id(.:format)                            {:locale=>/fr|en/, :action=>"update", :controller=>"sections"}
                      DELETE (/:locale)(/:locale)/sections/:id(.:format)                            {:locale=>/fr|en/, :action=>"destroy", :controller=>"sections"}

   section_pages      GET    (/:locale)(/:locale)/sections/:section_id/pages(.:format)              {:locale=>/fr|en/, :action=>"index", :controller=>"pages"}
                      POST   (/:locale)(/:locale)/sections/:section_id/pages(.:format)              {:locale=>/fr|en/, :action=>"create", :controller=>"pages"}
new_section_info_page GET    (/:locale)(/:locale)/sections/:section_id/pages/new(.:format)          {:locale=>/fr|en/, :action=>"new", :controller=>"pages"}
        dit_info_page GET    (/:locale)(/:locale)/pages/:id/edit(.:format)                          {:locale=>/fr|en/, :action=>"edit", :controller=>"pages"}
            info_page GET    (/:locale)(/:locale)/pages/:id(.:format)                               {:locale=>/fr|en/, :action=>"show", :controller=>"pages"}
                      PUT    (/:locale)(/:locale)/pages/:id(.:format)                               {:locale=>/fr|en/, :action=>"update", :controller=>"pages"}
                      DELETE (/:locale)(/:locale)/pages/:id(.:format)                               {:locale=>/fr|en/, :action=>"destroy", :controller=>"pages"}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM