Chapter 5. Nested resources

 

This chapter covers

  • Building a nested resource
  • Declaring data associations between two database tables
  • Working with objects within an association

With the project resource CRUD done, the next step is to set up the ability to create tickets within the scope of a given project. The term for performing actions for objects within the scope of another object is nesting. This chapter explores how to set up nested routing for Ticket resources by creating a CRUD interface for them, scoped underneath the projects resource that you just created.

5.1. Creating tickets

To add the functionality to create tickets underneath the projects, you first develop the Cucumber features and then implement the code required to make them pass. Nesting one resource under another involves additional routing, working with associations in Active Record, and using more before_filters. Let’s get into this.

To create tickets for your application, you need an idea of what you’re going to implement. You want to create tickets only for particular projects, so you need a New Ticket link on a project’s show page. The link must lead to a form where a title and a description for your ticket can be entered, and the form needs a button that submits it to a create action in your controller. You also want to ensure the data is valid, just as you did with the Project model. Start by using the code from the following listing in a new file.

5.2. Viewing tickets

5.3. Editing tickets

5.4. Deleting tickets

5.5. Summary