7

我正在阅读其中一个 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
4

1 回答 1

10

由于您使用的是 rails 4,请查看您的强参数设置并确保允许 password_confirmation。

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

于 2013-10-23T17:27:21.647 回答