How to Configure Redirect Routes for Nested Resource in Rails
In this lesson, we are going to see if our nesting is working properly and integrated custom redirect routes in the controller.
Guide Tasks
  • Read Tutorial

In this lesson, we are going to see if our nesting is working properly. So, let's start our rails server with the command rails s.

Next, we'll create our first task under a project. Since we haven't added an interface for it, let's go to our projects listing page and pick a project. I've picked project 13 and clicked on the show link.

Now that you know the route, just append /tasks/new after the existing URL. This should open a new task form.

large

Enter the values, and I'm marking it complete. When you click on Create Task, it actually throws an error because we don't have a show page associated specifically for it. So, open tasks_controller.rb and go to create and update methods. Change the redirect value in line 21 to

redirect_to_project_url(@task.project_id)

large

Now, if you go back to your browser page and hit refresh, you can see this page now.

small

To check if the call backs are working, create another task in the same project and mark it as not complete. When you create the task, you can see that it has updated the Percent complete field for us.

small

In the next lesson, let's build an interface that shows all the tasks in every project.