Rails 嵌套资源和 :path => “/”

发布于 2024-12-26 12:12:28 字数 260 浏览 1 评论 0原文

我有以下路由,允许像 /:username/:project_name 这样的 url

resources :users, :path => "/" do
  resources :projects, :path => "/"
end

问题是 /:username/edit 不起作用,因为它正在寻找名为“edit”的项目。

有什么办法解决这个问题吗?谢谢!

I have the following routes, which allows for urls like /:username/:project_name

resources :users, :path => "/" do
  resources :projects, :path => "/"
end

The problem is that /:username/edit doesn't work, because it is looking for a project with the name of 'edit'.

Any way around this? Thanks!

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

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

发布评论

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

评论(1

离不开的别离 2025-01-02 12:12:28

有几种方法可以做到这一点...

1)会给你像 /:user_id/:id 这样的路由(你想要的)

match '/:user_id/edit', :to => 'users#edit', :as => :edit_user
resources :users, :except => [:edit], :path => "/" do
  resources :projects, :path => "/"
end

2)会给你像 /:user_id/projects/ 这样的路由:id (你似乎在避免这样做)

resources :users, :path => "/" do
  resources :projects
end

我个人更喜欢#2,因为它更干净,并且一目了然地提供了有关路线的更多信息。

A couple ways of doing this...

1) Will give you routes like /:user_id/:id (which you wanted)

match '/:user_id/edit', :to => 'users#edit', :as => :edit_user
resources :users, :except => [:edit], :path => "/" do
  resources :projects, :path => "/"
end

2) Will give you routes like /:user_id/projects/:id (which it seems like you're avoiding)

resources :users, :path => "/" do
  resources :projects
end

I personally prefer #2 since it is cleaner and provides more knowledge about the route at a glance.

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