【问题标题】:Routing: prefix item name before controller resources路由:控制器资源前的项目名称前缀
【发布时间】:2011-01-25 01:41:20
【问题描述】:

我正在尝试让以下路线在我的 Rails 3 应用程序中工作。

场景
我的应用中有以下控制器:

  • 实践
  • 医生
  • 患者
  • 处方

在 routes.rb 中为每个人设置 :resources 为我提供来自

的路线
  • example.com/practices
  • example.com/doctors/1/edit 等

我想要的是以下资源丰富的路线,例如:

  • example.com/james_practice/docs 转换为医生控制器
  • example.com/james_practice/awesome_prescriptions 路由到处方控制器等。
    这两个都让我可以访问 :practice 名称,并使用所有帮助程序(如 edit_docs_path(doctor) 等)路由正确的控制器。

我该怎么做呢?我用过

resources :prescriptions, :path => "/:practice/awesome_prescriptions"

虽然它在“rake routes”中显示了正确的路由,但它仍然无法按预期工作。

【问题讨论】:

    标签: ruby-on-rails routing routes


    【解决方案1】:

    我认为这是您正在寻找的路线:

    scope :path => ":practice" do
      resources :docs, :controller => "doctors"
      resources :awesome_prescriptions, :controller => "prescriptions"
    end
    

    对了,Patients的例子你没有给我,所以我没有放在那里。

    【讨论】:

    • 谢谢,这就是我的意思。 “范围:路径”似乎是我不知道的魔法。
    【解决方案2】:

    map.resources 只是一种模式,更像是一种快捷方式。如果您想在示例中使用 URL,则应使用“命名路由”。 Rails 2.x 的一个很好的截屏视频:http://railscasts.com/episodes/34-named-routes

    编辑:阅读 Rails 教程中的第 3.2 节:http://guides.rubyonrails.org/routing.html

    附言您还将面临人们使用“。”的情况。以他们的名义,这会导致问题。 这是我的“标签”路线

    map.resources :tags, :requirements => { :id => %r([^/;,?]+) }

    【讨论】:

    • 我试过走命名路线。但这意味着我必须写出该路由的每个用例,包括每个控制器的所有 put/delete 等请求?
    猜你喜欢
    • 1970-01-01
    • 2017-06-10
    • 2018-12-18
    • 2019-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-24
    • 2015-03-13
    相关资源
    最近更新 更多