Rails tutorial (6th edition) Background operation of profile editing
table of contents
- Create new account
- Login (including Friendly Forwarding and persistent login) (https://qiita.com/akarin0519/items/f241b9699e156741a8d1)
- ** Edit profile **
- Reset Password (https://qiita.com/akarin0519/items/ea873bb165ed4099a40e)
- Post Micropost
- Follow and unfollow (https://qiita.com/akarin0519/items/f1b90b18577650a40ea9)
3. Edit profile
Operation screen
User operations for profile editing with the app proceed in the following two steps.
- Click the edit link while logged in to open the profile edit screen.
- Modify the edit items and click the submit button to return to the user's home screen.
Background operation
The background operation executed in each of the above steps is as follows.
- If you click the edit profile link while logged in, a GET request will be sent to/users /: id/edit and the edit action of the Users controller will be executed. In this edit action, the user corresponding to params [: id] is searched from the DB and stored in the instance variable @ user. Then, the view (users/edit.html.erb) corresponding to the edit action is displayed, and @ user is passed here. In addition, edit.html.erb is a profile edit screen.
- Since the contents of @ user passed to the form_with function in edit.html.erb exist, Rails automatically determines that the user information has been updated. Therefore, when the submit button of the edit form is clicked, a PATCH request is sent to the/users/edit path and the update action of the Users controller is executed. In this update action, it tries to update only the value of the key that can be accessed by the parameter received from the input form, and if the update is successful, redirects to the user's home screen (root_url).