Root of all Routes: How to define URLs in Ruby on Rails
By: Sunny P
Tags: Web Development, Codingbootcamp, London Tech, Ruby on Rails, Learning To Code

After having spent the first few weeks on Ruby Object Oriented Programming, in my 5th week into Flatiron School’s bootcamp, I was introduced to Rails. One of the things I picked up during this part of the course was that when building a Ruby on Rails web application it’s actually quite handy to know that Rails comes with a set of methods that are pre-built into the system . These can be formatted to create a wide variety of routes and pages for the next big Rails web app.

Back to our Routes…

For this blog I’m mostly going to discuss how we can build on the config/routes.rb file. This is where we outline all the routes in our application. As we know with any Web App, it’s pretty redundant in terms of what we can do with it unless we add more than one page to it. Defining routes using helper methods as well as the more custom-built options can go a long way.

Root ? or Route?

The root method defines where Rails should send us for the route / , for instance yahoo.com/ .

It is quite conventional to put the root route at the top of the config/routes.rb file as it is (ideally) the first route to be matched when we navigate to the site. Really speaking all the pages of our site that we want to specify can go in here:

Rails.application.routes.draw do root 'songs#index' end

Alternatively, we also have the option of using a controller:

root controller: :songs, action: :index

Both of these will direct us to the same “Songs” site. However what’s important to note here is that with the controller we can then use the method redirect_to root_path , which is what we would use to navigate back to this “root” page.

Time to get Resourceful …

Personally I found that having to mention every single route individually can be a rather labouring process. So here comes the divine helper methods known as resources . Whilst we can specify one single route if we wish to like so:

get 'songs', to: 'users#show'

or,

get 'songs', to: :show, controller: 'users'

… we could generated a model and a controller for Songs, multiple routes can then be created by just adding resources :songs in the config/route.rb file. So if we do rake routes in the terminal we should then be able to see the rails helper methods and urls generated by resources :songs .

So now in the controller or view files we can make use of these helper methods to direct our routes. For example,

redirect_to songs_path

will take us to:

By using an array of the pages we want, we can restrict the resources command to just the specific ones we want to render. So we can have:

resources :songs, only: [:new, :create, :show, :destroy]

The URL of a record is set by it’s ID, this happens by default in Rails so the url path songs/1 is for the first record.

Routing With Records

As we know ActiveRecord has many handy attributes. One of them being that their instances can be translated into routes by Rails. So no longer do we have to write out the routes associated with each record from memory.

@song = Song.find(12)

What we show on a browser can then come from an HTML view template where we render this record variable, like so:

<%= link_to 'SomeSongName', song_path(@song) %>

The path songs/12 can then be generated without having to specify the song_id. In the controller we can easily modify the record variable without updating the path in the view.

This can also be used in the instance of a delete functionality.

<%= link_to 'remove song', @song, method: :delete %>

Redirectors and ‘Page not found’ errors

By specifying a method, a target page and the redirect helper method we can redirect a page from a GET action.

get '/songs', to: redirect('/artists')

Often when a page is not found many websites come up with a 404 response error. With Rails we can set up default route for the event in which one of our pages aren’t found by the browser. In the line below we are basically taking account for all cases in which the user hit the wrong page and redirecting then to the index page.

Rails.application.routes.draw do

# ... all our other routes

match "*path", to: "songs#index", via: :all

end

Or, if we like, we can have a message to inform the user that the page wasn’t found:

class ApplicationController < ActionController::Base

protect_from_forgery with: :exception def index

flash.notice = 'Sorry, that page wasn't found!'

redirect_to root_path

end

end

Hopefully this post has helped anyone who may be trying to find out more about routes on Ruby on Rails. Thank you for reading. Happy coding!