rails db列remember_token没有保存,总是为零?

时间:2014-09-01 23:17:42

标签: ruby-on-rails ruby database ruby-on-rails-3

为什么我无法更新rails中的db列? 数据库有一列 - ' remember_token'但是更新不起作用吗?

数据库迁移的结果

    ....
    ....
    ....

== 20140830041234 AddRememberTokenToUsers: migrating ==========================
-- add_column(:users, :remember_token, :string)
   -> 0.0010s
-- add_index(:users, :remember_token)
   -> 0.0000s
== 20140830041234 AddRememberTokenToUsers: migrated (0.0030s) =================

rails console - 输出! 在直接分配后,remember_token为零

irb(main):063:0* user
=> #<User id: 1, name: "Test", email: "hb@c.co", created_at: "2014-09-01 22:52:02", updated_at: "2014-09-01 22:52:02", p
assword_digest: "$2a$10$/66wO2dBte/xCXqxk.UAo.v.7.XZjsBFA3AerAWkHp16...", remember_token: nil>
irb(main):064:0> User.digest('asdasd')
=> "85136c79cbf9fe36bb9d05d0639c70c265c18d37"
irb(main):065:0> user.remember_token = User.digest('asdasd')
=> "85136c79cbf9fe36bb9d05d0639c70c265c18d37"
irb(main):066:0>
irb(main):067:0* user
=> #<User id: 1, name: "Test", email: "hb@c.co", created_at: "2014-09-01 22:52:02", updated_at: "2014-09-01 22:52:02", p
assword_digest: "$2a$10$/66wO2dBte/xCXqxk.UAo.v.7.XZjsBFA3AerAWkHp16...", remember_token: nil>
irb(main):068:0>

在update_attributes

之后,remember_token为nil
irb(main):071:0> user.update_attributes(name: "Yahoo", remember_token: "will this get saved")
  ←[1m←[36m (0.0ms)←[0m  ←[1mbegin transaction←[0m
  ←[1m←[35mUser Exists (1.0ms)←[0m  SELECT 1 AS one FROM "users" WHERE (LOWER("users"."email") = LOWER('hb@c.co') AND "u
sers"."id" != 1) LIMIT 1
  ←[1m←[36mSQL (7.0ms)←[0m  ←[1mUPDATE "users" SET "name" = ?, "updated_at" = ? WHERE "users"."id" = 1←[0m  [["name", "Y
ahoo"], ["updated_at", Mon, 01 Sep 2014 23:12:59 UTC +00:00]]
  ←[1m←[35m (8.0ms)←[0m  commit transaction
=> true
irb(main):072:0>
irb(main):073:0* user
=> #<User id: 1, name: "Yahoo", email: "hb@c.co", created_at: "2014-09-01 22:52:02", updated_at: "2014-09-01 23:12:59",
password_digest: "$2a$10$/66wO2dBte/xCXqxk.UAo.v.7.XZjsBFA3AerAWkHp16...", remember_token: nil>


以下是型号

class User < ActiveRecord::Base
    attr_accessor :remember_token
    before_save { self.email = email.downcase }
    before_create :create_remember_token

    def User.new_remember_token
        SecureRandom.urlsafe_base64
    end

    def User.digest(token)
        Digest::SHA1.hexdigest(token.to_s)
    end


private
    def create_remember_token
        self.remember_token = User.digest(User.new_remember_token)
    end

end


以防万一 - 这是控制器

class UsersController < ApplicationController
  def new
    @user = User.new
  end

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

  def create
    #@user = User.new(params.require(:user).permit(:name, :email, :password, :password_confirmation)
    @user = User.new(user_params)
    if @user.save
        # Handle a successful save.
            flash[:success] = "Welcome #{@user.name} to the Sample App!"
            redirect_to @user
        else
        render 'new'
        end
    end

    private
    def user_params
        params.require(:user).permit(:name, :email, :password,:password_confirmation, :remember_token)
    end
end

2 个答案:

答案 0 :(得分:-2)

您必须在用户模型中将:remember_token添加到attr_accessible

attr_accessible :remember_token

答案 1 :(得分:-2)

看来:remember_token属性还没有被rails选中。尝试重新启动控制台,它可能会起作用。