Rails 成员路由应该很容易吧?

发布于 2024-12-13 07:57:19 字数 1299 浏览 2 评论 0原文

对于此路由:

resources :projects do
  member do
    resources :stakeholders
  end
end

生成的路由是:

    projects_stakeholders GET    /projects/projects/:id/stakeholders(.:format)                                                  {:action=>"index", :controller=>"projects/stakeholders"}
                          POST   /projects/projects/:id/stakeholders(.:format)                                                  {:action=>"create", :controller=>"projects/stakeholders"}
    new_projects_stakeholder GET    /projects/projects/:id/stakeholders/new(.:format)                                              {:action=>"new", :controller=>"projects/stakeholders"}
    edit_projects_stakeholder GET    /projects/projects/:id/stakeholders/:id/edit(.:format)                                         {:action=>"edit", :controller=>"projects/stakeholders"}
projects_stakeholder GET    /projects/projects/:id/stakeholders/:id(.:format)                                              {:action=>"show", :controller=>"projects/stakeholders"}

由于这些路由有两次 :id 参数,如果我有,例如 URL 'projects/4/stakeholders/11'

在我的日志文件中我会看到:

Parameters: {"id"=>"11"}

然后我如何访问我的控制器内部的project_id?

谢谢!!!

For this route:

resources :projects do
  member do
    resources :stakeholders
  end
end

The generated routes are:

    projects_stakeholders GET    /projects/projects/:id/stakeholders(.:format)                                                  {:action=>"index", :controller=>"projects/stakeholders"}
                          POST   /projects/projects/:id/stakeholders(.:format)                                                  {:action=>"create", :controller=>"projects/stakeholders"}
    new_projects_stakeholder GET    /projects/projects/:id/stakeholders/new(.:format)                                              {:action=>"new", :controller=>"projects/stakeholders"}
    edit_projects_stakeholder GET    /projects/projects/:id/stakeholders/:id/edit(.:format)                                         {:action=>"edit", :controller=>"projects/stakeholders"}
projects_stakeholder GET    /projects/projects/:id/stakeholders/:id(.:format)                                              {:action=>"show", :controller=>"projects/stakeholders"}

As these routes have two times a :id parameter, if I have, for instance the URL 'projects/4/stakeholders/11'

In my log file I see this:

Parameters: {"id"=>"11"}

How can I then access my project_id from inside my controller?

Thanks!!!

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

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

发布评论

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

评论(1

西瑶 2024-12-20 07:57:19

您不需要围绕它的 member do 块。只需执行此操作,您就应该开始在参数中看到 project_id

resources :projects do
  resources :stakeholders
end

You don't need the member do block around it. Just do this, and you should start seeing a project_id in your params:

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