Understanding REST will improve the outlook for Ruby on Rails tutorials

What you can get by reading this article

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)

What is REST?

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)

Level 1 Understanding the relationship between HTTP requests and CRUD operations

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

Understanding what is routed in Level 2 resources

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

Understanding the mysterious action group generated by level 3 scaffold

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

Level 4 Understanding of REST itself

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.

Summary

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

Understanding REST will improve the outlook for Ruby on Rails tutorials
Explanation of Ruby on rails for beginners ①
Ruby on Rails DB Tips for creating methods to reduce the load
Validation settings for Ruby on Rails login function
[Ruby on Rails] Until the introduction of RSpec
When the Ruby on Rails terminal rolls back
Publish the app made with ruby on rails
[Ruby on Rails] Select2 introduction memo for Webpacker
[Rails] Procedure for linking databases with Ruby On Rails
Determine the current page with Ruby on Rails
[Ruby on Rails Tutorial] Error in the test in Chapter 3
Docker the development environment of Ruby on Rails project
[Ruby on Rails] How to change the column name
Ruby on Rails Elementary
Ruby on Rails basics
Solve the N + 1 problem with Ruby on Rails: acts-as-taggable-on
Challenge the settings for developing with vue.js on Rails 6
Ruby On Rails Association
Explanation of Ruby on rails for beginners ⑥ ~ Creation of validation ~
Explanation of Ruby on rails for beginners ② ~ Creating links ~
(Ruby on Rails6) Reflecting the posted content from the form
Try using the query attribute of Ruby on Rails
Explanation of Ruby on rails for beginners ⑦ ~ Flash implementation ~
[Ruby on Rails] Only the user who posted can edit
Definitely useful! Debug code for development in Ruby on Rails
Ruby on Rails for beginners! !! Summary of new posting functions
[Procedure 1 for beginners] Ruby on Rails: Construction of development environment
(Ruby on Rails6) Display of the database that got the id of the database
[Environment construction] Get the Ruby on Rails 6 development environment within 1 hour
Delete all the contents of the list page [Ruby on Rails]
A note about the seed function of Ruby on Rails
[Ruby on Rails] Automatically enter the address from the zip code
[Note] About the Fizz_Buzz problem (How Ruby on Rails works)
Explanation of Ruby on rails for beginners ③ ~ Creating a database ~
Ruby on rails learning record -2020.10.03
Portfolio creation Ruby on Rails
Ruby on rails learning record -2020.10.04
[Ruby on Rails] Debug (binding.pry)
Ruby on rails learning record -2020.10.05
Ruby on rails learning record -2020.10.09
Ruby on Rails config configuration
[Ruby on Rails] about has_secure_password
Rails ~ Understanding the message function ~
Commentary on partial! --Ruby on Rails
Ruby on rails learning record-2020.10.07 ①
Cancel Ruby on Rails migration
Ruby on rails learning record -2020.10.06
Ruby on Rails validation summary
Ruby on Rails Basic Memorandum
[For beginners] Build the environment for Ruby on Rails Tutorial 4th Edition (Rails 5.1) in less than an hour!
Understand code coverage with Rspec, the Ruby on Rails test framework
[Ruby on Rails] Change the save destination of gem refile * Note
Explanation of Ruby on rails for beginners ⑤ ~ Edit and delete database ~
Create a development environment for Ruby 3.0.0 and Rails 6.1.0 on Ubuntu 20.04.1 LTS
I summarized the flow until implementing simple_calendar in Ruby on Rails.
[Ruby on Rails] Use the resources method to automatically create routes.
Things to remember and concepts in the Ruby on Rails tutorial
[Ruby on Rails] How to avoid creating unnecessary routes for devise
(Ruby on Rails6) Create a function to edit the posted content
[Ruby on Rails] Pass the parameters divided by date_select to FormObject.
[Ruby on Rails] When logging in for the first time ・ How to split the screen in half using jQuery