Ruby Rails--更新数据库中的模型

Ruby Rails--更新数据库中的模型,ruby,database,ruby-on-rails-3,Ruby,Database,Ruby On Rails 3,所以我在验证方面遇到了一个小问题——我创建了一个验证来确保数据库中没有用户共享相同的电子邮件地址。然后我在数据库中创建了一个用户。之后,我说user=user.find(1),它返回了我刚刚创建的用户。然后我想更改它的名称,所以我说user.name=“New name”,然后尝试使用user.save将它保存回数据库。但是,这个命令不再工作了(它返回false),我认为这与我的唯一性验证测试有关。有人能帮我解决这个问题吗 # == Schema Information # # Table na

所以我在验证方面遇到了一个小问题——我创建了一个验证来确保数据库中没有用户共享相同的电子邮件地址。然后我在数据库中创建了一个用户。之后,我说
user=user.find(1)
,它返回了我刚刚创建的用户。然后我想更改它的名称,所以我说
user.name=“New name”
,然后尝试使用
user.save
将它保存回数据库。但是,这个命令不再工作了(它返回false),我认为这与我的唯一性验证测试有关。有人能帮我解决这个问题吗

# == Schema Information
#
# Table name: users
#
#   id         :integer         not null, primary key
#  name       :string(255)
#  email      :string(255)
#  created_at :datetime
#  updated_at :datetime
#

class User < ActiveRecord::Base
    attr_accessor :password

    attr_accessible :name, :email,                    #says that the name and email attributes are publicly accessible to outside users.
                    :password, :password_confirmation #it also says that all attributes other than name and email are NOT publicly accessible.
                                                      #this protects against "mass assignment"

    email_regex = /^[A-Za-z0-9._+-]+@[A-Za-z0-9._-]+\.[A-Za-z0-9._-]+[A-Za-z]$/ #tests for valid email addresses.


    validates :name, :presence => true,
                     :length => {:maximum => 50}
    validates :email, :presence => true,
                      :format => {:with => email_regex},
                      :uniqueness => {:case_sensitive => false}
    validates :password, :presence => true,
                         :length => {:maximum => 20, :minimum => 6},
                         :confirmation => true

    before_save :encrypt_password

    def has_password?(submitted_password)
        #compare encrypted_password with the encrypted version of the submitted password.
        encrypted_password == encrypt(submitted_password)
    end

    def self.authenticate(email, submitted_password)
        user = find_by_email(email)
        if (user && user.has_password?(submitted_password))
            return user
        else
            return nil
        end 
    end

    private

        def encrypt_password 
            if (new_record?) #true of object has not yet been saved to the database
                self.salt = make_salt
            end
            self.encrypted_password = encrypt(password)
        end

        def encrypt(string)
            secure_hash("#{salt}--#{string}")
        end

        def secure_hash(string)
            Digest::SHA2.hexdigest(string) #uses cryptological hash function SHA2 from the Digest library to encrypt the string.
        end

        def make_salt
            secure_hash("#{Time.now.utc}--#{password}")
        end
end
#==架构信息
#
#表名:用户
#
#id:整数不为空,主键
#名称:字符串(255)
#电子邮件:string(255)
#创建时间:datetime
#更新时间:datetime
#
类用户true,
:length=>{:max=>50}
验证:email,:presence=>true,
:format=>{:with=>email_regex},
:唯一性=>{:区分大小写=>false}
验证:password,:presence=>true,
:length=>{:最大=>20,:最小=>6},
:确认=>true
保存前:加密密码
def有密码?(已提交密码)
#将加密密码与提交密码的加密版本进行比较。
加密密码==加密(已提交密码)
终止
def自我验证(电子邮件、提交的密码)
用户=通过电子邮件查找(电子邮件)
if(user&&user.has_密码?(已提交_密码))
返回用户
其他的
归零
终止
终止
私有的
def加密密码
如果(新建_记录?)#对象的true尚未保存到数据库中
self.salt=制造盐
终止
self.encrypted_password=加密(密码)
终止
def加密(字符串)
安全散列(“#{salt}--#{string}”)
终止
def secure_散列(字符串)
摘要:SHA2.hexdigest(string)#使用摘要库中的加密哈希函数SHA2对字符串进行加密。
终止
def制盐
安全散列(“#{Time.now.utc}--#{password}”)
终止
终止

尝试保存!看看异常告诉您什么

当我在IRB中输入user.save时,我看到的唯一错误消息就是“false”。我编辑了我的帖子以包含用户模型源代码。使用
User.save并查看引发了什么异常。同时检查
user.valid?
,如果为false则检查
user.errors