会员和收集路线

在资源内定义成员块会创建一条路径,该路由可以作用于该基于资源的路由的单个成员:

resources :posts do
  member do
    get 'preview'
  end
end

这会生成以下成员路由:

get '/posts/:id/preview', to: 'posts#preview'
# preview_post_path

收集路由允许创建可以作用于资源对象集合的路由:

resources :posts do
  collection do
    get 'search'
  end
end

这会生成以下收集路径:

get '/posts/search', to: 'posts#search'
# search_posts_path

另一种语法:

resources :posts do
  get 'preview', on: :member
  get 'search',  on: :collection
end