如何在密码字段中添加图标以在 rails 中显示和隐藏密码

How to add an icon in password field to show & hide password in rails

我的代码:

div.form-group.col-md-12
              label =t('.password')
              = f.text_field :password,
                      required: true,
                      label: false,
                      type: 'password',
                      placeholder: 'Password',
                      input_html: {autocomplete: "new-password"},
                      class: 'form-control input-sm form-control-sm'

此表单域用于输入密码。我想在输入字段框的右侧添加一个眼睛按钮,它将切换密码显示和隐藏。

 $("body").on('click', '.toggle-password', function() {
      $(this).toggleClass("fa-eye fa-eye-slash");
      var input = $("#pass_log_id");
      if (input.attr("type") === "password") {
        input.attr("type", "text");
      } else {
        input.attr("type", "password");
      }
    
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span toggle="#password-field" class="fa fa-fw fa-eye field_icon toggle-password">Show/Hide</span>
<input type="password" id="pass_log_id"/>

=>代表html可以使用此代码

%script{:src => "https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"}
%span.fa.fa-fw.fa-eye.field_icon.toggle-password{:toggle => "#password-field"} Show/Hide
%input#pass_log_id{:type => "password"}/