Skip to content
By Rachelle LeQuesne

Rails Routing

Note: This post is intended as a supplement to WTF Just Happened? A Quick Tour of Your First Rails App.

Rails Routing

The purpose of the routes.rb file is to control the conversion from an HTTP request to a Rails method call. You can think of it as a directory that takes a HTTP request and looks up the corresponding Rails controller method.

The REST routes know the action to call in the controller based in the HTTP method. As we have discussed in an earlier article REST and Rails, you will know that a resource comes with seven standard methods for requesting data:

  • show
  • update
  • destroy
  • index
  • create
  • new
  • edit

When you add resources :posts to your routes.rb file, Rails will generate routes for all seven of these methods. You can, of course, restrict them by using the keyword only, as in resources :comments, only: [:create]. This will create a route for creating a comment only.

Each controller action also has a path method as well as a URL method. These can be used in the view template to create a link. Below are the routes for the Posts controller:

routes-mappings-installfest.png

You can use routes to reflect a relationship between resources. For example, we have comments belonging to a post so in the route.rb file, we can write:

resources :posts do
  resources :comments, only: [:create]
end

This is known as a nested route and it means that a comment cannot be viewed outside of the context of a post. See the section 'Adding comments' in the Installfest Guide for a worked example.

We are not limited to the routes provided to us by Rails and REST. We can define customised routes to add your own actions to the resource. Each resource call provides three options for specifying custom actions. The :member option is for actions that apply to a specific resource, the :collection option is for actions on the entire list (like index), and the :new option applies to resources that have not yet been saved to the database.

The one 'gotcha' is the a custom resource route needs to be specified twice: once in the controller itself, and then again in routes.rb.

For a more in-depth (and more advanced) article on routing see Rails Routing from the Outside In.

Latest Articles by Our Team

Our expert team of designers and developers love what the do and enjoy sharing their knowledge with the world.

We Hire Only the Best

reinteractive is Australia’s largest dedicated Ruby on Rails development company. We don’t cut corners and we know what we are doing.

We are an organisation made up of amazing individuals and we take pride in our team. We are 100% remote work enabling us to choose the best talent no matter which part of the country they live in. reinteractive is dedicated to making it a great place for any developer to work.

Free Community Workshops

We created the Ruby on Rails InstallFest and Ruby on Rails Development Hub to help introduce new people to software development and to help existing developers hone their skills. These workshops provide invaluable mentorship to train developers, addressing key skills shortages in the industry. Software development is a great career choice for all ages and these events help you get started and skilled up.

  • Webinars

    Webinars

    Webinars are our online portal for tips, tricks and lessons learned in everything we do. Make the most of this free resource to help you become a better developer.

    Learn more about webinars

  • Installfest

    Installfest

    The Ruby on Rails Installfest includes a full setup of your development environment and step-by-step instructions on how to build your first app hosted on Heroku. Over 1,800 attendees to date and counting.

    Learn more about Installfest

  • Development Hub

    Development Hub

    The Ruby on Rails Development Hub is a monthly event where you will get the chance to spend time with our team and others in the community to improve and hone your Ruby on Rails skills.

    Learn more about Development Hub

Get the “reinteractive Review” Monthly Email