问题描述
阅读:http://guides.rubyonrails.org/routing.html#adding-more-restful-actions
添加成员路由"是什么意思?
What does it mean to add a 'member route'?
或者添加一个路由到集合?
or do add a route to the collection?
谈到路由时,什么是成员和集合?
What is a member and a collection when talking about routes?
推荐答案
它们都是在 Rails 中向基于资源的路由添加额外操作的两种方式.
They're both ways to add additional actions to a resource-based route in Rails.
- 成员路由需要一个 ID,因为它作用于成员.
- 集合路由不需要 ID,因为它作用于对象集合.
我喜欢从 RESTful URL 的角度来考虑它们.考虑资源/模型的基础知识 Foo
I like to think of them in terms of RESTful URLs. Consider the basics for a resource/model Foo
GET /foo # FooController#index
GET /foo/:id # FooController#show
GET /foo/new # FooController#new
POST /foo # FooController#create
GET /foo/:id/edit # FooController#edit
PUT /foo/:id # FooController#update
DELETE /foo/:id # FooController#destroy
注意方法:
- 有些路由有 :id 占位符用于 Foo.id,因此请参考特定 Foo
- 有些路由没有 :id,因此引用 all Foos(和/或没有特定的 foo,如 #new 和 #create)
- 某些路由(index/create、show/update/destroy)具有相同的 URL,并使用 HTTP 方法来区分它们
- 有些路由(编辑/显示)基本相同(方法和 URL 前缀),只是末尾有不同的后缀(包括无后缀").
- Some routes have :id placeholders for Foo.id, and so refer to a specific Foo
- Some routes have no :id, and thus refer to all Foos (and/or no specific foo, as in #new and #create)
- Some routes (index/create, show/update/destroy) have the same URL, and use HTTP methods to differentiate between them
- Some routes (edit/show) are basically the same (method & URL prefix) except for a different suffix (including "no suffix") at the end.
成员路由和集合路由允许您使用与我上面列出的相同的技术添加其他路由/操作.
Member routes and collection routes let you add additional routes/actions using the same techniques as I listed above.
成员路由使用您提供的 URL 后缀和 HTTP 方法向特定实例添加自定义操作.因此,如果您有 :member => 的成员路由声明;{ :bar =>:get }
.你会得到一条额外的路线:
A member route adds a custom action to a specific instance using the URL suffix and HTTP method you provide. So, if you had a member route declaration of :member => { :bar => :get }
. you'd get an additional route of:
GET /foo/:id/bar # FooController#bar
注意它是如何以与edit"相同的方式重载 GET/foo/:id
的.这是您实现删除"操作的方式,该操作为销毁"操作提供用户界面.
Note how it overloads GET /foo/:id
in the same way that `edit' does. This is how you'd implement a "delete" action that provides a UI for the "destroy" action.
类似地,一个集合路由会向 集合 和/或一个非特定实例添加一个重载(由你来决定它到底意味着什么).所以,如果你声明了 :collection =>{ :baz =>:get }
,你会得到一条额外的路线:
Similarly, a collection route adds an overload to the collection and/or a non-specific instance (it's up to you to decide exactly what it implies). So, if you declared :collection => { :baz => :get }
, you'd get an additional route:
GET /foo/baz # FooController#baz
...与 new
非常相似.
...in very much the same way as new
.
您还可以自定义 HTTP 方法.
You can also customize the HTTP method.
例如,我最近有一个项目,我需要对 Comment
执行回复"操作.它与 Comment#create
(使用 POST)基本相同,只是它引用了特定的父 Comment
.所以,我创建了一个成员路由::member =>{:回复=>:post }
.这给了我:
For example, I just recently had a project where I needed a "reply" action on a Comment
. It's basically the same idea as Comment#create
(which uses POST), except that it's in reference to a specific parent Comment
. So, I created a member route: :member => { :reply => :post }
. This gave me:
POST /comment/:id/reply # CommentController#reply
这使路由保持安静,同时仍然扩展了基本的 7 个操作.
This keeps the routes restful while still expanding upon the basic 7 actions.
这篇关于会员和收集路线是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!