找不到id为'sign_out'的用户。
找不到id为'sign_out'的用户。
我正在Rails中使用devise,但是目前无法注销我的用户。
当我使用users/log_out页面时,会出现以下错误:
在UsersController#show中的ActiveRecord::RecordNotFound 找不到id为sign_out的用户
无论如何,这是我的users控制器:
class UsersController < ApplicationController def new @user = User.new end def create @user = User.new(user_params) if @user.save redirect_to users_path else render 'new' end end def index @users=User.all end def edit @user = User.find(params[:id]) end def destroy @user = User.find(params[:id]) @user.destroy redirect_to users_path end def update @user = User.find(params[:id]) if @user.update_attributes(user_params) redirect_to user_path(@user.id) else render 'edit' end end def show @user = User.find(params[:id]) end private def user_params params.require(:user).permit(:name, :email, :password) end end
我的route.rb:
Rails.application.routes.draw do devise_for :users do get "/users/sign_out" => "devise/sessions#destroy", :as => :destroy_user_session end resources :posts resources :users end
我的devise迁移文件:
class AddDeviseToUsers < ActiveRecord::Migration def self.up change_table(:users) do |t| ## 数据库可验证性 t.string :email, null: false, default: "" t.string :encrypted_password, null: false, default: "" ## 可恢复性 t.string :reset_password_token t.datetime :reset_password_sent_at ## 可记住性 t.datetime :remember_created_at ## 可追踪性 t.integer :sign_in_count, default: 0, null: false t.datetime :current_sign_in_at t.datetime :last_sign_in_at t.string :current_sign_in_ip t.string :last_sign_in_ip ## 可确认性 # t.string :confirmation_token # t.datetime :confirmed_at # t.datetime :confirmation_sent_at # t.string :unconfirmed_email #仅在使用重新确认时 ## 可锁定性 # t.integer :failed_attempts, default: 0, null: false #仅在锁定策略为:failed_attempts时 # t.string :unlock_token #仅在解锁策略为:email或:both时 # t.datetime :locked_at # 如果你的原始模型中未包含时间戳,请取消下面的注释。 # t.timestamps end add_index :users, :email, unique: true add_index :users, :reset_password_token, unique: true # add_index :users, :confirmation_token, unique: true # add_index :users, :unlock_token, unique: true end def self.down # 默认情况下,我们不想对已存在的模型进行回滚迁移的任何假设。请编辑下面的内容,指定您想要在此迁移中删除的字段。 raise ActiveRecord::IrreversibleMigration end end
应用程序的跟踪如下:
app/controllers/users_controller.rb:40:in `show'
在application.js文件中,需要包含以下内容:
//= require jquery //= require jquery_ujs
其中,jquery_ujs代表非侵入式JavaScript,它能确保许多功能(如删除方法)按预期工作。
如果出现了"Couldn't find User with 'id'=sign_out"的问题,可以参考Rafik在这里的回答:Couldn't find User with id=sign_out