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.

Deleting tickets<br />

In this section, you implemented edit and update for the TicketsController by<br />

using the scoped finders and some familiar methods, such as update_attributes.<br />

You’ve got one more part to go: deletion.<br />

5.4 Deleting tickets<br />

We now reach the final story for this nested resource, the deletion of tickets. As with<br />

some of the other actions in this chapter, this story doesn’t differ from what you used<br />

in the ProjectsController, except you’ll change the name project to ticket for your<br />

variables and flash[:notice]. It’s good to have the reinforcement of the techniques<br />

previously used: practice makes perfect.<br />

Let’s use the code from the following listing to write a new feature in features/<br />

deleting_tickets.feature.<br />

Listing 5.15 features/deleting_tickets.feature<br />

Feature: Deleting tickets<br />

In order to remove tickets<br />

As a user<br />

I want to press a button and make them disappear<br />

Background:<br />

Given there is a project called "TextMate 2"<br />

And that project has a ticket:<br />

| title | description |<br />

| Make it shiny! | Gradients! Starbursts! Oh my! |<br />

Given I am on the homepage<br />

When I follow "TextMate 2"<br />

And I follow "Make it shiny!"<br />

Scenario: Deleting a ticket<br />

When I follow "Delete Ticket"<br />

Then I should see "Ticket has been deleted."<br />

And I should be on the project page for "TextMate 2"<br />

When you run this using bin/cucumber features/deleting_tickets.feature, the<br />

first step fails because you don’t yet have a Delete Ticket link on the show template for<br />

tickets:<br />

When I follow "Delete Ticket"<br />

no link with title, id or text 'Delete Ticket' found (Capybara::ElementNotFound)<br />

You can add the Delete Ticket link to the app/views/tickets/show.html.erb file just<br />

under the Edit link (shown in the next listing), exactly as you did with projects.<br />

Listing 5.16 app/views/tickets/show.html.erb<br />

:delete,<br />

:confirm => "Are you sure you want to delete this ticket?" %><br />

The :method => :delete is specified again, turning the request into one headed for<br />

the destroy action in the controller. Without this :method option, you’d be off to the<br />

115

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

Saved successfully!

Ooh no, something went wrong!