Understanding Rails Routes and RESTful Design

Understanding Rails Routes and RESTful Design

Image for post

What is Rails Router? Where does users request go to? What is the connection between rails routes, view and controller? what is Rails RESTful Design? Naming routes.

Recently, we had a ruby meetup and we discuss more on Rails Routes. Lets dive into our discoveries.

What is Rails Router?

A router in Rails is a directing module that recognizes browser URLs and dispatches them to the controller actions requested. To make it simple, when you enter a url in your domain, the rails router will know which controller and action to handle your url

Image for postRails router in action

In the diagram above, a request made by a user to the URL /pages/home goes through the browser to the Rails Router(first blue box above). The router then decides that the request be handled by the pages_controller at the same time being particular on the controller action as home. The controller now renders back HTML to the user as a response.

This answers our next question?Where does users request go? Lets get this right. The Rails application asks the router to match the users request to a corresponding controller action. Rails app?s routes are configured in config/routes.rb. In the above diagram, the matching route is:

get ?pages/home?, to: pages#home?Image for postDefining route

If you run rake routes from within the rails application in the terminal you see the route:

Image for post

The router automatically maps the request from the browser to the home action/method of the pages_controller. The action then determines which page it should render to the user. In our case is the home.html.

Rails RESTful Design

REST is an architectural style(Wikipedia) that is used in Rails by default. As a Rails developer, RESTful design helps you to create default routes by using resources keyword followed by the controller name in the the routes.rb file

resources :users

which creates seven routes all mapping to the user controller.

If you run rake routes in the terminal you see the routes:

Image for post

This design also allows Rails developers to make choices about which actions to write. For instance:

resources: users, only: [:index, :new, :create]

Rails also allows you to define multiple resources in one line.

resources :users, :books, :messages

Learn more about resources in rails routes from the official rails guides

Controller Namespace and Routing

Rails allows you to organize groups of controllers under a namespace with a keyword ?namespace? during routing. For instance grouping Articles and Comments Controllers under Admin controller.

namespace :admin do resources :articles, :commentsend

To get to the articles on your browser you have to prefix it with admin , that is

/admin/articles

Naming Routes.

Instead of using raw urls generated by Rails app, Rails allows you to refer to routes by names. For example, the following will create a logout_path or logout_url a named helpers in your application.

get ?sessions/destroy?, as: :logout

To refer to this route anywhere in your application you can write logout_path . Beauty of this being that you can always change the controller and action without refactoring so much code in the views and other controllers

Read more on namespacing from official rails guides

Redirection

You can redirect any path to another path using the redirect helper in your router:

get ?/stories?, to: redirect(?/articles?)

Please note that default redirection is a 301 ?Moved Permanently? redirect. Keep in mind that some web browsers or proxy servers will cache this type of redirect, making the old page inaccessible. You can use the :status option to change the response status:

get ?/stories?, to: redirect(?/articles?, status : 302)

This depicts a ?Moved Temporarily? so the next time user requests for the resource the redirect will always occur. If we used the 301 then if a browser caches it as inaccessible then if a user tries to access the route for a second time the browser will not even make the request to our server assuming that the resources was permanently moved

Conclusion

I hope this helped make rails routes syntax a bit clearer. For more on building a RESTful CRUD app and using those web requests visit rails guides http://edgeguides.rubyonrails.org/routing.html.

12

No Responses

Write a response