【发布时间】:2014-06-09 13:38:15
【问题描述】:
我在我的User 模型中使用has_secure_password 行。它看起来像这样:
class User < ActiveRecord::Base
before_create :create_remember_token
validates :name, presence: true, length: {minimum: 3, maximum: 22},
uniqueness: true
#validates :password, presence: true, length: {minimum: 6, maximum: 22}
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+\.[a-z]+\z/i
validates :email, :allow_blank => true, format: { with: VALID_EMAIL_REGEX }
has_secure_password
validates :password, length: {minimum: 6}
在我的Users 新视图中,我有这个:
<h1>Sign up</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(@user) do |f| %>
<%= f.label :name, 'Username' %>
<%= f.text_field :name %>
<%= f.label :email, 'Email (Optional)' %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation %>
<%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>
根据我的阅读,has_secure_password 位应该“自动”创建和验证密码确认内容。但是,当我使用不匹配的password 和password_confirmation 创建用户时,它不会抛出任何东西。
任何想法我哪里出错了?
EDIT: forgot this params.require(:user).permit(:name, :email, :password,
line right here-----> :password_confirmation)
【问题讨论】:
-
可以发
users_controller.rb的代码吗?特别是user_paramspart. -
@ekremkrc 哦,天哪。我刚刚发现了问题。我的 user_param 位丢失了:password_confirmation。感谢您为我指明正确的方向
标签: ruby-on-rails ruby-on-rails-4 activemodel