DEV Community

Theodore Hoover
Theodore Hoover

Posted on

Rails Dynamic and Nested Routes

Rails Dynamic and Nested Routes
I recently built a rails app using some dynamic and nested routes. These types of routes are used by most websites we use every day, and in this blog I hope to explain some of their benefits. But first, what is a dynamic route?

Dynamic Routes

A dynamic route is a route where some part of the URL is used to act essentially as an input. This works well for accessing resources that there may be many of, for instance, user accounts. On Facebook for example, where the number of profiles is way beyond the number of pages you would route by hand, the URL for a profile is usually something like: www.facebook.com/john.doe . The dynamic part of this URL would be john.doe which tells the server which profile we would like to see. If we look at an application I just built a dynamic route might be localhost:3000/users/17 which tells the server we want to look at user 17. If we enter the console where this application is and looked at the URI we would see /users/:id as one of our routes. So the dynamic id from the URL is passed into one of our controller methods within the parameters hash. Here it is used to identify the user who’s profile we want to use but I’m sure you can think of many possible other uses. I’m sure that the URL at the top of your browser right now is dynamic!

Nested Routes

Nested routes go beyond the basic kind of dynamic route described above. These routes allow us to have many routes using the first dynamic route. For instance, on Facebook, let’s say we don’t just want to look at one of our friend John Doe’s profile page, we want to see all of his photos. We’d click on a link and Facebook would send us to page with a URL like www.facebook.com/john.doe/photos As you can see the URL is telling the server which profile we want to see, and what we want to see on that profile. If we go back to the application I recently built it looks like localhost:3000/users/17/workouts But what if we want to see just one of our friend’s photos or just one of user 17’s workouts? We can have another dynamic parameter in our URL! So in my example localhost:3000/users/17/workouts/130 If we go back into the console and check the URI we will see users/:user_id/workouts/:id (It is important to note that our old :id key was replaced by :user_id) Now we are looking specifically at the workouts with id 130, belonging to the user with id 17. This allows us to make more powerful methods and views, where we can identify two objects we want to know about just from the URL. It’s pretty cool, but not even the coolest part of nested routes. If user 17 wants to add an exercise to one of her workouts how could she do that? We might make our program so that she does this on the show page at /users/17/workouts/130 or we could use another nested route at /users/17/workouts/130/new This gives our server all of the information it needs to make a form for the user to fill out. The workout is indicated by the 130 and the action we want indicated by new. This makes the ease of use of the site much better because there is no need for the user to indicate to which workout they are adding, and at the same time it makes it clear to the user what they are doing.
Thanks for reading my blog I hope it was informative!

Top comments (0)