Working with Nested Routes

Tutorial 4 of 5
  1. Introduction
  2. Goal: The primary focus of this tutorial is to understand and work with nested routes in Rails. We will learn how to utilize nested routes to express relationships between resources and how to implement them in our Rails application.
  3. Learning Outcome: By the end of this tutorial, you will be able to implement nested routes in your Rails application and understand how it helps in expressing complex relationships.
  4. Prerequisites: Basic understanding of Ruby on Rails, and familiarity with MVC architecture.

  5. Step-by-Step Guide

  6. Nested routes in Rails allow you to capture relationships in your URL schemas. For instance, if you have a blog application with posts and comments, a comment is always related to a post.
  7. The URL for a comment might look like '/posts/1/comments/2', where '1' is the id of the post and '2' is the id of the comment.
  8. We set up nested routes in our 'config/routes.rb' file.
    ruby # config/routes.rb Rails.application.routes.draw do resources :posts do resources :comments end end
  9. This will create a set of routes for comments that are nested within posts.
  10. To handle these routes, we'll have to make some changes in our CommentsController to fetch the parent post from the database.
    ruby # app/controllers/comments_controller.rb def index @post = Post.find(params[:post_id]) @comments = @post.comments end
  11. Best Practice: Avoid nesting resources more than 1 level deep. Deeply nested resources can lead to complex code that is hard to maintain.

  12. Code Examples

  13. Example 1: Creating a new comment on a post.
    ruby # app/controllers/comments_controller.rb def create @post = Post.find(params[:post_id]) @comment = @post.comments.create(params[:comment]) redirect_to post_path(@post) end
    Here, we first find the post using the 'post_id' from the parameters. Then we create a new comment related to that post. Finally, we redirect the user back to the post.

  14. Example 2: Deleting a comment from a post.
    ruby # app/controllers/comments_controller.rb def destroy @post = Post.find(params[:post_id]) @comment = @post.comments.find(params[:id]) @comment.destroy redirect_to post_path(@post) end
    We're finding the post and the comment in the same way as before. But this time, we're calling 'destroy' on the comment and then redirecting the user back to the post.

  15. Summary

  16. We've learned how to set up nested routes in Rails and how they can be used to express relationships between resources.
  17. To continue learning, try setting up nested routes in your own Rails application.
  18. Additional resources: Rails Routing guide

  19. Practice Exercises

  20. Exercise 1: Set up a new Rails application with nested routes for 'Authors' and 'Books'. Each author should have many books.
  21. Exercise 2: Add 'Reviews' as a nested resource under 'Books'. Each book can have many reviews.
  22. Tips for further practice: Try working with more complex relationships, such as many-to-many or polymorphic associations.

Remember, practice is key when it comes to mastering new concepts in programming. Happy coding!