Rails routing techniques

Tram Ho

Routing is a technique of navigating through URLs sent from the browser to the actions in the Controller. Routes are the main communication bridge when users manipulate the website. That is why routing is very important, creating URLs that are user-friendly, user-friendly but still meet the technical requirements of the server. Routing appears in most popular frameworks. In this article I will introduce you to the Routing techniques in Rails.

  • Resources

Resources will create 7 default routes that map to 7 actions in the controller: [: index,: new,: create,: show,: edit,: update,: destroy] Example when you want to create routes for the user object.

To check the existing routes in the project, we type the command rails routes , in the above case, the 7 routes generated will have the form:

In which: * Prefix: is a shortened path instead of writing as URI * Verb: is the HTTP method for each route * URI: is the path corresponding to the URL bar of the website * Controller # Action: is the Action to be executed when where the route is called

  • Nested Resources

Nested Resources is used to reflect 1-n relationship between models in routes and expressed through URLs, for example, a Room has many Reports and a Report must belong to a Room, Rails allows to set up nested resources. . Setting up Nested Resources will help manage resources better and more scientifically. Writing style:

When running rails routes:

If written as above, it will generate all 14 routes equally divided between room and report. Notice the URI Pattern of the report, we can see that they are all associated with room.

  • Resource

Resource will only create 6 routes, no index. Also when Resource routes will not use: id.

When running rails routes:

  • Namespace

Namespace helps to group, create prefixes to distinguish and manage routes. For example we want to distinguish the admin routes

When rails routes:

The routes already prefixed with admin.

  • collection and member

Both of the above options are used in resources to create routes that map to non-default methods (: index,: new, …) in controllers. The difference is that the collection does not use the id from the resources it contains. Writing style:

Members also use the id from the resources containing it. In this example it is the id of the room.

  • only:, except:

If you do not want to create all 7 routes as the default of resources, you can use: only : only get the specified except routes: ignore the specified routes Go back to the resources: user example if you only want to create 2 routes [: index,: new] then we use only as follows:

There will be 2 routes generated: index and: new.

On the contrary, when we don’t want to create routes [: index,: new], we change only to except :

There will be 5 routes generated except: index and: new

  • Epilogue

This article I have referenced at: https://guides.rubyonrails.org/routing.html Thank you for reading ^^

Share the news now

Source : Viblo