Change Devise Password Manually

devise, ruby-on-rails

Solution

You don't need to set the password in that way, just set the password and password confirmation and devise will take care of the rest, don't worry, it won't be stored in plain text, devise automatically converts the plain text into hashed password before saving it to the db

def update
  user = User.find(params[:id])
  password = params["user"].delete("user_password")
  if password && !password.blank?
      user.password = user.password_confirmation = password
  end

  if user.save
    render json: {company: user}, status: 200
  else
    render json: {message: "Problem updating company"}, status: 500
  end
end

Problem

Ok what i have is an API that i built for our customer service team. We can create accounts and users and everything works great. But when i built it to Change the user passwords it changes them but will not login. to create an account i have this coding: ``` def create params["user"].delete("user_id") // this is being sent in from the api.. params["user"]["encrypted_password"] = Password.new(Password.create(params["user"]["password"])) params["user"]["password"] = Password.new(Password.create(params["user"]["password"])) user = User.create(params["user"]) if user render json: {company: user}, status: 200 else render json: {message: "Problem creating company"}, status: 500 end end ``` that works perfectly but when i go to update a user the same: ``` params["user"]["encrypted_password"] = Password.new(Password.create(password)) params["user"]["password"] = Password.new(Password.create(password)) ``` Does not work correctly ``` def update user = User.find(params[:id]) password = params["user"].delete("user_password") if password && !password.blank? puts password params["user"]["encrypted_password"] = Password.new(Password.create(password)) params["user"]["password"] = Password.new(Password.create(password)) end if user.update_attributes(params["user"]) render json: {company: user}, status: 200 else render json: {message: "Problem updating company"}, status: 500 end end ``` It will not log-in after changeing

Original source