DEV Community

Sam Markham
Sam Markham

Posted on • Originally published at sharkham.github.io

1 1

Changing attributes on a user in Rails without having to re-enter password

(originally published on May 5, 2020)

This is something that first came up for me during my Rails project for Flatiron, and I really wish I had figured it out then, but it feels fitting to have finally solved this mystery in my last weeks in the program.

The issue is: you've set up a project with Rails. You include a user model, and add validations for the user's password. Then you want to make something about the user editable. A bio attribute, or something like that. You get all of the edit functionality set up, test out submitting it, and, error! A password hasn't been entered!

This makes sense, because you didn't enter a password, and you don't want the user to have to enter a password to make changes to their profile when they're already logged in either! But because of the way the password validation is set up, Rails requires a password to be there to save the user.

To fix this, include this line in your user model:

  validates :password, length: { minimum: 5, wrong_length: "Password must be at least 5 characters." }, if: :password
Enter fullscreen mode Exit fullscreen mode

The first bit is specific to my project, though having a minimum password length is probably a good idea anyway, but the last part is what does the job here: if: :password. This tiny bit of code means that if a password isn't included in the changed user object being submitted, then Rails won't worry about validating it.

Finally, if you're dealing with strong params, include this in whatever controller is dealing with updating the user:

if user_params[:password].blank?
  user_params.delete(:password)
end
Enter fullscreen mode Exit fullscreen mode

This should work for projects with Rails as an API (my current project) as well as projects only using Rails (the project I may go back and add this functionality to!).

I hope this helps anyone else struggling with this issue as I was!

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (1)

Collapse
 
sharkham profile image
Sam Markham

Thanks Mark! And you're very welcome! :)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay