Chapter 6. Routing

In the first half of this chapter, it explains what you need to know to use rails, and in the second half, it explains how to write routes.rb.

Things to keep in mind about HTTP

HTTP Communication

  • HTTP Communication requires a URL (action) and a Method
  • Methods are GET, POST, PATCH/PUT, and DELETE.
  • For RDB-based systems, the above methods should correspond to SQL SELECT, INSERT, UPDATE, and DELETE respectively.
    • To send further information, GET uses URL parameters. POST, PATCH, and PUT use request body.
    • Other supplementary information is submitted using HTTP headers.
    • The information to send from an HTTP client such as a browser is called request
    • The information from the server is called response.
  • HTTP communication result is determined by HTTP Status, which in detail varies from systems, so it should be understood systematically.
    • 2xx: Success
    • 3xx: Redirection
    • 4xx: Client errors
    • 5xx: Server errors

REST

REST is an abbreviation of "REpresentational State Transfer", but you don't need to think too much about that. It is safe to assume that in REST as Rails architecture are the following.

  • Controller name is the resource name (= table name) and has the actions index, show, new, create, edit, update, and destroy.
  • The base code can be generated by rails generator such as scaffold.
  • HTTP method uses
    • GET for index, show, new, edit
    • POST for create
    • PATCH/PUT for update
    • DELETE for destroy.
  • Implementations that do not fall into these categories (such as updating using GET) should not be used unless there is a reason.

Besides, there are the following ideas.

  • Return the appropriate response depending on the requested file type. For example, a CSV request will return a response with Content-Type of CSV.
  • To update user_images the child table of User, use action such as user_images_controller.rb or users/images_controller.rb and define the update action there rather than update_user_image in users_controller.rb.

These ideas can also be applied to Web development using non-Rails to some extent.

How to write routes.rb

Rails Guides - Routing

Rails Guides has details, but it is a bit too much information, so summarized it below.

Major Patterns

There are three major patterns.

  1. root for root.
  2. resource / resources for pages associated with the table.
  3. get, post for individual pages not associated to any table.

In other words, use resource / resources in most cases.

root

Write as {controller}#{action}.

e.g. root to: 'home#index'

resource / resources use for pages associated with the table.

# resources :table name, only: %i[action name]
resource :users, only: %i[index show create update]
resource :cart, only: :show

Data viewed in a list use resources, and single data (such as has_one association) use resource.

If only option is omitted, all routings (index, show, new, create, edit, update, and destroy) will be created, so it is basically required.

If parent resource id is not needed, it appears that it can also be done as follows. (I don't use it.)

resource :basket
resolve "Basket" do
  [:basket]
end

Nesting (hierarchy)

Creating a namespace

Use namespace.

namespace :mypage do
  resources :articles
end

This code:

  • create /mypage/articles actions
  • does not create /mypage

If you want to create URL /mypage, add resource or get like the following.

resource :mypage, only: :show, controller: 'mypage'
# or
get :mypage => 'mypage#show'
Creating a hierarchy with parent id

In this case, the child URLs contain use_id.

resources :users do
  resources :images, only: :index
  resource :setting, only: :show
end

Changing the parameter name

Use :param option.

resources :users, param: :name

get, post, etc, for individual pages.

# {method} :{URL} => '{controller}#{action}'
get :mypage => 'mypage#show'
post :inquiry => 'inquiry#create'

All HTTP method can be used. Individual pages not associated to any table (such as Terms of Use, About Us, Privacy Policy, FAQ, etc.) can be written in this way.

How to check the generated routing

It can be checked paths by accessing http://localhost:3000/rails/info/routes after rails restart (change the port number accordingly). If rails s not started, can also use a command rails routes.