New engineer who will be one serving in 100 days (5th day)

New engineer who will be one serving in 100 days (5th day)

New engineer who will be one serving in 100 days (4th day)

Good evening, it's already the 5th day. Today's topic is about Rails routes. When I looked it up, there were quite a few rules, so I would like to summarize them.

Rails routing

Rails router

The Rails router will recognize the URL it receives and assign it to the appropriate in-controller action.

GET /patients/17                  

When there is a request with such a URL

get '/patients/:id', to: 'patients#show' 

Assigned to the show action on the patients controller. The 17 part corresponds to: id. After to:, it is associated with the feeling of'controller #action'.

Resource-based Routing

It is a guy who puts together various related requests into one action.

resources :users

Just write a line in routes like this and the following routes will be added

HTTP path controller#action
GET /users users#index
GET /users/new users#new
POST /users users#create
GET /users/:id users#show
GET /users/:id/edit users#edit
PATCH/PUT /uses/:id users#update
DELETE /users/:id users#destroy

Controller namespace and routing

You can group by namespace.

namespace :admin do
  resources :users
end
HTTP path controller#action
GET /admin/users admin/users#index
GET /admin/users/new admin/users#new
POST /admin/users admin/users#create
GET /admin/users/:id admin/users#show
GET /admin/users/:id/edit admin/users#edit
PATCH/PUT /admin/uses/:id admin/users#update
DELETE /admin/users/:id admin/users#destroy

This way you can create routes in groups starting with / admin.

By the way, if you want to select and use some routes in resources

resources :articles do
  resources :comments, only: [:index, :new, :create]
end

Just like this, you can specify the routes you want to use after only :. It's unlikely that you'll specify routes that you don't need.

Named routing

You can use the: as option to name any routing.

get 'exit', to: 'sessions#destroy', as: :logout

The above routing creates logout_path and logout_url as the application's named routing helpers. Calling logout_path will return / exit. And so on: you can create a named routing with as.

Named routing is also created at the time of resources.

Basically, it seems that the url is often specified by a named route, so I recently feel that getting used to this method is quite important.

If you write everything, it seems infinite, so I'm done here today. If you don't understand, I want to get into the habit of checking the Rails guide each time.

*** 95 days to become a full-fledged engineer ***

Recommended Posts

New engineer who will be one serving in 100 days (5th day)
New engineer who will be one serving in 100 days (6th day)
New engineer who will be one serving in 100 days (4th day)
New engineer who will be one serving in 100 days (day 0)
New engineer who will be one serving in 100 days (3rd day)
New engineer who will be one serving in 100 days (1st day)
New engineer who will be one serving in 100 days (2nd day)
26th day of engineer who will become full-fledged in 100 days
28th day of an engineer who will become a full-fledged person in 100 days