Rails - 从RESTFul资源到定制路线
这是我正在尝试解决的一个设计问题。我将解释我现在拥有的以及我想要拥有的:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经尝试过与您尝试过的相同的操作,它仅返回您想要的结果,不确定您的问题是什么。在这里发布耙子路线结果。
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.