Routing with RESTful Design for Rails

Interaction with Router in Ruby on Rails

Tushar Adhao
Nov 11 · 2 min read
Photo by Denys Nevozhai on Unsplash

REST is an architectural style that is used in Rails by default. RESTful design helps to create default routes (by using resources keyword) in the config/routes.rb file, as given below,

config/routes.rbRails.application.routes.draw do
resources :homes
end

Now this will create following routes by default as,

$ rake routesPrefix    Verb    URI Pattern          Controller#Action
homes GET /homes(.:format) homes#index
POST /homes(.:format) homes#create
home GET /homes/:id(.:format) homes#show
PATCH /homes/:id(.:format) homes#update
PUT /homes/:id(.:format) homes#update
DELETE /homes/:id(.:format) homes#destroy
new_home GET /homes/new(.:format) homes#new
edit_home GET /homes/edit(.:format) homes#edit

Resources vs Resource

Similar to the resources, another option that we have is to go for ‘resource’ instead of ‘resources’. The term ‘resource’ will work same as ‘resources’ except the condition that it won’t create ‘index’ action.

config/routes.rbRails.application.routes.draw do
resource :homes
end

Get back to the terminal,

$ rake routesPrefix    Verb   URI Pattern           Controller#Action
homes GET /homes(.:format) homes#show
PATCH /homes(.:format) homes#update
PUT /homes(.:format) homes#update
DELETE /homes(.:format) homes#destroy
POST /homes(.:format) homes#create
new_home GET /homes/new(.:format) homes#new
edit_home GET /homes/edit(.:format) homes#edit

The point to be noted is that when resource without (s) is used, it will force the route to go for plural controller even if it is defined as singular, URL remains singular.

config/routes.rbRails.application.routes.draw do
resource :home
end

At terminal end,

$ rake routesPrefix    Verb    URI Pattern          Controller#Action
home GET /home(.:format) homes#show
PATCH /home(.:format) homes#update
PUT /home(.:format) homes#update
DELETE /home(.:format) homes#destroy
POST /home(.:format) homes#create
new_home GET /home/new(.:format) home#new
edit_home GET /home/edit(.:format) home#edit

But this won’t be the same in case of resources with (s). As resources with (s) followed by singular route name will look for singular name controller.

config/routes.rbRails.application.routes.draw do
resources :home
end

At terminal end,

$ rake routesPrefix      Verb    URI Pattern          Controller#Action
home_index GET /home(.:format) home#index
POST /home(.:format) home#create
home GET /home/:id(.:format) home#show
PATCH /home/:id(.:format) home#update
PUT /home/:id(.:format) home#update
DELETE /home/:id(.:format) home#destroy
new_home GET /home/new(.:format) home#new
edit_home GET /home/edit(.:format) home#edit

Yeah, so important point to note is to always have a keen eye on singular and plural naming convention in not only while defining routes but throughout in Rails framework.

Reference

Tushar Adhao

Written by

Software developer spreading nuggets of coding gold related to web as well as mobile applications.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade