`
biyeah
  • 浏览: 201384 次
  • 来自: ...
社区版块
存档分类
最新评论

[转]ROR 中的Nesting resources

 
阅读更多
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.

用户评论也值得一看。
分享到:
评论

相关推荐

    ror中文资料

    在提供的压缩包文件中,我们可以看到"Ruby语言中文教程",这暗示了资源可能包含了关于Ruby语言的基础知识和进阶内容,对于学习RoR至关重要。Ruby是RoR的基础,理解其语法和特性对于掌握RoR框架极其关键。 **Ruby...

    RoR中文解决方案(很不错哦)

    标题中的“RoR中文解决方案”指的是Ruby on Rails(简称RoR)的一个针对中文环境的解决方案。Ruby on Rails是一个开源的Web应用程序框架,它遵循MVC(Model-View-Controller)架构模式,用Ruby语言编写,旨在使开发...

    RoR性能优化经验谈

    在本文中,我们将探讨一些RoR性能优化的关键方面,主要基于JavaEye网站在使用RoR过程中积累的实际经验。 首先,RoR性能优化涉及到多个层面,包括应用程序的部署、服务器配置以及代码优化。在部署方面,操作系统的...

    神经网络ror resenet模型

    在Ror块中,不仅有常规的残差学习,还有更高层次的残差学习,即残差学习的残差。这种多层次的结构使得网络能更细致地理解特征,并且有助于减少过拟合,提高模型的泛化能力。 **CIFAR-10和CIFAR-100数据集** CIFAR-...

    ror

    NULL 博文链接:https://xuxiangpan888.iteye.com/blog/266696

    RoR选题方向—源代码

    在这个选题方向中,我们主要探讨的是与RoR相关的源代码分析和学习。源代码是理解任何编程语言或框架工作原理的关键,对于RoR来说也不例外。 在深入源代码之前,我们需要了解RoR的一些核心概念。如ActiveRecord,它...

    ror实例

    "ror实例"可能指的是在学习或实践中,通过创建一个具体的Rails应用程序来理解和掌握RoR的工作原理和最佳实践。 在Ruby on Rails中,重要知识点包括: 1. **Gemfile与Gemfile.lock**:项目中的Gemfile用于指定项目...

    初探ROR

    **初探ROR** Ruby on Rails(简称ROR)是一个基于Ruby编程语言的开源Web应用程序框架,它遵循MVC(模型-视图-控制器)架构模式,旨在促进开发过程的简洁性和效率。Ruby on Rails的核心理念是“Don't Repeat ...

    RoRBlog 基于RoR的博客系统

    基于RoR的博客系统,代码风格简单清晰,前后太完善,适合初学者。

    Windows 上搭建 ROR环境

    之前本人在windows 上搭建ROR环境,屡屡受挫,上网查资料、加入ruby on rails的qq群,关注ruby社区等,没有得到一套完整的正确的方法,很多朋友说不要在windows 弄ROR,很费力。的确很费力,后来在朋友的帮助下,...

    家蚕Ror2基因的克隆与表达分析

    家蚕Ror2基因的克隆与表达分析,刘伟,陈菲,Ror家族属于受体酪氨酸激酶中的孤儿受体,在进化上非常保守。本研究克隆了家蚕Ror2基因BmRor2的ORF序列,BmRor2基因的ORF为1924 bp,编码638

    ROR环境配置

    在IT行业中,Ruby on Rails(简称ROR)是一款基于Ruby语言的开源Web应用程序框架,它遵循MVC(Model-View-Controller)架构模式,旨在简化Web应用开发过程,提高开发效率。本文将深入探讨如何配置ROR开发环境,以及...

    机遇ROR 的图书管理系统

    《机遇ROR的图书管理系统》是一份以Ruby on Rails(简称ROR)技术为核心,旨在构建高效、便捷的图书管理解决方案的学习资料。Ruby on Rails,是基于Ruby编程语言的开源Web应用框架,它遵循MVC(Model-View-...

    freemis 基于ror框架的mis

    2. **ActiveRecord**:这是RoR中的一个ORM(对象关系映射)库,允许开发者用Ruby类来代表数据库表,通过面向对象的方式来操作数据库,简化了数据库交互。 3. **Scaffolding**:RoR提供快速原型开发工具,能自动生成...

    ROR绿色最新环境(2013/3/10)

    ROR环境 Ruby version 1.9.3 (java) RubyGems version 1.8.24 Rack version 1.4 Rails version 3.2.12 JavaScript Runtime therubyrhino (Rhino) Active Record version 3.2.12 Action Pack version 3.2.12 ...

    ASP.NET项目移植ROR实践----DB转移(已完成)

    ASP.NET项目移植到Ruby on Rails (ROR)的实践中,DB转移是一个关键步骤。这个过程涉及到将现有的ASP.NET应用程序的数据模型、数据库结构以及业务逻辑迁移到ROR框架中。在这个过程中,我们需要理解两个不同框架之间的...

    從 PL/SQL 中調用 ROR 创建的 Web 服务

    标题 "從 PL/SQL 中調用 ROR 创建的 Web 服务" 暗示了我们需要讨论如何在 Oracle 的 PL/SQL 环境中利用 Ruby on Rails (ROR) 构建的 Web 服务。ROR 是一个流行的开源 Web 应用框架,它基于 Ruby 语言,而 PL/SQL 则...

Global site tag (gtag.js) - Google Analytics