【问题标题】:rails 4 password confirmation carails 4密码确认ca
【发布时间】:2013-11-02 01:40:13
【问题描述】:

我正在阅读 Rails 教程之一,在有关用户验证的部分中,我不断收到错误消息,告诉我在编辑/创建用户时我的密码确认不能为空。我查看了以前的答案,看起来人们使用了 attr_accessible,它被拉出了轨道。我是一个完全的 rails/web dev 新手,所以我不知道如何继续。该视图在 HAML 中,如果这是不好的做法,请见谅。

型号

class User < ActiveRecord::Base

  before_save { self.email = email.downcase }
  attr_accessor :name, :email
  validates_confirmation_of :password
  has_secure_password


  validates :name, presence: true, length:{ maximum: 16 }

  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

  validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
    uniqueness: { case_sensitive: false }

  validates :password, length: { minimum: 6 }


  has_one :profile
  has_many :posts


  end

查看

= form_for(@user) do |f|
  - if @user.errors.any?
    #error_explanation
      %h2
        = pluralize(@user.errors.count, "error")
        prohibited this username from being saved:
      %ul
        - @user.errors.full_messages.each do |msg|
          %li= msg

  .field
    = f.label :name
    = f.text_field :name   

  .field
    = f.label :email
    =f.text_field :email       

  .field
    = f.label :password
    = f.password_field :password

    = f.label :password_confirmation
    = f.password_field :password_confirmation

  .actions
    = f.submit

【问题讨论】:

  • 这个错误到底出现在哪里? (在运行测试或您自己测试时)如果后者可以发布控制器代码。

标签: ruby-on-rails ruby password-confirmation


【解决方案1】:

自从您使用 rails 4 以来,请查看您的强参数设置并确保允许 password_confirmation。

这是 rails 4 中的一个新功能: http://edgeapi.rubyonrails.org/classes/ActionController/StrongParameters.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-05
    • 2011-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-08
    • 2022-10-21
    • 1970-01-01
    相关资源
    最近更新 更多