首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails password_confirmation不工作

Rails password_confirmation不工作
EN

Stack Overflow用户
提问于 2017-08-15 23:51:56
回答 1查看 1.4K关注 0票数 1

我需要帮助我的用户注册表单,以验证密码和password_confirmation完全匹配。

当前,表单将传递输入到数据库中的密码摘要字段的数据,并将接受password_confirmation字段中的任何输入,并且不会出现任何错误。

我的代码如下;

模型- user.rb

代码语言:javascript
复制
class User < ApplicationRecord

    before_save { self.email = email.downcase }
    #attr_accessible :user_name, :email
    validates_confirmation_of :password
    has_secure_password

    validates :user_name, presence: true, length: { maximum: 25 }
    VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
    validates :email, presence: true, length: { maximum: 255 }, format: { with: VALID_EMAIL_REGEX }
    validates :password, presence: true, confirmation: true, length: { minimum: 6 }

    has_many :trips
    has_many :countries, through: :trips

end

视图- new.html.erb

代码语言:javascript
复制
<div class="container">
  <h1 class="text-center" style="margin-top: 10px;" >Sign up</h1>
  <div class="row">
    <div class="col-md-6 offset-md-3 ">
      <%=form_for(@user) do |f| %>
      <%= render 'shared/error_messages' %>
      <div class="form-group">
        <%= f.label :user_name, "Username" %>
        <%= f.text_field :user_name, class: "form-control" %>
      </div>

      <div class="form-group">
        <%= f.label :email %>
        <%= f.email_field :email, class: "form-control" %>
      </div>

      <div class="form-group">
        <%= f.label :password %>
        <%= f.password_field :password, class: "form-control" %>
      </div>

      <div class="form-group">
        <%= f.label :password_confirmation, "Password Confirmation" %>
        <%= f.password_field :password_confirmation, class: "form-control" %>
      </div>

      <div class="form-group">
        <%= f.submit "Create an account", class: 'form-control btn btn-primary' %>
      </div>
      <% end %> 
    </div>
  </div>
</div>

我的宝石文件中也有bcrypt -v 3.1.7。如果需要任何其他信息,请告诉我,我很乐意提供。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-16 00:24:04

我认为您没有得到password_confirmation,为此您应该添加

代码语言:javascript
复制
validates :password_confirmation, presence: true

此外,在您的控制器,您应该白名单:password_confirmation到您的许可证部分。

只有当password_confirmation不是零时,才会验证确认。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45703141

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档