Rails 3 vs Rails 2 "wild controller" routing

发布于 2022-09-06 00:02:16 字数 1402 浏览 13 评论 0

I'm trying to get my Rails 3 project to automatically resolve namespaced controllers, similar to the Rails 2 behaviour. For example (Rails 2):

$ rails pathtest && cd pathtest
$ script/generate controller First
$ script/generate controller first/second
$ script/generate controller first/second/third
$ script/server

Requests to:

  • /first routes to first controller
  • /first/second routes to second controller
  • /first/second/third routes to third controller

    But, Rails 3 yields different routing using the 'wild controllers' path.

    $ rails new pathtest && cd pathtest
    $ rails generate controller First
    $ rails generate controller first/second
    $ rails generate controller first/second/third
    
    ## config/routes.rb
    Pathtest::Application.routes.draw do
      match ':controller(/:action(/:id(.:format)))'
    end
    
    $ rails server
    

    Requests to:

  • /first routes to first controller
  • /first/second routes to first#second
  • /first/second/third routes to first#second :id => 'third'

    I'm starting to think that kind of automatic resolution of namespaced controllers isn't the way anymore? Has anyone found a way to automatically resolve namespaced controllers like this?

    Thanks!

  • 如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(1

    a√萤火虫的光℡ 2022-09-13 00:02:16

    Try this:

    Pathtest::Application.routes.draw do
      resources :first do
        resources :second do
          resources :third
        end
      end
    end
    
    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文