加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails-4 – 在Rails 4中使用has_secure_password时编辑/

发布时间:2020-12-17 02:22:08 所属栏目:百科 来源:网络整理
导读:我正在使用Michael Hartl的 Ruby on Rails tutorial制作我自己的用户身份验证系统.创建新用户时一切正常.当尝试更新用户时,事情不再有意义.我正在使用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-zd-.]+.[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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读