Ruby on Rails如何使用password_digest和表单更新用户密码

问题描述 投票:0回答:1

我具有以下文件来更新用户详细信息。他们目前可以处理除密码以外的所有内容。所有逻辑均正常工作,我已使用puts检查是否输入了正确的逻辑部分,但是似乎在调用@user.update(user_params)时密码永远不会在最后更新。

为了使它正常工作,我必须在UserController的密码逻辑中添加以下两行,鉴于我进一步调用@user.update(user_params)似乎应该符合逻辑,因此应该使用提供的用户参数来更新用户。任何意见,建议或评论将不胜感激。

@user.password = params[:user][:password]
@user.save

我未使用任何Gems等进行身份验证,并且我的用户表如下所示

create_table "users", force: :cascade do |t|
    t.string "username"
    t.string "password_digest"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.string "title"
    t.string "firstName"
    t.string "surname"
    t.index ["username"], name: "index_users_on_username", unique: true
end

UserController

class UsersController < ApplicationController

  skip_before_action :authorized, only: [:new, :create]
  before_action :set_user, only: [:update]
  def new
    @user = User.new
  end

  def create
    @user = User.create(params.require(:user).permit(:username, :password))
    session[:user_id] = @user.id
    redirect_to '/welcome'
  end

  def update
    errorMessage = ''
    if current_user.username != params[:user][:username]
      if User.find_by(username: params[:user][:username]).nil?
      else
        errorMessage += "Username is already taken"
      end
    else
    end
    if !params[:user][:password].blank?
      if @user.authenticate(params[:user][:password_confirmation])
      else
        errorMessage += "Confirmation password is incorrect"
      end
    else
    end

    if !errorMessage.blank?
      redirect_to account_path, notice: errorMessage
    else
      @user.update(user_params)
      redirect_to account_path
    end
  end

  private 
  def user_params
    params.require(:user).permit(:username, :title, :firstName, :surname,:password, :password_confirmation)
  end
  def set_user
    @user = current_user
  end
end

edit.html.erb

<p id=”notice”><%= notice %></p>
<%= form_for current_user do |f|%>
    <div class="form-group row col-md-12">
        <%= f.label :username, class:"col-sm-2 col-form-label"%><br>
        <%= f.text_field :username, class:"form-control col-sm-10" %>
    </div>

    <div class="form-group row col-md-12">
        <div class="col-md-2">
            <%= f.label :title%><br>
            <%= f.text_field :title, class:"form-control" %>
        </div>
        <div class="col-md-5">
            <%= f.label :firstName, "First Name" %><br>
            <%= f.text_field :firstName, class:"form-control" %>
        </div>
        <div class="col-md-5">
            <%= f.label :surname %><br>
            <%= f.text_field :surname, class:"form-control" %>
        </div>
    </div>

    <div class="form-group row col-md-12">
        <div class="col-md-6">
            <%= f.label :password_confirmation, "Current Password"%><br>
            <%= f.password_field :password_confirmation, class:"form-control" %>
        </div>
        <div class="col-md-6">
            <%= f.label :password, "New Password"%><br>
            <%= f.password_field :password, class:"form-control" %>
        </div>
        <small id="passwordHelpBlock" class="form-text text-muted col-md-12">
            To update your password, please confirm your current password.
        </small>
    </div>

    <%= f.submit "Submit" ,class: "btn btn-primary"%>
<% end %>

用户模型

class User < ApplicationRecord
    has_secure_password
end
ruby-on-rails bcrypt ruby-on-rails-6
1个回答
0
投票

打开app / models / user.rb并将has_secure_password方法添加到您的User类。 has_secure_password和password_digest在bcrypt中可以一起工作,因此都需要同时存在两者才能使其工作。

© www.soinside.com 2019 - 2024. All rights reserved.