关于问题,答案是使用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 } %>
关于恢复密码值的次要主题,经过调查,它更像是一个额外的安全漏洞。真正的安全性是通过使用 HTTPS 来完成的,或者通过使用 javascript 对客户端进行密码散列处理,这样服务器甚至都不知道密码。
例如,即使像https://github.com/join这样大的网站似乎也按照我的方式进行操作(填写密码但输入无效的电子邮件,然后看到密码被填写为纯 HTMLvalue
元素)。
对应的路由和控制器可能如下所示:
路线.rb:
devise_for :users, :controllers => {registrations: 'users/registrations' }
注册控制器.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