在 Rails 3 中路由嵌套资源 [英] Routing nested resources in Rails 3

查看:22
本文介绍了在 Rails 3 中路由嵌套资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常常见的嵌套路由案例,我觉得它看起来像这样(在某种伪符号中):

I have a pretty common case for nested routes, I feel like, that looks something like this (in some sort of pseudonotation):

'/:username/photos' => Show photos for User.find_by_username
'/photos' => Show photos for User.all

简而言之:我有用户.他们有照片.我希望能够在他们的页面上显示他们的照片.我还希望能够显示所有照片,而不管用户是谁.我想让我的路由保持 RESTful 并且使用内置的 resource 方法感觉是正确的方法.

In a nutshell: I have users. They have photos. I want to be able to show their photos on their page. I also want to be able to show all photos, regardless of the user. I'd like to keep my routes RESTful and using the built-in resource methods feels like the right way to do it.

选项 1 这样做是让 PhotosController#index 使用条件来检查给出了哪些参数并获取照片列表并设置视图(用户的照片与所有照片不同).路由它甚至很容易:

Option 1 for doing this is to have PhotosController#index use a conditional to check which params are given and get the list of photos and set the view (different for a user's photos than for all photos). It's even easy to route it:

resources :photos, :only => [:index]
scope ':/username' do
  resources :photos
end

轰隆隆.似乎 Rails 就是为此而设置的.然而,在路线之后,事情变得更加复杂.在 PhotosController#index 操作中返回的条件变得越来越臃肿并且正在做大量的委托.随着应用程序的增长以及我想要显示照片的方式数量的增加,情况只会变得更糟.

Boom. It'd seem like Rails was setup for this. After the routes, though, things get more complicated. That conditional back in the PhotosController#index action is just getting more and more bloated and is doing an awful lot of delgation. As the application grows and so do the number of ways I want to show photos, it is only going to get worse.

选项 2 可能是使用 User::PhotosController 处理用户照片,使用 PhotosController 处理显示所有照片.

Option 2 might be to have a User::PhotosController to handle user photos, and a PhotosController to handle showing all photos.

resources :photos, :only => [:index]
namespace :user, :path => '/:username' do
  resources :photos
end

生成以下路由:

           photos GET    /photos(.:format)                    {:action=>"index", :controller=>"photos"}
      user_photos GET    /:username/photos(.:format)          {:action=>"index", :controller=>"user/photos"}
                  POST   /:username/photos(.:format)          {:action=>"create", :controller=>"user/photos"}
   new_user_photo GET    /:username/photos/new(.:format)      {:action=>"new", :controller=>"user/photos"}
  edit_user_photo GET    /:username/photos/:id/edit(.:format) {:action=>"edit", :controller=>"user/photos"}
       user_photo GET    /:username/photos/:id(.:format)      {:action=>"show", :controller=>"user/photos"}
                  PUT    /:username/photos/:id(.:format)      {:action=>"update", :controller=>"user/photos"}
                  DELETE /:username/photos/:id(.:format)      {:action=>"destroy", :controller=>"user/photos"}

我认为这很有效,但一切都在用户模块下,我觉得当我将它与其他东西集成时可能最终会导致问题.

This works pretty well, I think, but everything is under a User module and I feel like that might end up causing problems when I integrate it with other things.

  • 有人有过这样的经历吗?
  • 谁能分享更好的处理方法?
  • 使用这些选项中的任何一个需要考虑的其他优缺点吗?

更新:我已经开始实施选项 2,因为它让 Rails 的逻辑工作而不是覆盖它感觉更干净.到目前为止一切进展顺利,但我还需要将我的命名空间重命名为 :users 并添加一个 :as =>:user 以防止它与我的 User 模型发生冲突.我还覆盖了 User 模型上的 to_param 方法以返回用户名.路径助手仍然以这种方式工作.

Update: I've gone ahead implementing Option 2 because it feels cleaner allowing Rails' logic to work rather than overriding it. So far things are going well, but I also needed to rename my namespace to :users and add an :as => :user to keep it from clashing with my User model. I've also overridden the to_param method on the User model to return the username. Path helpers still work this way, too.

我仍然很感激对此方法的反馈.我是按照预期的方式做事,还是滥用了这个功能?

推荐答案

执行此操作的最佳方法取决于应用程序,但在我的情况下肯定是选项 B.使用命名空间路由我能够使用模块来以非常干净的方式将不同的关注点分离到不同的控制器中.我还使用特定于命名空间的控制器向特定命名空间中的所有控制器添加共享功能(例如,添加 before_filter 以检查命名空间中所有资源的身份验证和权限).

The best way to do this depends on the application, but in my case it is certainly Option B. Using namespaced routes I'm able to use a module to keep different concerns separated out into different controllers in a very clean way. I'm also using a namespace-specific controller to add shared functionality to all controllers in a particular namespace (adding, for example, a before_filter to check for authentication and permission for all resources in the namespace).

这篇关于在 Rails 3 中路由嵌套资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆