Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails、更新方法和验证_Ruby On Rails_Ruby On Rails 3_Ruby On Rails 3.1 - Fatal编程技术网

Ruby on rails Rails、更新方法和验证

Ruby on rails Rails、更新方法和验证,ruby-on-rails,ruby-on-rails-3,ruby-on-rails-3.1,Ruby On Rails,Ruby On Rails 3,Ruby On Rails 3.1,我有一个问题,我需要一个如何修复我的更新方法的想法。我有一个管理面板,我可以创建用户。此表单包括名称、邮件、密码、重复密码字段,并且工作正常。然后我想有一个所有用户的列表,并编辑这些我想要的用户。问题是我想编辑注册表单中未包含的部分信息,默认值为空。在编辑模式下,我的表单有两个新字段-备注和缺勤。当我更改这些字段并调用update method时,我会看到一条消息,密码和重复密码不匹配,这是注册中的验证,但我在编辑模式下没有这些文件。我怎样才能解决这个问题。这是我代码的一部分: class Us

我有一个问题,我需要一个如何修复我的更新方法的想法。我有一个管理面板,我可以创建用户。此表单包括名称、邮件、密码、重复密码字段,并且工作正常。然后我想有一个所有用户的列表,并编辑这些我想要的用户。问题是我想编辑注册表单中未包含的部分信息,默认值为空。在编辑模式下,我的表单有两个新字段-备注和缺勤。当我更改这些字段并调用update method时,我会看到一条消息,密码和重复密码不匹配,这是注册中的验证,但我在编辑模式下没有这些文件。我怎样才能解决这个问题。这是我代码的一部分:

class UsersController < ApplicationController
  def edit
    @user = User.find(params[:id])
    @title = "Edit user"
  end

def update
  @user = User.find(params[:id])
  if @user.update_attributes(params[:user])
    flash[:success] = "Profile updated."
    redirect_to @user
  else
    @title = "Edit user"
    render 'edit'
  end
end


class User < ActiveRecord::Base
  attr_accessor :password
  attr_accessible :name, :email, :password, :password_confirmation
  validates :name,  :presence => true,
                :length   => { :maximum => 50 }
  validates :email, :presence => true

  email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

  validates :email, :presence   => true,
            :format     => { :with => email_regex },
            :uniqueness => true

  validates :password, :presence     => true,
            :confirmation => true,
            :length       => { :within => 6..40 }

  before_save :encrypt_password

  def has_password?(submitted_password)
    encrypted_password == encrypt(submitted_password)
  end

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

  def self.authenticate_with_salt(id, cookie_salt)
    user = find_by_id(id)
    (user && user.salt == cookie_salt) ? user : nil
  end

  private

  def encrypt_password
    self.salt = make_salt unless has_password?(password)
    self.encrypted_password = encrypt(password)
  end

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

  def make_salt
    secure_hash("#{Time.now.utc}--#{password}")
  end

  def secure_hash(string)
    Digest::SHA2.hexdigest(string)
  end
end

当您创建用户时,密码存在的验证是正确的,但是一旦用户拥有加密密码,您就不希望强制它在将来的所有表单提交中都存在

Active record支持向验证添加条件,因此我建议在密码验证上设置一个条件,使其仅在用户对象没有加密密码时执行。相关的片段将是:

validates :password, :presence     => true,
        :confirmation => true,
        :length       => { :within => 6..40 },
        :if           => :needs_password?

def needs_password?
    encrypted_password.nil?
end

谢谢,它可以工作,但现在我收到一条消息,操作已完成,但在DB中我没有任何更改。我不知道这是否是原因,但您在加密密码上有一个before_save hook,因此您需要确保如果密码为空,它不会执行。