2011-03-02 65 views
4

我正在嘗試使用Devise刪除用戶。我有一個用戶列表,每個用戶都有他們的電子郵件和他們旁邊的「刪除」鏈接,只有管理員才能看到。我希望能夠簡單地點擊刪除鏈接永久刪除用戶。以下代碼刪除我,管理員!在Devise中以管理員身份銷燬用戶

<%= link_to "delete", user_registration_path, :method => :delete, :confirm => "You sure?" %> 

我想你需要通過:您要刪除某種「destroy_user」方法的用戶的ID:

@user.find(params[:id]).destroy_user 

但你如何做到這一點的時候,你必須向user_registration_path提交DELETE請求?

------編輯--------

OK,我已經添加了這個方法我的用戶控制器:

def destroy 
    User.find(params[:id]).destroy 
    flash[:success] = "User destroyed." 
    redirect_to users_path 
end 

所以,我需要告訴用戶控制器在收到DELETE請求時調用destroy方法。你如何在routes.rb中做到這一點?目前,我有:

match '/users/:id', :to => 'users#show', :as => :user 
match '/all_users', :to => 'users#index', :as => :all_users 

我需要這樣的東西:

match 'delete_user', :to => 'users#destroy', :as => :destroy_user, :method => :delete 

但這不起作用。什麼應該在鏈接?:

<%= link_to "delete", destroy_user, :method => :delete, :confirm => "You sure?" %> 

換一種方式,你該把在routes.rb中的文件,以不同的請求類型(GET,DELETE等)來區分同一網址?

+0

這完全指出我在正確的方向 - 確保您在Routes.rb文件中正確排序行。我把匹配線放在devise_for:用戶線下面,一切都運行良好。 – Eric 2011-07-08 21:19:06

回答

2

設計沒有按」 t提供刪除其他用戶的操作,僅刪除當前登錄的用戶。您必須在其中一個控制器(很可能是無論哪個控制器都有顯示所有用戶的操作)中創建您自己的操作來處理除當前登錄的用戶之外的其他用戶。

+0

我試圖做到這一點,但掙扎(見編輯)。我不知道a)在路線文件中使用了什麼網址:'match [url],:to => users#destroy',以及b)如何告訴它擊中用戶#只有當此神祕網址收到刪除請求。 – Bazley 2011-03-02 20:25:52

+0

我想我試圖重新創建由路由文件中的命令'resources:users'實現的路由,但我不想使用'resources:users,:only => [:destroy]如果我可以幫忙它。 – Bazley 2011-03-02 20:39:10

4

替換「用戶」的要銷燬的實際用戶,例如:如果你要打印出來的電子郵件作爲user.email,然後插件用戶那裏等瞧

<%= link_to "delete", user_registration_path(user), :method => :delete, :confirm => "You sure?" %> 
0

Got it!只需要在路由中添加:via參數:

match '/users/:id', :to => 'users#show', :as => :user,   :via => :get 
match '/users/:id', :to => 'users#destroy', :as => :destroy_user, :via => :delete 
相關問題