0

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

错误发生在:

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

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

传递的参数是:

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

我一直想知道这个错误是否与我为设计定制创建的类 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 "Cancel my account", registration_path(resource), :data => { :confirm => "Are you sure?" }, :method => :delete %>

我还尝试为 resource_name、@user、@user.id、current_user、current_user.id 切换资源。

谢谢你的帮助!!

4

2 回答 2

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 %>
于 2013-04-01T14:22:54.710 回答
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 < 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

最后:link_to 操作:

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

我不知道为什么这会这么麻烦。但是将我的路由操作放在 devise_scope 内似乎是解决方案。

于 2013-04-01T14:58:01.940 回答