validates_confirmation_of :password
在我提交表单时不起作用。即使密码确认不相同,也会注册用户。
我写了与guide相同的东西。在研究过程中,我没有发现任何可以解释这一点的东西。
您知道发生了什么事,我该如何解决?
这是我的代码:
我的观点:
<%= form_for User.new do |f| %>
<div class="row">
<div class="col-md-6">
<%= f.label :Prénom %> :
<%= f.text_field :first_name, class: "form-control" %><br />
</div>
<div class="col-md-6">
<%= f.label :Nom %> :
<%= f.text_field :last_name, class: "form-control" %><br />
</div>
</div>
<div class="row">
<div class="col-md-6">
<%= f.label :Pseudo %> :
<%= f.text_field :user_name, class: "form-control"%><br />
</div>
<div class="col-md-6">
<%= f.label :Email %> :
<%= f.email_field :email, class: "form-control" %><br />
</div>
</div>
<div class="row">
<div class="col-md-6">
<%= f.label :Mot_de_passe %> :
<%= f.password_field :password, class: "form-control" %><br />
</div>
<div class="col-md-6">
<%= f.label :Confirmation_mot_de_passe %> :
<%= f.password_field :password_confirmation, class: "form-control" %><br />
</div>
</div>
<%= f.label :Club_favori %> :
<%=f.select(:club) do%>
<%= options_from_collection_for_select(Club.all,:id ,:name) %>
<% end %>
<br>
<%= f.submit class: "btn btn-primary" %>
<% end %>
我的模特:
class User < ActiveRecord::Base
has_merit
has_secure_password
has_many :comments
validates_length_of :password, minimum: 5, too_short: 'please enter at least 5 characters', on: :create
validates_presence_of :user_name, :message => 'Vous devez remplir tout les champs.', on: :create
validates_uniqueness_of :user_name, :case_sensitive => false, :message => "Ce pseudo n'est pas disponible.", on: :create
validates_confirmation_of :password, on: :create
end
我的控制器:
class UsersController < ApplicationController
def create
@user = User.new(user_params)
if @user.save
@user.add_badge(1)
session[:user_id] = @user.id
redirect_to '/feed'
else
redirect_to '/signup', flash: {error_message: @user.errors}
end
end
private
def user_params
params.require(:user).permit( :user_name, :first_name, :last_name, :email, :password, :sash, :club)
end
def new
@user = User.new
@clubs =Club.all
end
end
预先感谢您的帮助。
答案 0 :(得分:0)
在输入表单时,您将覆盖当前的@user
。
只需更改:
<%= form_for User.new do |f| %>
到
<%= form_for @user do |f| %>