/ Authentication / Allow users to edit account without providing a password (DEVISE)

First of all, there is a short simple tutorial from Devise Wiki which you could take a look here

I have honestly tried it and it doesn’t work. And on this article I will show you the solution that works for me and also completely cater all requirements for my app.

1. Make sure you permitted all the variables that associated with your user

You could either do one of the following:

Edit application_controller.rb

before_filter :configure_permitted_parameters, if: :devise_controller?

protected
def configure_permitted_parameters
  devise_parameter_sanitizer.for(:sign_up) << :first_name << :last_name << :email << :password << :password_confirmation 
  devise_parameter_sanitizer.for(:account_update) << :first_name << :last_name << :email << :password << :password_confirmation 
end

 

Edit registration_controller.rb

def sign_up_params
  devise_parameter_sanitizer.for(:sign_up) << :first_name << :last_name << :email << :password << :password_confirmation 
end

def account_update_params
  devise_parameter_sanitizer.for(:account_update) << :first_name << :last_name << :email << :password << :password_confirmation 
end

 

2. Put this Update method in registration_controller.rb

# For Rails 4
account_update_params = devise_parameter_sanitizer.sanitize(:account_update)

# required for settings form to submit when password is left blank
if account_update_params[:password].blank?
  account_update_params.delete("password")
  account_update_params.delete("password_confirmation")
end

@user = User.find(current_user.id)

@update = update_resource(@user, account_update_params)
if @update 
  # Sign in the user bypassing validation in case their password changed
  sign_in @user, :bypass => true
end

respond_to do |format|
  format.html
  format.js
end

 

 

 

No video for this post.

by ryzalyusoff