Using Nested Routes in a Rails Project

For my Ruby on Rails Project, I created an app called Travel Planner where users can plan trips and make lists of destinations that they would like to visit. They can also write notes about their destinations. I used the Models-Views-Controller pattern to organize the code, created different models to separate concerns, and included their associations. I also implemented Devise Authentication and use Github OmniAuth as an additional login.

So far the toughest thing to learn about developing a Rails application is nested routes. “Rails magic” means that the user is routed to different pages after they click on something on a web page. This can be straightforward when you’re just dealing with a single model, but when objects are dependent on each other, it can get a little tricky.

When you nest resources, one model belongs to another model. That relationship must be clearly expressed when you go from one view file to another. If you don’t code the route appropriately in your “link_to” or the “redirect_to”, you will get an error in the server. The last project that we did was in Sinatra and didn’t require nested routes, so this took a little while to understand at first.

The best way to understand nested routes is to run “rails routes” in the terminal and look for the actions being taken on the controller. It shows you the action methods and rails path helpers in your app. Here is one example of a single resource’s controller and action method in ‘rails routes’, for creating a new trip in the app:

trip#new

Trip is the controller name, and #new is the action method.

Each action will also provide a corresponding prefix. You can add the prefix to _path or _url when you are creating a link or redirecting the user. For example, to show the user a form to create a new trip, you would code this rails path helper:

new_trip_path

To put it all together, when you create the link, this is what it looks like:

<%= link_to ‘Create a Trip’, new_trip_path %>

The new trip object hasn’t been created at this point, so you wouldn’t need to pass it an argument.

Routing a nested resource takes this concept to the next level. When objects are nested, you code both objects into the url. Each trip can have many destinations, so you have to know how to utilize the routes and which variables to pass to them. So, when you code the link below — If the URL you are creating involves a specific object ID, then use the name of that object as a variable in the path:

<%= link_to ‘Add Destination’, new_trip_destination_path(@trip) %>

The URL for this link looks like:

https://localhost:3000/trips/:id/destination/new

The link takes the user to a view file where they can create a new destination for their trip. You pass the ‘@trip’ variable to the URL to make sure that it uses the correct trip_id.

Utilizing this concept is really helpful in keeping Rails code as simple and DRY (“Don’t Repeat Yourself”) as possible.

Software developer with a tech stack in Ruby, Rails, HTML, CSS, JavaScript, React, and Redux. Will code for coffee.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store