尝试注销时出错:无法找到带有' id' = sign_out的用户

时间:2015-05-31 19:03:23

标签: ruby-on-rails

我尝试注销用户时收到此错误。我查了很多同样错误的帖子,但没有人解决我的错误,希望你能帮助我。

错误如下:

    ActiveRecord::RecordNotFound in UsersController#destroy 
Couldn't find User with 'id'=sign_out

以下是我的代码:

users_controller.rb

    def destroy
      @user.destroy
      respond_to do |format|
        format.html { redirect_to unauthenticated_root_path }
        format.json { head :no_content }
      end
    end

    private

    private
    # Use callbacks to share common setup or constraints between actions.
    def set_user
      @user = User.find(params[:id])
    end

查看/用户/ index.html.erb

<li>
    <%= link_to "Logout", destroy_user_session_path, :method => :delete %>
  </li>

的routes.rb

    Rails.application.routes.draw do
  get 'admin/index'

  resources :contacts

  resources :afections

  resources :injuries

  resources :allergies

  resources :trainers

  resources :idusuarios

  resources :diseases

  resources :weights

  resources :diets

  resources :exercices

  resources :profiles

  resources :users

  devise_for :users

  get '/users/sign_out' => 'devise/sessions#destroy'

  devise_scope :user do
    authenticated :user do
      root 'pagina#index', as: :authenticated_root
    end

    unauthenticated do
      root 'devise/sessions#new', as: :unauthenticated_root
    end
  end
end

2 个答案:

答案 0 :(得分:1)

在您的路线中将get '/users/sign_out' => 'devise/sessions#destroy'移到resources :users之上。路由在路由文件中的顺序优先。

答案 1 :(得分:0)

在路线中使用resources :users时,您有以下路线:

/users
/users/:id
/users/:id/edit
and ...

因此,当调用/users/sign_out时,认为sign_out是一个ID。

例如,将/users/sign_out更改为/user/sign_out

BUT

如果要自定义会话路径,可以执行以下操作:

  1. 自定义会话路径:

    as :user do
        get 'user/signin' => 'devise/sessions#new', :as => :new_user_session
        post 'user/signin' => 'devise/sessions#create', :as => :user_session
        delete 'user/signout' => 'devise/sessions#destroy', :as => :destroy_user_session, via: [:delete, :get]
    end
    
  2. 并在设计中跳过会话:

     devise_for :users, :skip => [:sessions]
    
    1. 如果您想要用户注册,您必须在设计中跳过它,然后才能使用用户脚手架:

      as :user do
          get 'user/signin' => 'devise/sessions#new', :as => :new_user_session
          post 'user/signin' => 'devise/sessions#create', :as => :user_session
          delete 'user/signout' => 'devise/sessions#destroy', :as => :destroy_user_session, via: [:delete, :get]
      end
      scope "admin" do                
          resources :users 
          devise_for :users, :skip => [:sessions, :registrations]
      end