0

我的 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' ...路线并改用嵌套或范围,但我无法弄清楚。有没有办法做到这一点?

谢谢!

4

1 回答 1

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
于 2013-06-25T05:59:33.103 回答