编辑密码字段不会在表单中显示-Rails 6

时间:2019-09-27 12:57:53

标签: ruby-on-rails ruby

我正在尝试通过编辑表单来编辑密码,因此我希望它显示磁盘字符,以作为其从数据库中提取密码的证明。但是,在打开我的编辑用户页面时,密码丢失并且不会保存数据库中的更新。

这是我的表格:

          <div class="row">

            <div class="col-lg-12">
              <div class="card">
                <div class="card-header d-flex align-items-center">
                  <h4>Edit user</h4>
                </div>
                <div class="card-body">


               <%= form_for @user, :html => { class: "form-horizontal", role: "form" } do |f| %>
                    <div class="form-group row">
                      <label class="col-sm-2 form-control-label">
                           <%= f.label :name %>
                      </label>
                      <div class="col-sm-10">
                          <%= f.text_field :name, class: "form-control", placeholder: "Name..", autofocus: true %>
                      </div>
                    </div>

                    <div class="form-group row">
                      <label class="col-sm-2 form-control-label">
                           <%= f.label :username %>
                      </label>
                      <div class="col-sm-10">
                          <%= f.text_field :username, class: "form-control", placeholder: "Username..", autofocus: true %>
                      </div>
                    </div>

                    <div class="form-group row">
                      <label class="col-sm-2 form-control-label">
                           <%= f.label :email %>
                      </label>
                      <div class="col-sm-10">
                          <%= f.email_field :email, class: "form-control", placeholder: "Email..", autofocus: true %>
                      </div>
                    </div>


                    <div class="form-group row">
                      <label class="col-sm-2 form-control-label">
                           <%= f.label :password %>
                      </label>
                      <div class="col-sm-10">
                          <%= f.password_field :password, class: "form-control", placeholder: "Password..", autofocus: true %>
                      </div>
                    </div>


                    <div class="line"></div>
                    <div class="form-group row">
                      <div class="col-sm-4 offset-sm-2">

                        <%= link_to "Cancel", users_path, class: "btn btn-secondary" %>
                        <%= f.submit "Update", class: "btn btn-primary" %>
                      </div>
                    </div>
                 <% end %>
                </div>
              </div>

这是该特定方法的控制器:

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

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

    if @user.update(user_params)
      flash[:notice] = "User was successfully created."
      redirect_to user_path(@user)
    else
      render 'edit'
    end
  end 

要在编辑时以磁盘字符显示密码并能够更改密码,我想念什么?在同一编辑表单上更新密码?

1 个答案:

答案 0 :(得分:0)

尝试设置密码字段的值。有关密码字段,请参阅此文档。 https://apidock.com/rails/ActionView/Helpers/FormHelper/password_field

<div class="form-group row">
    <label class="col-sm-2 form-control-label">
        <%= f.label :password %>
        </label>
    <div class="col-sm-10">
        <%= f.password_field :password, value: f.password, class: "form-control", placeholder: "Password..", autofocus: true %>
    </div>
</div>