Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Introduction to Ruby on Rails 7!
You have completed Introduction to Ruby on Rails 7!
Preview
Allow users to update their own profile information while keeping data secure. We’ll build the forms and routes needed for safe account edits, ensuring only the rightful owner can make changes.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Okay, so
in this video we're going to add this tab.
0:00
We're going to make it so that you
can actually edit the user's account.
0:02
So you're gonna be able to
update the password, and
0:06
you're also gonna be able
to update the email.com.
0:08
So the first thing that we're
going to do is go to the navbar,
0:11
because I haven't shown
you how to add this link.
0:13
So we're gonna go to views > layouts > and
navbar.html.erb.
0:15
Then up here, we should have a link to,
yeah, right here,
0:20
it says My account,
edit_user_registration_path.
0:24
This is the link that you need to add in
order to be able to edit your account.
0:28
So I'll just type this out with you.
0:32
So link_to, embedded ruby,
then My Account.
0:34
Then need to add the route,
0:39
which is edit_user_registration_path.
0:42
And then you need to add the class,
which is nav-link.
0:47
So save that file, refresh.
0:50
I've spelled it wrong, so I just
have to go back to it, registration,
0:52
there we go, refresh.
0:57
Now, as you can see, it's there.
0:58
Now what we want to do is
generate the device controllers.
1:01
So I'm gonna stop the server,
and I'm actually going to
1:06
run this command, so
rails g devise controllers users.
1:11
I believe that's the syntax,
but I'm not sure.
1:17
So when I'm not sure about a syntax,
1:19
I'll usually just copy the command that
I think it is and then I'll ask ChatGPT.
1:21
So I'll be like, what is the correct
syntax for this command?
1:26
That's not how you spell command,
but it doesn't really matter.
1:34
And as you can see,
this is the correct syntax.
1:36
So if we did that command,
it would not have worked.
1:38
Rails generate device:controllers users,
perfect.
1:41
Now press Enter, and
this is gonna be really useful so
1:45
that we can actually update our account
without inputting the password.
1:48
Anyway, as you can see, it says it
created all of these controllers.
1:52
So let's go into VS Code,
controllers > users.
1:57
These are the device controllers.
2:01
They were generated so
we can customize them.
2:03
Now it says,
ensure you have overridden routes for
2:06
generated controllers in your routes.rb.
2:09
For example, Rails.application.routes.draw
do devise_for :users, controllers.
2:11
So basically you have to specify which
controllers you want to actually use.
2:17
By default, these are not the controllers.
2:21
These controllers are there, but
they're not being used at the moment,
2:23
they're unused.
2:26
So we have to specify in routes
if we want to actually use them.
2:27
So let's go to routes.rb.
2:30
And so I'm just gonna copy this
devise_for, put it into routes.rb.
2:32
And then we're also
gonna add registrations,
2:36
registrations: users/registrations.
2:43
And that should be good for now, but
2:49
I'm just gonna make sure by
going into my past project.
2:51
So I'll go to config > routes.rb, and
yeah, these are the ones that we need.
2:53
So I'll just copy them in routes.rb.
2:59
And okay, so that's all we need.
3:03
And now that we have got that done,
devise is going to be using
3:06
the registrations controller for
the registrations page.
3:11
Now we can edit this and make it so
3:16
that it actually works when
we try to update the account.
3:18
So let's go back to our clone,
let's go to My account.
3:22
I need to run the server, run the server,
rails s, s stands for server.
3:28
And we got an error, and the error
comes from me spelling registrations.
3:32
The error comes from me spelling
registrations wrong somewhere.
3:37
So save that file, refresh,
and it should be okay.
3:44
Let me start the rails server again.
3:47
I'm getting another error,
let's see what it is.
3:49
Invalid route name, already in use.
3:51
Okay, so it's because we're
using the same route twice.
3:54
So all we have to do is get rid of this,
that's what we need to do.
3:57
Okay, perfect, so get rid of the
devise_for: users at the top of the page
4:01
because we're adding it down here.
4:05
And then let's go here and refresh.
4:07
Now we have to start the rails server,
my bad.
4:11
So refresh now, and
as you can see, it's working.
4:13
Now, the goal of this video
is to be able to edit this
4:16
email without inputting the password.
4:19
So look, if I try and
edit the email now and say I add mala,
4:21
Save changes, it doesn't work.
4:26
Current password cannot be blank.
4:29
So we're gonna go and fix that now,
and it's gonna be really easy.
4:31
So I'm gonna go and copy code from my
own source code that I already have, and
4:34
it's gonna be like this.
4:39
This def method is gonna make it work.
4:41
So go to your controllers,
4:43
then go to registrations and
input write this method.
4:46
So we're saying def
update_resource(resource, params).
4:53
Those are the values that we're taking
in to the function or the method.
4:57
And we're saying, return super
if params["password"]&.present?
5:01
And this requires the current password if
the user is trying to change password.
5:05
But then we're also saying,
5:08
allow the user to update registration
information without the password.
5:10
So
resource.update_without_password(params.e-
5:14
xcept("current_password")).
5:16
So if we save that file,
refresh this page,
5:18
we should be able to change the email.
5:22
And then go back, and
it has changed, perfect.
5:26
So I can change this to
Malasgharian@gmail.com, Save changes, and
5:29
it changes,
although it directs us to the route page.
5:34
So let's go back, and
as you can see, it changed.
5:37
So now that that successfully works,
I've showed you how to edit the account,
5:40
I've showed you how to get this
link to this Edit User page, and
5:44
yeah, that's the video done.
5:47
The next video, I'm gonna show you
how to deploy this app online.
5:49
So other people can see it,
we're gonna give it its own URL.
5:52
It's gonna be hosted on its own server.
5:55
It's all gonna be completely
free using Fly.io.
5:57
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up