ruby-on-rails-4 – 在Rails 4中使用has_secure_password时编辑/更新用户不工作

我正在使用Michael Hartl的 Ruby on Rails tutorial制作我自己的用户身份验证系统.创建新用户时一切正常.当尝试更新用户时,事情不再有意义.我正在使用has_secure_password方法.所以我让它担心取密码并加密它.但是当我更新时,会发生一些奇怪的行为.

重要的是我可以在不输入任何密码及其确认的情况下更新用户.但是如果我输入密码而没有确认,则需要确认.如果我只输入确认密码,则更新时没有错误.既然has_secure_paassword假设是在做那个部分,为什么在更新时不这样做呢?我没有在我的用户模型中添加的一件事是由于本教程中的这一行而进行了状态验证:

Presence validations for the password and its confirmation are
automatically added by has_secure_password.

我知道在这方面有类似的问题,但似乎都没有强制执行强参数,这是我猜测为什么它不适合我.我的一些代码如下.

用户控制器

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

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

    # if params[:user][:password].blank?
    #   render 'edit'
    #   return
    # end

    if @user.update(user_params)
        redirect_to @user,notice: 'User successfully updated'
    else
        render 'edit',notice: 'Failed to update profile.'
    end
end
def user_params
    params.require(:user).permit(
        :username,:email,:phone,:bio,:password,:password_confirmation
        )
end

用户模型

class User < ActiveRecord::Base

  before_save {self.email = email.downcase}
  has_secure_password
  before_create :create_remember_token

  validates :username,presence: true,length: {maximum: 255,minimum: 5},format: { with: /\A[a-zA-Z0-9]*\z/,message: "may only contain letters and numbers." },uniqueness: true

  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email,format: { with: VALID_EMAIL_REGEX }
  #... Other User stuff.

编辑表格

<div class="col-xs-8 col-xs-offset-2">
  <%= form_for(@user,:html => {:class => 'well'}) do |f| %>

    <div class="form-group">
      <%= f.label :username %><br />
      <%= f.text_field :username,class: 'form-control' %>
    </div>

    <div class="form-group">
      <%= f.label :phone %><br />
      <%= f.phone_field :phone,class: 'form-control' %>
    </div>


    <div class="form-group">
      <%= f.label :bio %><br />
      <%= f.text_area :bio,class: 'form-control' %> 
    </div>


    <div class="form-group">
      <%= f.label :email %><br />
      <%= f.email_field :email,class: 'form-control' %>
    </div>

    <div class="form-group">
      <%= f.label :password %><br />
      <%= f.password_field :password,class: 'form-control' %>
    </div>

    <div class="form-group">
      <%= f.label :password_confirmation %><br />
      <%= f.password_field :password_confirmation,class: 'form-control' %>
    </div>

    <div class="form-group">
      <%= f.submit "Update",class: 'btn btn-default' %>
    </div>
  <% end %>

</div>

解决方法

由于这是更新操作而不是创建,因此验证可能未在运行.

ActiveModel::SecurePassword开始:

Validations for presence of password on create,confirmation of password (using a password_confirmation attribute) are automatically added.

来自secure_password.rb的相关代码部分

def has_secure_password(options = {})
  ...
  if options.fetch(:validations,true)
    validates_confirmation_of :password,if: lambda { |m| m.password.present? }
    validates_presence_of     :password,:on => :create
    validates_presence_of     :password_confirmation,if: lambda { |m| m.password.present? }
  end
  ...
end

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...