Reputation: 13
I have to give a registration facility to a store. There the user have to enter name, password,confirm password. But when I am trying to enter a different password for confirmation, it isn't showing any error and user is successfully getting registered. Here is the code in UserMOdel
class User < ActiveRecord::Base
attr_accessible :name, :password_digest, :password, :password_confirmation
validates :name, :presence => true, :uniqueness => true
validates :password, :presence =>true, :confirmation =>true
validates_confirmation_of :password
has_secure_password
end
And my code in views/users/_form.html.erb is as follows..
<div class="depot_form">
<%= form_for(@user) do |f| %>
<% if @user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% @user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<fieldset>
<legend>Enter User Details</legend>
<div >
<%= f.label :name %>:
<%= f.text_field :name, :size => 40 %>
</div>
<div>
<%= f.label :password, 'Password' %>:
<%= f.password_field :password, :size => 40 %>
</div>
<div>
<%= f.label :password_confirmation, 'Confirm Password' %>:
<%= f.password_field :password_confirmation, :size => 40 %>
</div>
<div>
<%= f.submit %>
</div>
</fieldset>
<% end %>
</div>
I have gone through various solutions and modifications, but in vain... Any help please..
Upvotes: 1
Views: 2220
Reputation: 540
You should use devise gem for this use case.
and for devise use this attributes
devise :database_authenticatable, :registerable, :confirmable, :recoverable,
:rememberable, :trackable, :validatable
To read more about Devise Click here
Upvotes: 0
Reputation: 13014
Add this validation too:
validates :password_confirmation, :presence =>true
A presence check is still required for confirmation attribute.
Read - http://guides.rubyonrails.org/active_record_validations_callbacks.html#confirmation
Upvotes: 5