My Rails Project and Nested Resources

Jonathan Zavala
4 min readMay 10, 2021

Introduction

Nested resources were something that I personally didn’t get a grasp of the first or second time around. We all know what the solutions that problem is, write a blog about it so it forces you to understand it. Kidding, but not kidding obviously writing and implementing the code and seeing the errors is what get you to learn, but I’ve realized writing a blog definitely helps the cause especially with communication of the topic. We’re going to brush up on resources and path helpers, talk about what nested resources are how they help and the best practices for nested resources. Let’s get to it.

Quick Overview on Resources and Path Helpers

Rails routes identify URLs and sends them to proper controller action, it also creates paths for views. Resources creates all the necessary routes for your controller actions including: index, show, new, edit, create, update and destroy. Using resources also opens up path and url helpers for example movie_path which would return movies. Alright now that we’ve went over that let’s get into nested routes.

What Are Nested Resources?

Nested resources are resources that belong to other resources aka child resources with the resources that it’s within being the parent. We should already understand this concept if we look at our models. Nested resources allow us to capture the parents, child relationship of the models and display it in our routes.

These examples show exactly the relationship I’m talking about and don’t worry too much about what that “shallow: true” means I’ll go over that later in the blog. Now we’ll go into how nested resources help us do certain actions.

How Do Nested Resources Help?

Well first off it allows for a better user experience our URL will be more readable to our users we are sharing our projects with, nobody likes to just see a bunch of random characters in the URL.

Secondly if we didn’t use resources our routes.rb and our controllers would have a lot of repeat code so our D.R.Y. sensors should be going off, with the nested resources it cleans up the routes page and our controllers. Thirdly it allows us to show the parent/child relationship in the routes and even in the URL. Lastly it gives us paths to render say a new review to a certain movie “new_movie_review_path(@movie)” it allows for more readable code but there is a lot going on behind the scenes.

Now that we know what they help us with let’s talk about best practices and why they help us and anyone else who looks at our code.

Best Practices

You should only be nesting one level deep because if you go any deeper that that separation of concerns comes into play with your routes and also your routes page get messy which defeats the purpose of the resources. We could use the “shallow” short hand which generates “:index, :new, :create” for the child resource(which you seen in the above example). Ruby is all about convention so if we all follow the rules of ruby code we’ll all have cleaner more understandable code.

Conclusion

We reviewed routes and urls a bit then went into resources and figuring what nested resources are and what they even do. We figured out how they help us with our application and user experience and then went over the best practices of implementing them. I hope this blog has helped you understand a little bit more about nested resources. I know it definitely helped me, but the best way to learn is to go out and get your hands dirty, get the errors and figure out how to get rid of them! Thanks for reading good luck coding!

--

--