Updating a user profile without having to enter a password and confirmation every time

I am trying to create a new page called edit_profile for my User model so that the user can edit his profile (line). I follow http://railscasts.com/episodes/41-conditional-validations

Here is the form (edit_profile.html.erb):

  <%= form_for @user, :html => { :multipart => true } do |f| %>

  <div class="field">
    <%= f.label :profile %><br/>
    <%= f.text_area :profile, :class => "round" %><br />
  </div>

  <div class="actions">
    <%= submit_tag "update", :id => "updateSubmit" %>
  </div>
<% end %>

The problem I am facing is that I have a password check and password confirmation. When I load the edit_profile view, I continue to receive this message Password is too short (minimum is 6 characters)even before I try to submit a new profile.

Here is my users_controller.rb:

def edit_profile
  @user = current_user
  @user.updating_password = false
  @user.save
  @title = "Edit profile"
end

def update
  @user = User.find(params[:id])
  if @user.update_attributes(params[:user])
    flash[:success] = "Account updated."
    redirect_to @user
  else
    @title = "Edit user"
    render 'edit'
  end
end

How to get around password verification when I just want to change the profile attribute in a user model?

Thank!

Other relevant information:

user.rb

class User < ActiveRecord::Base
    attr_accessor :password, :updating_password
    attr_accessible :name, :email, :password, :password_confirmation, :photo, 
                    :profile
    before_save :downcase_fields
    before_save :encrypt_password
    validates_presence_of :password, :if => :should_validate_password?
    validates_confirmation_of :password, :if => :should_validate_password?      
    def should_validate_password?
      updating_password || new_record?
    end

    validates :name, :presence => true,
              :name_format => true,  
              :uniqueness => { :case_sensitive => false }          
    validates :email, :presence => true, 
              :email_format => true,
              :uniqueness => { :case_sensitive => false }
    validates :password, 
               #:presence => true,
              #:confirmation => true,
              :length => { :within => 6..40 }
    validates :profile,  :length => { :maximum => 160 }
end
+3
5

, , , .

password password_confirmation, password_confirmation . :.

class UsersController < ApplicationController
  before_filter :skip_password_attribute, only: :update

  ...

  def update
    ...
  end

  private

  def skip_password_attribute
    if params[:password].blank? && params[:password_validation].blank?
      params.except!(:password, :password_validation)
    end
  end
end
0

(1) , @user , form_for , @user , . , : url, , :.

<%= form_for @user, :url => (@user.new_record? ? users_path : user_path(@user),
            :html => (@user.new_record? ? { :multipart => true, :method => :post } : { :multipart => true, :method => :put } do |f| %>

(2)

class User
  validate :validate_password_length

  def validate_password_length
    !new_record? || password.length >= 8
  end
end

, , - . :

class User
  validate :validate_password_length

  def validate_password_length
    !password_changed? || password.length >= 8
  end
end
+1

auth . (https://github.com/plataformatec/devise/blob/master/lib/devise/models/validatable.rb):

validates_presence_of     :password, :if => :password_required?
validates_confirmation_of :password, :if => :password_required?
validates_length_of       :password, :within => 6..128, :allow_blank => true

protected
  def password_required?
    !persisted? || !password.nil? || !password_confirmation.nil?
  end
0

, , - , .

, - , .

-, :on => :create password password_confirmation. , , , . , before_update . User :

class User < ActiveRecord::Base
  ...
  before_update :check_password
  ...
  validates :password, presence: true,
                       length: { minimum: 6 },
                       on: :create

  validates :password_confirmation, presence: true,
                                    on: :create
  ...
  private
    ...
    def check_password
      is_ok = self.password.nil? || self.password.empty? || self.password.length >= 6

      self.errors[:password] << "Password is too short (minimum is 6 characters)" unless is_ok

      is_ok # The callback returns a Boolean value indicating success; if it fails, the save is blocked
    end
end

As noted in the comment above, the result of this method determines whether there will be an attempt to save. To prevent the user from dropping back into the edit form without reporting an error, I add one. [:password]indicates in which field the error is indicated.

0
source

You just need to add :on => :createin validate.

-1
source

All Articles