ruby on rails 中的收集路线和成员路线之间的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3028653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:46:38  来源:igfitidea点击:

difference between collection route and member route in ruby on rails?

ruby-on-railsruby

提问by never_had_a_name

What is the difference between collection routes and member routes in Rails?

Rails 中的集合路由和成员路由有什么区别?

For example,

例如,

resources :photos do
  member do
    get :preview
  end
end

versus

相对

resources :photos do
  collection do
    get :search
  end
end

I don't understand.

我不明白。

回答by Theo

A member route will require an ID, because it acts on a member. A collection route doesn't because it acts on a collection of objects. Preview is an example of a member route, because it acts on (and displays) a single object. Search is an example of a collection route, because it acts on (and displays) a collection of objects.

成员路由需要一个 ID,因为它作用于成员。集合路由不会,因为它作用于对象集合。预览是成员路由的一个示例,因为它作用于(并显示)单个对象。搜索是收集路线的一个例子,因为它作用于(并显示)一组对象。

回答by Amit Patel

                URL                 Helper                      Description
----------------------------------------------------------------------------------------------------------------------------------
member          /photos/1/preview   preview_photo_path(photo)   Acts on a specific resource so required id (preview specific photo)
collection      /photos/search      search_photos_path          Acts on collection of resources(display all photos)

回答by tybro0103

Theo's answer is correct. For documentation's sake, I'd like to also note that the two will generate different path helpers.

西奥的答案是正确的。为了文档起见,我还想指出,这两者将生成不同的路径助手。

member {get 'preview'}will generate:

member {get 'preview'}将产生:

preview_photo_path(@photo) # /photos/1/preview

collection {get 'search'}will generate:

collection {get 'search'}将产生:

search_photos_path # /photos/search

Note plurality!

注意复数!

回答by Beena Shetty

1) :collection- Add named routes for other actions that operate on the collection. Takes a hash of #{action} => #{method}, where method is :get/:post/:put/:delete, an array of any of the previous, or :any if the method does not matter. These routes map to a URL like /users/customers_list, with a route of customers_list_users_url.

1) :collection- 为对集合进行操作的其他操作添加命名路由。取散列#{action} => #{method},其中方法是:get/:post/:put/:delete,任何前面的数组,或者 :any 如果方法无关紧要。这些路由映射到像/users/customers_list这样的 URL ,路由为customers_list_users_url

map.resources :users, :collection => { :customers_list=> :get }

map.resources :users, :collection => { :customers_list=> :get }

2) :member- Same as :collection, but for actions that operate on a specific member.

2) :member- 与 相同:collection,但用于对特定成员进行操作的操作。

map.resources :users, :member => { :inactive=> :post }

map.resources :users, :member => { :inactive=> :post }

it treated as /users/1;inactive=> [:action => 'inactive', :id => 1]

它被视为 /users/1;inactive=> [:action => 'inactive', :id => 1]