http://weblog.jamisbuck.org/2007/2/5/nesting-resources
The RESTful routes feature in Rails makes it really, really simple to nest resources within each other. Just give a block to the “map.resources” call, and define further resources on the value yielded to that block:
map.resources :accounts do |accounts|
accounts.resources :people do |people|
people.resources :notes do |notes|
notes.resources :comments
end
end
end
That monstrosity would allow you to define routes like:
accounts_url #-> /accounts
account_url(1) #-> /accounts/1
people_url(1) #-> /accounts/1/people
person_url(1,2) #-> /accounts/1/people/2
notes_url(1,2) #-> /accounts/1/people/2/notes
note_url(1,2,3) #-> /accounts/1/people/2/notes/3
comments_url(1,2,3) #-> /accounts/1/people/2/notes/3/comments
comment_url(1,2,3,4) #-> /accounts/1/people/2/notes/3/comments/4
Simple! However, in using RESTful routes more and more, I’m coming to realize that this is not a best practice. Rule of thumb: resources should never be nested more than 1 level deep. A collection may need to be scoped by its parent, but a specific member can always be accessed directly by an id, and shouldn’t need scoping (unless the id is not unique, for some reason).
Think about it. If you only want to view a specific comment, you shouldn’t have to specify the account, person, and note for the comment in the URL. (Permission concerns can come into this, to some degree, but even then I’d argue that judicious use of the session is better than complicating your URLs.) However, if you want to view all comments for a particular note, then you do need to scope the request by that note. Given the above nesting of routes, I’m finding the following a better (if slightly more verbose) method:
map.resources :accounts do |accounts|
accounts.resources :people, :name_prefix => "account_"
end
map.resources :people do |people|
people.resources :notes, :name_prefix => "person_"
end
map.resources :notes do |notes|
notes.resources :comments, :name_prefix => "note_"
end
map.resources :comments
You’ll notice that I define each resource (except accounts) twice: once at the top level, and once nested within another resource. For the nested resources, I also give a “name_prefix”—this gets tacked onto the front of the named routes that are generated.
So, the above mappings give you the following named routes:
accounts_url #-> /accounts
account_url(1) #-> /accounts/1
account_people_url(1) #-> /accounts/1/people
person_url(2) #-> /people/2
person_notes_url(2) #-> /people/2/notes
note_url(3) #-> /notes/3
note_comments_url(3) #-> /notes/3/comments
comment_url(4) #-> /comments/4
The URL’s are shorter, and the parameters to the named routes are much simpler. It’s an all-around win! I won’t go so far as to say that resources should never be deeply nested, but I will say that you should think long and hard before you go that route.
用户评论也值得一看。
分享到:
相关推荐
在提供的压缩包文件中,我们可以看到"Ruby语言中文教程",这暗示了资源可能包含了关于Ruby语言的基础知识和进阶内容,对于学习RoR至关重要。Ruby是RoR的基础,理解其语法和特性对于掌握RoR框架极其关键。 **Ruby...
标题中的“RoR中文解决方案”指的是Ruby on Rails(简称RoR)的一个针对中文环境的解决方案。Ruby on Rails是一个开源的Web应用程序框架,它遵循MVC(Model-View-Controller)架构模式,用Ruby语言编写,旨在使开发...
在本文中,我们将探讨一些RoR性能优化的关键方面,主要基于JavaEye网站在使用RoR过程中积累的实际经验。 首先,RoR性能优化涉及到多个层面,包括应用程序的部署、服务器配置以及代码优化。在部署方面,操作系统的...
在Ror块中,不仅有常规的残差学习,还有更高层次的残差学习,即残差学习的残差。这种多层次的结构使得网络能更细致地理解特征,并且有助于减少过拟合,提高模型的泛化能力。 **CIFAR-10和CIFAR-100数据集** CIFAR-...
NULL 博文链接:https://xuxiangpan888.iteye.com/blog/266696
在这个选题方向中,我们主要探讨的是与RoR相关的源代码分析和学习。源代码是理解任何编程语言或框架工作原理的关键,对于RoR来说也不例外。 在深入源代码之前,我们需要了解RoR的一些核心概念。如ActiveRecord,它...
在Ruby on Rails(ROR)开发环境中,安装和配置正确的依赖包是至关重要的。这个压缩包包含了一系列用于ROR框架的基础组件,但不包括Ruby本身。让我们深入了解一下这些包的作用和重要性。 首先,`actionpack`是Rails...
"ror实例"可能指的是在学习或实践中,通过创建一个具体的Rails应用程序来理解和掌握RoR的工作原理和最佳实践。 在Ruby on Rails中,重要知识点包括: 1. **Gemfile与Gemfile.lock**:项目中的Gemfile用于指定项目...
**初探ROR** Ruby on Rails(简称ROR)是一个基于Ruby编程语言的开源Web应用程序框架,它遵循MVC(模型-视图-控制器)架构模式,旨在促进开发过程的简洁性和效率。Ruby on Rails的核心理念是“Don't Repeat ...
基于RoR的博客系统,代码风格简单清晰,前后太完善,适合初学者。
### ROR 文件的上传与下载:深入解析与实践 在Ruby on Rails(简称ROR)框架下,处理文件的上传与下载是一项常见的需求,尤其是在构建包含媒体内容的应用程序时。本文将基于给定的文件信息,详细阐述如何在Rails...
### Windows上搭建Ruby on Rails(ROR)环境详解 #### 一、引言 随着Web开发技术的不断发展,Ruby on Rails(简称Rails或ROR)作为一种高效、简洁且优雅的Web开发框架,受到了广大开发者的青睐。然而,在Windows...
在IT行业中,Ruby on Rails(简称ROR)是一款基于Ruby语言的开源Web应用程序框架,它遵循MVC(Model-View-Controller)架构模式,旨在简化Web应用开发过程,提高开发效率。本文将深入探讨如何配置ROR开发环境,以及...
《机遇ROR的图书管理系统》是一份以Ruby on Rails(简称ROR)技术为核心,旨在构建高效、便捷的图书管理解决方案的学习资料。Ruby on Rails,是基于Ruby编程语言的开源Web应用框架,它遵循MVC(Model-View-...
2. **ActiveRecord**:这是RoR中的一个ORM(对象关系映射)库,允许开发者用Ruby类来代表数据库表,通过面向对象的方式来操作数据库,简化了数据库交互。 3. **Scaffolding**:RoR提供快速原型开发工具,能自动生成...
- **缓存机制**:讲解RoR中的多种缓存策略,包括页面缓存、动作缓存等,以提升应用性能。 - **路由规则**:介绍Rails的路由系统,学习如何定义URL到控制器方法的映射规则。 - **RESTful设计**:解释RESTful架构...