简体   繁体   中英

Rails - Devise - Can't Cancel Account - Couldn't find User with id=users

I'm working with devise for authentication. My issue comes along when trying to delete a user. I get the following ActiveRecord::RecordNotFound Error Couldn't find User with id=users

The error is hitting at:

before_filter :authenticate_user!, :except => [:show]

def show
@user = User.find(params[:id])
end

and the parameters being passed are:

{"controller"=>"users", "action"=>"show", "id"=>"users"}

I have been wondering if this error is related to the class RegistrationsController < Devise::RegistrationsController I've created for devise customization, but I'm not sure.

My gut is telling me that this really is a routing issue. Here is my routes file:

Music::Application.routes.draw do

# resources :users



devise_scope :user do

  get "/edit/account/:id" , to: "devise/registrations#edit" , :as => "edit_account"

  get "/login" , to: "devise/sessions#new" , :as => "login"
  get "/logout" , to: "devise/sessions#destroy" , :as => "logout"
end

devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks", :registrations => "registrations" }

resources :contact_us_emails


get('/about-us', { :controller => "WelcomePages", :action => 'about', :as => 'about_us'})
get('/contact_us', { :controller => "WelcomePages", :action => 'contact_us', :as => 'contact_us'})

get('/search_collaborators', { :controller => "SongRooms", :action => 'search_collaborators', :as => 'search_collaborators'})

resources :searches do 
  collection do
    get '/song_rooms/:query', :controller => "searches", :action => 'song_rooms', :as => 'search_song_rooms'
    get '/users/:query', :controller => "searches", :action => 'users', :as => 'search_users'
    get '/final_songs/:query', :controller => "searches", :action => 'final_songs', :as => 'search_final_songs'
    get '/search_and_invite_facebok_friends/:user_id', :controller => "searches", :action => "search_and_invite_facebok_friends", :as => 'invite_fb_friends'
    end
  end

match 'download/:id' =>  'versions#download', :as => :download
match '/musicians' => 'searches#musicians', :via => :get, :as => 'musicians'
match '/producers' => 'searches#producers', :via => :get, :as => 'producers'

match '/accept/:id' => 'requests#accept', :via => :get, :as => 'accept'
match '/deny/:id' => 'requests#deny', :via => :get, :as => 'deny'

match '/users/:id' => 'devise/registrations#destroy', :via => :delete, :as => 'cancel_registration'


resources :versions do 
  resources :messages
  member do
    post :vote
    get :mark_as_final
    get :unmark_as_final
  end
end

post('/collaborators', {:controller => "Collaborators", :action => 'create', :as => 'create_collaborator'})

delete('song_rooms/:id/collaborators/:id', { :controller => "Collaborators", :action => 'destroy', :as => 'delete_collaborator'})

resources :song_rooms do 
  member do
    get 'choose_final_song'
  end
  resources :collaborators
end



resources :user_song_uploads
# resources :users




post('/user/:id/vote', { :controller => "Users", :action => 'vote', :as => 'user_vote'})





match '/:id', :to => "users#show", :as => :user, :via => :get


authenticated :user do
  root :to => "welcome_pages#home" , :as => "user_home"
end
root :to => "welcome_pages#index" , :as => "home"



end

Finally my cancel link_to action is this:

<%= link_to "Cancel my account", registration_path(resource), :data => { :confirm => "Are you sure?" }, :method => :delete %>

I've also tried switching out resource for resource_name, @user, @user.id, current_user, current_user.id.

Thanks for any help!!

You've setup a route going to devise/registrations#destroy and called it cancel_registration

match '/users/:id' => 'devise/registrations#destroy', :via => :delete, :as => 'cancel_registration'

Try changing your link to:

<%= link_to "Cancel my account", cancel_registration_path(resource), :data => { :confirm => "Are you sure?" }, :method => :delete %>

Figured out a work around:

devise_scope :user do
  get "/edit/account/:id" , to: "devise/registrations#edit" , :as => "edit_account"
  delete "/delete_user/:id", to: "devise/registrations#destroy", :as => "delete_account"
  get "/login" , to: "devise/sessions#new" , :as => "login"
  get "/logout" , to: "devise/sessions#destroy" , :as => "logout"
end

Routed this to action: in RegistrationsController < Devise::RegistrationsController

def destroy
  @user = User.find(params[:id])
  @user.destroy
  respond_to do |format|
    format.html { redirect_to home_path, notice: "We're sorry to see you go. Send us some feedback before you go!"}
  end
end

Finally: the link_to action:

<%= button_to "Cancel my account", delete_account_path(@user.id), :data => { :confirm => "Are you sure?" }, :method => :delete %>

I'm not sure why this was so troublesome. But placing my routing action inside the devise_scope seemed to be the solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM