我需要在rails视图中的form_for中为两个输入设置相同的值
.form-box
= form_for(resource, as: resource_name, url: registration_path(:user), html: { class: "guest-form", autocomplete: "off" }) do |f|
= f.text_field :username, placeholder: "Username", class: "input"
= f.password_field :password, class: "input", style: 'visibility: hidden'
= f.submit "Enter", class: "signup"
我需要设置:密码相同的值:username。是否可以在不使用jquery或overriding devise的情况下完成它?例如,使用value:attribute?
答案 0 :(得分:1)
在您的用户模型中,您可以:
class User
...
before_validation :init_password, on: :create
private
def init_password
self.password = username
end
end
删除表单的字段。
这必须做到这一点。
答案 1 :(得分:0)
您只需传递value
属性:
.form-box
= form_for(resource, as: resource_name, url: registration_path(:user), html: { class: "guest-form", autocomplete: "off" }) do |f|
= f.text_field :username, placeholder: "Username", class: "input"
= f.password_field :password, class: "input", style: 'visibility: hidden', value: f.object.username
= f.submit "Enter", class: "signup"