Rails - 设计 - 无法取消帐户 - 找不到id = users的用户

时间:2013-04-01 14:17:32

标签: ruby-on-rails authentication devise

我正在使用设计进行身份验证。尝试删除用户时出现了我的问题。 我得到以下ActiveRecord :: RecordNotFound错误找不到具有id = users

的用户

错误发生在:

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

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

并且传递的参数是:

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

我一直想知道这个错误是否与此有关  class RegistrationsController< Devise :: RegistrationsController我是为设计定制而创建的,但我不确定。

我的直觉告诉我这确实是一个路由问题。这是我的路线文件:

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

最后我的取消link_to动作是:

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

我也尝试为resource_name,@ user,@ user.id,current_user,current_user.id切换资源。

感谢您的帮助!!

2 个答案:

答案 0 :(得分:0)

您已设置前往devise/registrations#destroy的路线,并将其称为cancel_registration

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

尝试将您的链接更改为:

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

答案 1 :(得分:0)

找出解决方法:

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

将此行动路由到:在RegistrationsController&lt;设计:: 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

最后:link_to动作:

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

我不确定为什么这么麻烦。但是将我的路由操作放在devise_scope中似乎是解决方案。