Nested Routes and How they Relate to the Controllers

Alex Waller
4 min readFeb 12, 2021

One of the keys to creating a dynamic website is having nested routes. What would rottentomatoes.com be without nested routes? Just imagine if you could not select a specific movie and not see the reviews that were specific to that movie? Not much, right? Therefore, it is important that we nest our routes, set up the proper relationships in our models, and then set up our controllers with the proper arguments.

For this blog post, I’d like to discuss how we approach the Controllers, where the majority of the work is that the programmer will to do in order ensure that these relationships will function properly. For my Rails project, I made an app that goes along with my comic book story Hybrids. Without getting to specific, in the app, you sign up as a member of a fictional corporation, you then proceed different to report different hybrids that you are tracking as they moving across the country. As the hybrids travel, they meet friends, go to different places, and ride in motorhomes. When the user clicks on a hybrid on the hybrid index page, they will be taken to the hybrid show page. Once they are on the show page, there will be links to the index pages for places, friends, and motorhomes, and those index pages will link to the respective show pages. So, for this blog post, we will cover how we set up the hybrids controller, and then how we set up the controller for the friends, which will be pretty much the same for how we set up the places and motorhomes controllers.

Starting with the hybrids controller, the index controller is very straight forward:

@hybrids = Hybrid.all

Same goes for the show page for hybrid.

@hybrid = Hybrid.find(params[:id])

Things get slightly more complex for new, since we want to make the new instance of the hybrid class to have an argument of the current user’s user id.

@hybrid = Hybrid.new(user: current_user)

When we post the new hybrid information from the new form a new instance of the hybrid class will instantiated, which will be our create method. The new instance will have an argument of the hybrid params we defined within this controller.

@hybrid = Hybrid.new(hybrid_params)

As long as the new instance is able to be saved to the database, we will be redirected to that specific hybrid’s show page.

if @hybrid.save

redirect_to hybrid_path(@hybrid)

Next, we will need to take care of the edit and update methods. For the edit method, we will define our variable as:

@hybrid = Hybrid.find(params[:id])

For the update method, we will once again find the hybrid by it’s id, and then update the hybrid with the updated params:

@hybrid = Hybrid.find(params[:id])

Then as long as the proper conditions are met, the hybrid instance will update with the hybrid params:

hybrid.update(hybrid_params)

If we’d like to remove the Hybrid from the database, then we’ll have to find the hybrid by its id before removing it:

@hybrid = Hybrid.find(params[:id])

Then, as long as the proper conditions are met:

@hybrid.destroy

Now onto the friends controller. It will be critical that as new instances of the friend class are instantiated, that they also belong to a specific hybrid. So , for the index, we will do this:

hybrid = Hybrid.find(params[:hybrid_id])

Then:

@friends = hybrid.friends

The show method will be very familiar:

@friend = Friend.find(params[:id])

Remember how when we made our new method for the hybrids that we made the new instance with the argument of the current user’s id. Well, when new instances are made for the new method of the friends controller, the new instances will have an argument of the hybrid’s id:

@friend = Friend.new(hybrid_id: params[:hybrid_id])

Then we will have the create method. Where the instance variable will be a new instance of the class that has an argument of the friend params:

@friend = Friend.new(friend_params)

As long as this instance is able to save to the database, we will redirect to the path that is the show page of that specific friend.

@friend.save

redirect_to hybrid_friend_path(hybrid_id: @friend.hybrid_id, id: @friend.id)

Next is the the edit method, where we will be making a GET request, and we will find the specific instance of the friend class by the id of that object.

@friend = Friend.find(params[:id])

For the update method, we will find the instance of the friend class by its id, then update that object with the argument of the friend params that were posted.

@friend = Friend.find(params[:id])

@friend.update(friend_params)

As long as all conditions are met, we will redirect to the show page of the friend instance.

redirect_to hybrid_friend_path(hybrid_id: @friend.hybrid_id, id: @friend.id)

Then, finally, if we wish to remove that friend from the database, we will once again find the friend instance by its ID. Then, as long as all conditions are met, we will remove that instance from the database.

@friend = Friend.find(params[:id])

@friend.destroy

--

--