我正在使用Desive for Rails.在默认注册过程中,Desive要求用户键入两次密码以进行验证和身份验证.我怎样才能禁用它?

推荐答案

要禁用密码确认you can simply remove the 100 field from the registration form.,则无需完全确认密码!

  1. 如果没有,生成设计视图:rails g devise:views
  2. 移除app\views\devise\registrations\new.html.erb中的password_confirmation部分

一种方法是:

<%# Disable password confirmation so that the user doesn't have to enter a password twice %>
<% if false %>
  <div class="field">
    <%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation, autocomplete: "new-password" %>
  </div>
<% end %>

这项工作的原因在于设计来源:

module Devise
  module Models
    module Validatable


      def self.included(base)

        base.class_eval do
          #....SNIP...
          validates_confirmation_of :password, :if => :password_required?
        end
      end

      #...SNIP...

      def password_required?
        !persisted? || !password.nil? || !password_confirmation.nil?
      end
    end
  end
end

请注意,只有当password_required?返回true时才会触发验证,如果password_confirmation字段为nil,则password_required?将返回false.

因为如果表单中存在password_confirmation字段,它将始终作为空字符串包含在参数散列中,如果保留为空,则会触发验证.但是,如果从表单中删除输入,参数中的password_confirmation将是nil,因此不会触发验证.

Ruby-on-rails相关问答推荐

参数数量错误(给定1个,预期为0个;必需关键字:IO、文件名)-活动存储

在Ruby on Rails中获取堆栈级别太深错误(&Q)

Ubuntu Webhooks无法读取安装的正确RVM Ruby

NoMethodError:Puma::Events:Class 的未定义方法字符串

Rails 'includes' 和 'where' 与 belongs_to 和 has_many 关联

如何使用默认的 Rails 记录器记录 Ruby 异常的整个回溯?

RSpec 设置会话对象

如何在 Linux 上使用 Ruby 2.0 改进独角兽进程之间的内存共享

HABTM 关系 - 如何根据关联模型的属性找到记录

禁用 RVM 或使用没有 RVM 安装的 Ruby?

了解 Rails 验证:allow_blank 有什么作用?

可能在rails中有多态has_one关系?

Ruby gem 命名约定

如何在 Rails 中测试助手?

Rails Activeadmin - 自定义关联 Select 框

非持久 ActiveRecord 模型属性

为什么 Google Oauth 在我的 Rails 应用程序中返回无效的 redirect_urI?

Rails 3:乘客找不到Bundle 程序安装的 git gem

Rails:您已经激活了 rake 10.3.1,但您的 Gemfile 需要 rake 10.2.2 (Gem::LoadError)

错误:删除表违反外键约束.密钥 id 仍然从表中引用(很多)