在Rails 4中使用has_secure_password时编辑/更新用户不工作

时间:2014-02-17 21:12:49

标签: ruby-on-rails-4

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

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

  

密码的存在验证及其确认   由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, presence: true, 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>

1 个答案:

答案 0 :(得分:3)

由于此操作是update而非create,因此验证可能无法正常运行。

来自ActiveModel::SecurePassword

  

自动添加验证密码创建,密码确认(使用password_confirmation属性)。

来自 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