我正在阅读一个Rails教程,在一个关于用户验证的章节中,我一直收到一个错误,告诉我在编辑/创建用户时,我的密码确认不能是空的。我查看了以前的答案,看起来人们使用的是attr_accessible,它被从轨道上拉了出来。我是一个完全的rails/web新闻,所以我不知道如何继续。视图在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发布于 2013-10-23 17:27:21
由于您使用rails 4,请查看您的强params设置,并确保允许使用password_confirmation。
这是rails 4:http://edgeapi.rubyonrails.org/classes/ActionController/StrongParameters.html中的一个新特性
https://stackoverflow.com/questions/19547452
复制相似问题