Rails - 从RESTFul资源到定制路线

发布于 2025-01-03 20:39:46 字数 993 浏览 0 评论 0原文

这是我正在尝试解决的一个设计问题。我将解释我现在拥有的以及我想要拥有的:

1。实际设计

我定义了一个资源:用户,通过这样做,我在用户控制器中定义了不同的操作,例如新建、创建和更新。这可以通过以下url(如 users/new 、 users/:id 等)按预期工作... 现在我想向前迈出一步,我希望能够做到以下几点......

2。我在寻找什么

我希望能够有这样的路线:

users/overview/profile - This should be equivalent to `users/:id` (show action) 
users/overview/network - This should be equivalent to users/:id/network (list of networks for that user)

3。我的想法

我的第一个想法是定义这样的内容:

resource :users do
 namespace :overview do
  resource :networks
 end
end

但这适用于像这样的 url:users/:id/overview/networks 并且我不希望用户 ID 显示在 URL 中。所以我的问题是:

1 - 假设我可以从会话中获取用户 ID,我该如何处理 users/overview/networks 而不是 users/:id/overview/networks 。

2 - 我怎样才能管理这样的URL:users/overview/profile,其中实际上配置文件只是users/:id的显示方法现在我有定义了用户控制器中的所有操作,一切正常(新建、删除、创建、更新...)我只是不知道如何进入该“命名空间”概述/配置文件

This is a design problem that I am trying to figure out. I will explain what I have right now, and what I would like to have:

1. Actual design

I have a defined a resources :users and by doing so I have defined different actions such as new, create and update in the Users controller. This is working as expected by following urls like users/new , users/:id, etc...
Now I want to go one step forward, and I want to be able to do the following...

2. What am I looking for

I want to be able to have a route like this:

users/overview/profile - This should be equivalent to `users/:id` (show action) 
users/overview/network - This should be equivalent to users/:id/network (list of networks for that user)

3. My idea

My first idea was to define something like this:

resource :users do
 namespace :overview do
  resource :networks
 end
end

But this would work for urls like: users/:id/overview/networks and I don't want the user id to be shown in the URL. So my questions are:

1 - How can I deal with users/overview/networks instead of users/:id/overview/networks , assuming that I can get the user id from session.

2 - How can I be able to manage URLs like this: users/overview/profile where actually a profile is just the show method of users/:id Right now I have defined all the actions in the users controller and everything is working fine (new,delete,create,update...) I just don't know how to move into that "namespace" overview/profile

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

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

发布评论

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

评论(2

芸娘子的小脾气 2025-01-10 20:39:46

我已经尝试过与您尝试过的相同的操作,它仅返回您想要的结果,不确定您的问题是什么。在这里发布耙子路线结果。

users_overview_networks POST        /users/overview/networks(.:format)         {:action=>"create", :controller=>"overview/networks"}
new_users_overview_networks GET         /users/overview/networks/new(.:format)     {:action=>"new", :controller=>"overview/networks"}
edit_users_overview_networks GET         /users/overview/networks/edit(.:format)    {:action=>"edit", :controller=>"overview/networks"}
GET         /users/overview/networks(.:format)         {:action=>"show", :controller=>"overview/networks"}
PUT         /users/overview/networks(.:format)         {:action=>"update", :controller=>"overview/networks"}
DELETE      /users/overview/networks(.:format)         {:action=>"destroy", :controller=>"overview/networks"}
users POST        /users(.:format)                           {:action=>"create", :controller=>"users"}
new_users GET         /users/new(.:format)                       {:action=>"new", :controller=>"users"}
edit_users GET         /users/edit(.:format)                      {:action=>"edit", :controller=>"users"}
GET         /users(.:format)                           {:action=>"show", :controller=>"users"}
PUT         /users(.:format)                           {:action=>"update", :controller=>"users"}
DELETE      /users(.:format)                           {:action=>"destroy", :controller=>"users"}

I have tried same thing you tried, and it is returning your desired results only, not sure what is your problem. Posting rake routes result here.

users_overview_networks POST        /users/overview/networks(.:format)         {:action=>"create", :controller=>"overview/networks"}
new_users_overview_networks GET         /users/overview/networks/new(.:format)     {:action=>"new", :controller=>"overview/networks"}
edit_users_overview_networks GET         /users/overview/networks/edit(.:format)    {:action=>"edit", :controller=>"overview/networks"}
GET         /users/overview/networks(.:format)         {:action=>"show", :controller=>"overview/networks"}
PUT         /users/overview/networks(.:format)         {:action=>"update", :controller=>"overview/networks"}
DELETE      /users/overview/networks(.:format)         {:action=>"destroy", :controller=>"overview/networks"}
users POST        /users(.:format)                           {:action=>"create", :controller=>"users"}
new_users GET         /users/new(.:format)                       {:action=>"new", :controller=>"users"}
edit_users GET         /users/edit(.:format)                      {:action=>"edit", :controller=>"users"}
GET         /users(.:format)                           {:action=>"show", :controller=>"users"}
PUT         /users(.:format)                           {:action=>"update", :controller=>"users"}
DELETE      /users(.:format)                           {:action=>"destroy", :controller=>"users"}
清风不识月 2025-01-10 20:39:46
  scope :path => 'users/overview' do
    match ':id/profile', :to => 'users#show'
    match ':id/network', :to => 'users#network'
  end 
  scope :path => 'users/overview' do
    match ':id/profile', :to => 'users#show'
    match ':id/network', :to => 'users#network'
  end 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文