27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

116 CHAPTER 5 Nested resources<br />

show action because the link defaults to the GET method. Upon running bin/cucumber<br />

features/deleting_tickets.feature, you’re told a destroy action is missing:<br />

When I follow "Delete Ticket"<br />

The action 'destroy' could not be found in TicketsController<br />

The next step must be to define this action, right? Open app/controllers/<br />

tickets_controller.rb, and define it directly under the update action:<br />

def destroy<br />

@ticket.destroy<br />

flash[:notice] = "Ticket has been deleted."<br />

redirect_to @project<br />

end<br />

With that done, your feature should now pass:<br />

1 scenario (1 passed)<br />

8 steps (8 passed)<br />

Yet again, check to see that everything is still going as well as it should by using rake<br />

cucumber:ok spec. If it is, you should see output similar to this:<br />

13 scenarios (13 passed)<br />

108 steps (108 passed)<br />

and<br />

5 examples, 0 failures, 4 pending<br />

Commit and push!<br />

git add .<br />

git commit -m "Implemented deleting tickets feature"<br />

git push<br />

You’ve now completely created another CRUD interface, this time for the tickets<br />

resource. This resource is accessible only within the scope of a project, so you must<br />

request it using a URL such as /projects/1/tickets/2 rather than /tickets/2.<br />

5.5 Summary<br />

In this chapter, you generated another controller, the TicketsController, which<br />

allows you to create records for your Ticket model that will end up in your tickets<br />

table. The difference between this controller and the ProjectsController is that the<br />

TicketsController is accessible only within the scope of an existing project because<br />

you used nested routing.<br />

In this controller, you scoped the finds for the Ticket model by using the tickets<br />

association method provided by the association helper method has_many call in your<br />

Project model. has_many also provides the build method, which you used to begin to<br />

create new Ticket records that are scoped to a project.<br />

In the next chapter, you learn how to let users sign up and sign in to your application<br />

using a gem called devise. You also implement a basic authorization for actions<br />

such as creating a project.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!