Instead of doing the tutorial "on the fly", you will be able to proceed with the tutorial with a ray of light passing through (maybe)
Assumed readers do not have much prior knowledge around the WEB People who are in the middle of or just after the Rails tutorial (I am)
Roughly speaking, it's the design concept of the web. Rails is also designed according to the idea of REST
In fact, the tutorial is in column 2.2 (for the 4th edition) of Chapter 2 of the Rails tutorial. There is an overview of REST that seems to be necessary to read it I think there are some people who take a quick look and move on without knowing it well. I advanced
But,
The concept of REST goes around throughout the tutorial (or even the gist) It's better to grab it early during the tutorial I think it's hard to get lost because you can see the consistent structure and what the author thinks.
The concept of REST is so abstract that you don't have to understand it all right away. If you understand, not only inside the tutorial but also outside the tutorial It is advantageous to be able to see the web world a little clearly
Below, I will explain according to the level I felt
[Understanding the relationship between Level 1 HTTP requests and CRUD operations](# Understanding the relationship between Level 1 http requests and crud operations) [Understanding the contents routed by Level 2 resources](#Understanding the contents routed by Level 2 resources) [Understanding the mysterious action group generated by level 3 scaffold](#Understanding the mysterious action group generated by level 3 scaffold) [Understanding Level 4 REST itself](#Understanding Level 4 REST itself)
HTTP request is sent when the user operates the browser In the Rails tutorial, write the part that operates the database based on the request Most of the time, I write the browser operation procedure in a test and check the operation.
If you understand this much, I think that the contents in the tutorial will be somehow managed for the time being
・ Four of the eight HTTP requests that are often used
HTTP request | processing |
---|---|
GET | Get |
POST | Registration |
PUT | update |
DELETE | Delete |
・ Operations required for the database
CRUD operation | processing |
---|---|
READ | Get |
CREATE | Registration |
UPDATE | update |
DELETE | Delete |
・ Each correspondence
HTTP request | CRUD operation | processing |
---|---|---|
GET | READ | Get |
POST | CREATE | Registration |
PUT | UPDATE | update |
DELETE | DELETE | Delete |
Q: Just write "resources: users" in "routes.rb" Why do you route me in various ways?
routes.rb
Rails.application.routes.draw do
resources :users
end
A: Rails is selfish by default when you declare resources Be careful based on GET, POST, PUT and DELETE It will generate the following by associating the URL with the action of the controller
URL | action | HTTP request | Named route | Use |
---|---|---|---|---|
/users | index | GET | users_path | Display user list screen |
/users/:id | show | GET | user_path(user) | Display the screen of a specific user |
/users/new | new | GET | new_user_path | Display new user registration screen |
/users/:id/edit | edit | GET | edit_user_path(user) | Display user edit screen |
/users | create | POST | users_path | User registration action |
/users/:id | update | PATCH/PUT | user_path(user) | User update action |
/users/:id | destroy | DELETE | user_path(user) | User delete action |
If you use scaffold, the following 7 actions will be generated in the users controller without permission.
def index def show def new def edit def create def update def destroy
At the beginning, I think you didn't know what it was because it was made in various ways. At this point, I think you can see the relationship between them. It seems that these seven are Rails templates. (You can delete unnecessary actions You can add your own actions if necessary)
The explanation is mixed up, If you write "resources: controller name" in "routes.rb", all 7 actions will be routed. As in Chapter 13 of the tutorial, you can generate only the routes you need with the: only option.
routes.rb
Rails.application.routes.draw do
resources :microposts, only: [:create, :destroy]
end
What is REST after all?
The original REST design principle seems to be the following four
- Must be published with a URI that can be addressed
- The interface (using HTTP methods) must be unified
- Be stateless
- The processing result is notified by HTTP status code
Source: Introduction to REST Basic Knowledge
The Rails tutorial requires one of the above REST principles
only. (This article also focuses on that) Please refer to the citation source link for other details.
However, if you know this much, the APIs of various services are also made with REST in mind. Since there are many, it will be easy to link with other RESTful services.
If you use Rails, it is a prerequisite to adopt a RESTful style I hope you can understand it somehow.
The part you make yourself also follows REST, and the part that Rails does nicely Since it is in line with REST, it seems better to proceed with the tutorial while understanding that.
Recommended Posts