我在Rails routes.rb中有以下内容:
resource :sign_up, only: [:new, :create]
resources :users
get 'users/activate/:token' => 'users#activate', as: 'activate_user'
这给了我以下路线:
Prefix Verb URI Pattern Controller#Action
sign_up POST /sign_up(.:format) sign_ups#create
new_sign_up GET /sign_up/new(.:format) sign_ups#new
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
activate_user GET /users/activate/:token(.:format) users#activate
我想摆脱get 'users/activate/:token' ...
路线并使用嵌套或范围,但我无法弄明白。有没有办法实现这个目标?
谢谢!
答案 0 :(得分:2)
您可以为用户设置收集路线:
resources :users do
collection do
get 'activate/:token', :action => :activate, :as => :activate
end
end
它会给你这样的路线:
Prefix Verb URI Pattern Controller#Action
activate_users GET /users/activate/:token(.:format) users#activate
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy