Rails tutorial (6th edition) Background operation of the posting function of the micro post
table of contents
- Create new account
- Login (including Friendly Forwarding and permanent login) (https://qiita.com/akarin0519/items/f241b9699e156741a8d1)
- Edit Profile (https://qiita.com/akarin0519/items/4b3c64f88aefcaeb1781)
- Reset Password (https://qiita.com/akarin0519/items/ea873bb165ed4099a40e)
- ** Posting a micro post **
- Follow and unfollow
5. Posting a micro post
Operation screen
Posting a micropost proceeds in the following two steps.
- Log in to display the home screen. A micropost posting form is installed on the logged-in home screen.
- Enter the contents of the micropost and press the send button to save the contents of the micropost in the DB.
Background operation
The background operation executed in each of the above steps is as follows.
- See __ here __ for background behavior when logging in. Sending a GET request to/static_pages/home executes the home action of the StaticPages controller. In this home action, a new @ micropost is created in the form of being linked to the user ID (only the part related to the micropost is extracted). At the end of the home action, the corresponding view (/static_pages/home.html.erb) is called and the home screen is displayed. The @ micropost created by the home action is passed to this view, and Rails determines that it is a new micropost because the contents are empty.
- When the user fills out the micropost input form and presses the post button, a POST request to/microposts is sent and the Microposts controller create action is executed. Immediately before executing this create action, it is confirmed whether the user is currently logged in. In this create action, only the values of params [: micropost] [: content] and params [: micropost] [: image] are allowed, stored in @ micropost, and tried to save to DB. .. If the save to DB is successful, the redirect to root_url will be performed and the home screen will be displayed.