Rails simple_form记住密码输入

Sil*_*lex 1 ruby-on-rails simple-form

铁路的simple_form宝石:

如果我有,= f.input :password, :required => true那么无论何时我提交表单并且它包含错误,我输入的密码都会在页面重新加载时消失.如果我使用= f.input :password, :required => true, :as => :text它所有工作都像预期的那样,所以显然对密码字段有不同的处理方式.

如何记住密码字段的值?

编辑:有些人似乎不明白我在谈论用之前输入的值重新显示相同的表格.这个问题与数据库无关,事实上在这种情况下甚至没有任何东西保存在数据库中.对于PHP人来说,这相当于<input name="pass" type="password" value="<?= $_POST['pass']; ?>">

Sil*_*lex 5

关于这个问题,答案是使用 input_html: { value: @user_password }

像这样:

<%= f.input :password, :required => true, input_html: { value: @user_password } %>
<%= f.input :password_confirmation, :required => true, input_html: { value: @user_password } %>
Run Code Online (Sandbox Code Playgroud)

关于恢复密码值的第二个主题,在调查后,它真的更像是一个额外的安全凹凸.真正的安全性是通过使用HTTPS完成的,或者使用javascript以客户端方式完成密码哈希,服务器甚至不知道密码.

例如,即使像https://github.com/join这样大的网站似乎也按照我的方式进行(填写密码但输入无效的电子邮件,并将密码视为纯HTML value元素填写).

相应的路由和控制器可能如下所示:

routes.rb:

devise_for :users, :controllers => {registrations: 'users/registrations' }
Run Code Online (Sandbox Code Playgroud)

registrations_controller.rb:

class Users::RegistrationsController < Devise::RegistrationsController
  # build_resource is called inside the new and create methods
  def build_resource(*args)
    if params[:user]
      # So that the password isn't blanked out when the other validations fail. User shouldn't have to retype the password.
      @user_password = params[:user][:password]
    end
    super
  end
end
Run Code Online (Sandbox Code Playgroud)