27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Editing tickets<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 />

When I follow "Edit Ticket"<br />

Scenario: Updating a ticket<br />

When I fill in "Title" with "Make it really shiny!"<br />

And I press "Update Ticket"<br />

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

And I should see "Make it really shiny!" within "#ticket h2"<br />

But I should not see "Make it shiny!"<br />

Scenario: Updating a ticket with invalid information<br />

When I fill in "Title" with ""<br />

And I press "Update Ticket"<br />

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

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

the first two steps pass, but the third fails:<br />

When I follow "Edit"<br />

no link with title, id or text 'Edit Ticket' found<br />

To fix this, add the Edit Ticket link to the TicketsController’s show template,<br />

because that’s where you’ve navigated to in your feature. Put it on the line underneath<br />

the tag in app/views/tickets/show.html.erb:<br />

<br />

Here is yet another use of the Array argument passed to the link_to method, but<br />

rather than passing all Active Record objects, you pass a Symbol first. Rails, yet again,<br />

works out from this Array what route you wish to follow. Rails interprets this array to<br />

mean the edit_project_ticket_path method, which is called like this:<br />

edit_project_ticket_path(@project, @ticket)<br />

Now that you have an Edit Project link, you need to add the edit action to the<br />

TicketsController.<br />

5.3.1 Adding the edit action<br />

The next logical step is to define the edit action in your TicketsController, which<br />

you can leave empty because the find_ticket before filter does all the hard lifting for<br />

you (shown in the following listing).<br />

def edit<br />

end<br />

Listing 5.13 app/controllers/tickets_controller.rb<br />

113

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

Saved successfully!

Ooh no, something went wrong!