2013-06-25 37 views
0

我在我的Rails routes.rb中的以下內容:製作Rails的路由美麗

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' ...路線並使用嵌套或範圍代替,但我無法弄清楚。有沒有辦法做到這一點?

謝謝!

回答

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 
+0

感謝您的答覆,@Domon。任何想法如何使用沒有id的'member'塊?對於命名的路由,目標是'activate_user'而不是'activate_users'。 – cmoel