ruby on rails - Password Confirmation with has_secure_password -
i'm using line has_secure_password
in user
model. looks this:
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}
in user
s new view, have this:
<h1>sign up</h1> <div class="row"> <div class="col-md-6 col-md-offset-3"> <%= form_for(@user) |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 account", class: "btn btn-large btn-primary" %> <% end %> </div> </div>
from read, has_secure_password
bit supposed "automagically" create , validate password confirmation stuff. however, doesn't throw when create user non matching password
, password_confirmation
.
any ideas i'm going wrong?
edit: forgot params.require(:user).permit(:name, :email, :password, line right here-----> :password_confirmation)
from question i'm not sure if user created without match password , it's confirmation or if error messages aren't shown. i'll try answer both of these issues.
the form don't throw information errors because have no code view that. add piece of code @ begining of form show flash errors in form.
<%= form_for @user |f| %> <% if @user.errors.any? %> <div class="error_messages"> <h2>form invalid</h2> <ul> <% message in @user.errors.full_messages %> <li><%= message %></li> <% end %> </ul> </div> <% end %> ....
other thing might wrong here:
has_secure_password validates :password, length: {minimum: 6}
you validate minimum length of password, not it's presence. try change second line with: validates :password, presence: true, length: {minimum: 6}
if not work, make sure access password , password_confirmation fields in model ( rails 3) or controller (rails 4):
if use rails 3, code should added in model access parameters wan't allow in forms:
app/models/user.rb attr_accessible :name, :email, :password, :password_confirmation
and if use rails 4, should this:
app/controllers/users_controller.rb def user_params params.require(:user).permit(:name, :email, :password, :password_confirmation) end
hope help.
Comments
Post a Comment