我最近觀看了railscast插曲#250 Authentication from Scratch (revised),我註冊/登錄/註銷操作正常。不過,我正在創建一個操作來從數據庫中刪除用戶,但是當我嘗試刪除用戶時,我目前遇到了一些錯誤。如何從數據庫中刪除/銷燬rails應用程序中的用戶?
的users_controller.rb刪除/銷燬行動如下所示,
def delete
# the below line calls the destroy method/action
self.destroy
end
def destroy
session[:user_id] = nil
# @user = User.find(params[:id])
@user.destroy
# User.find(parmas[:id]).destroy
# the below line didn't delete the current user :(
# @user = User.destroy
redirect_to :controller=>'users', :action => 'new'
end
我得到在瀏覽器中,當我試圖刪除用戶看起來像下面的錯誤消息。
包含刪除鏈接如下所示的頁面,index.html.erb
<h1>Welcome
<% if current_user %>
<%= current_user.email %>
<% end %>
</h1>
<p>You have <%= current_user.credit %> credits.</p>
<!-- http://stackoverflow.com/questions/5607155/ -->
<%= link_to('Delete your account', :controller => 'users', :action => 'destroy') %>
的routes.rb
Rails.application.routes.draw do
# the below generated route is not necessary
# get 'sessions/new'
# delete user route
#get 'delete' => 'users#delete'
# shortened routes, per railscast comment
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
get 'logout' => 'sessions#destroy'
# get 'signup', to: 'users#new', :as 'signup'
# get 'login', to: 'sessions#new', :as 'login'
# get 'logout', to: 'sessions#destroy', :as 'logout'
resources :users
resources :sessions
root to: 'users#new'
# get 'users/new'
# the below line specifies JSON as the default API format
namespace :api, defaults: {format: 'json'} do
namespace :v1 do
resources :users
end
end
將上述行更改爲'index .html.erb'到以下內容,'<%= link_to'刪除您的帳戶',users_path,:method =>:delete%>'但現在我收到以下錯誤,'沒有路由匹配[DELETE]「/用戶'' – Chris
'rake routes'的輸出是什麼,特別是與用戶有關的部分? – ABMagil
https://ghostbin.com/paste/zqvv8 – Chris