Restful Routing

Two Snakes
3 min readMay 21, 2017

--

Anytime you create an app you will have three major components: Routes, Controllers and Views. A route connects to the controller where you will create actions to be called upon. From the actions you will then create views which will make your code viewable in html form so the user can see what we are all very used to seeing in the interfaces we interact with daily on the internet.

For today, I am going to focus specifically on Routes. To reiterate, the purpose of routes in Rails is as follows: The Rails router recognizes URLs and dispatches them to a controller’s action. It can also generate paths and URLs, avoiding the need to hardcode strings in your views. To create routes we use a variety of verbs depending on what we want the route and action to accomplish.

this cheat sheet is invaluable and will help you get comfortable with creating routes.

Before I delve further into some tips and tricks of these routes, what is not listed here is that it is customary to create a root folder above all of these. The root folder is often a great place to redirect things after they have been deleted as well as for other reasons I won’t get into now. As you begin your coding journey in Rails you will most likely want to make your root folder have the same controller as your index. Just know that in more advanced scenarios the root folder could link to a homepage that does not necessarily show a list of all items you are wishing to display. The root folder along with the rest of the routes in Rails will actually look like the following (if we were making it in conjunction with the above example using photos.)

get ‘/’ => ‘photos#index’
get ‘/photos’ => ‘photos#index’

get ‘/photos/new’ => ‘photos#new’
post ‘/photos’ => ‘photos#create’

get ‘/photos/:id’ => ‘photos#show’

get ‘/photos/:id/edit’ => ‘photos#edit’
patch ‘/photos/:id’ => ‘photos#update’

delete ‘/photos/:id’ => ‘photos#destroy’

Many people including myself like to separate their routes as I have done above to help their thought process and understanding. As you can see the root and index make sense to be together; if you were to create a new photo it would also then need to be posted/created (data captured and saved in the database) so those two go together. Show is unique and standing by itself because it will show one item. Edit and update go together because once a photo is edited the information will need to be updated. Lastly destroy goes by itself because once something is deleted it’s gone so there is no information left to work with.

So to go over the whole thing one more time:

When your Rails application receives an incoming request for:

GET /photos/17

It asks the router to match it to a controller action. If the first matching route is:

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

the request is dispatched to the photos controller's show action with { id: '17' } in params.

lastly your html views will allow this to be displayed to the user in a presentable way.

The order of this whole thing is known as restful routing. To disturb this order or change the sequence in which you write these Url verbs can cause problems down the line as you begin to write more complicated code. There are times when we can break convention of restful routing but for now just stick to this and get comfortable fully understanding this concept and how it truly functions.

One last thing. The table does in fact explain this but it is easy for beginners to miss; an imperative aspect of restful routing is the index shows many items and show, shows a single item.

Alright, rest well!

--

--