rails 4密码确认ca

Noo*_*ion 7 ruby ruby-on-rails password-confirmation

我正在浏览一个Rails教程,在一个关于用户验证的部分,我一直收到一个错误,告诉我在编辑/创建用户时我的密码确认不能为空.我查看了之前的答案,看起来人们使用的是attr_accessible,它被拉出铁轨.我是一个总计rails/web dev newb所以我不知道如何继续.这个观点是在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
Run Code Online (Sandbox Code Playgroud)

视图

= 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
Run Code Online (Sandbox Code Playgroud)

Jon*_*ias 10

由于您使用的是rails 4,请查看强大的params设置,并确保允许使用password_confirmation.

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