Вопрос

I'm writing a simple application and am having some difficulties updating a user's password from within rails console. I'm VERY new to RoR (and Ruby). I'm sure there's something simple I'm missing. I've included my model as well as the output I get from Terminal.

admin_users.rb Model

    require 'digest/sha1'
class AdminUser < ActiveRecord::Base

  attr_accessor :password

  EMAIL_REGEX = /^[A-Z0-9._%+-]+@[A-Z0-9.-]\.[A-Z]{2,4}$/i

  def self.authenticate(username="", password="")
    user = AdminUser.find_by_username(username)
    if user && user.password_match?(password)
        return user
    else
        return false
    end
  end

  def password_match?(password="")
    hashed_password == AdminUser.hash_with_salt(password, salt)
  end

  validates :first_name, :presence => true, :length => { :maximum => 25 }
  validates :last_name, :presence => true, :length => { :maximum => 50 }
  validates :username, :presence => true, :length => { :within => 8..25 }, :uniqueness => true
  validates :email, :presence => true, :length => { :maximum => 100 }, :uniqueness => true, :format=> EMAIL_REGEX, :confirmation => true

  # only on create ,so other attributes of this user can be changed
  validates_length_of :password, :within => 8..25, :on => :create

  before_save :create_hashed_password
  after_save :clear_password

  # scope :named, lambda {|first, last| where (:first_name => first, :last_name => last)}

  attr_protected :hashed_password, :salt

  def self.make_salt(username="")
    Digest::SHA1.hexdigest("Use #{username} with #{Time.now} to make salt")
  end

  def self.hash_with_salt(password="", salt="")
    Digest::SHA1.hexdigest("Put #{salt} on #{password}")
  end

  private

  def create_hashed_password
    # Whenever :password has a value, hashing is needed
    unless password.blank?
      #always use "self" when assigning values
      self.salt = AdminUser.make_salt(username) if salt.blank?
      self.hashed_password = AdminUser.hash_with_salt(password, salt)
    end
  end

  def clear_password
    # for security reasons and because hasing is not needed
    self.password = nil
  end

end

Terminal -

user = AdminUser.find(1)
user.password = ('secretpassword')
user.save

Then terminal gives me this...

    (0.1ms)  BEGIN
  AdminUser Exists (0.4ms)  SELECT 1 AS one FROM `admin_users` WHERE (`admin_users`.`username` = BINARY 'benlinus' AND `admin_users`.`id` != 1) LIMIT 1
  AdminUser Exists (0.2ms)  SELECT 1 AS one FROM `admin_users` WHERE (`admin_users`.`email` = BINARY 'email.email@me.com' AND `admin_users`.`id` != 1) LIMIT 1
   (0.1ms)  ROLLBACK
 => false 
2.0.0-p195 :018 > user.save
   (0.1ms)  BEGIN
  AdminUser Exists (0.3ms)  SELECT 1 AS one FROM `admin_users` WHERE (`admin_users`.`username` = BINARY 'benlinus' AND `admin_users`.`id` != 1) LIMIT 1
  AdminUser Exists (0.2ms)  SELECT 1 AS one FROM `admin_users` WHERE (`admin_users`.`email` = BINARY 'email.email@me.com' AND `admin_users`.`id` != 1) LIMIT 1
   (0.1ms)  

I'm not sure where the hangup is. It doesn't appear that I'm getting errors in my validation, so I'm left a little bit confused.

Это было полезно?

Решение

Looks like you have uniqueness validation on name and email address. There is already a record with either the same name or email address. Update the other record with different name and email address and you should be fine

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top