我正在嘗試使用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等)來區分同一網址?
這完全指出我在正確的方向 - 確保您在Routes.rb文件中正確排序行。我把匹配線放在devise_for:用戶線下面,一切都運行良好。 – Eric 2011-07-08 21:19:06