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.

100 CHAPTER 5 Nested resources<br />

New Ticket link on a project’s show page. The link must lead to a form where a title<br />

and a description for your ticket can be entered, and the form needs a button that<br />

submits it to a create action in your controller. You also want to ensure the data is<br />

valid, just as you did with the Project model. Start by using the code from the following<br />

listing in a new file.<br />

Listing 5.1 features/creating_tickets.feature<br />

Feature: Creating Tickets<br />

In order to create tickets for projects<br />

As a user<br />

I want to be able to select a project and do that<br />

Background:<br />

Given there is a project called "Internet Explorer"<br />

And I am on the homepage<br />

When I follow "Internet Explorer"<br />

And I follow "New Ticket"<br />

Scenario: Creating a ticket<br />

When I fill in "Title" with "Non-standards compliance"<br />

And I fill in "Description" with "My pages are ugly!"<br />

And I press "Create Ticket"<br />

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

Scenario: Creating a ticket without valid attributes fails<br />

When I press "Create Ticket"<br />

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

And I should see "Title can't be blank"<br />

And I should see "Description can't be blank"<br />

When you run the code in listing 5.1 using the bin/cucumber features/<br />

creating_tickets.feature command, your background fails, as shown here:<br />

And I follow "New Ticket"<br />

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

You need to add this New Ticket link to the app/views/projects/show.html.erb template.<br />

Add it underneath the Delete Project link, as shown in the following listing.<br />

Listing 5.2 app/views/projects/show.html.erb<br />

<br />

This helper is called a nested routing helper and is just like the standard routing helper.<br />

The similarities and differences between the two are explained in the next section.<br />

5.1.1 Nested routing helpers<br />

In listing 5.2, you used a nested routing helper—new_project_ticket_path—rather<br />

than a standard routing helper such as new_ticket_path because you want to create a<br />

new ticket for a given project. Both helpers work in a similar fashion, except the<br />

nested routing helper takes one argument always, the @project object for which you<br />

want to create a new ticket: the object that you’re nested inside. The route to any

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

Saved successfully!

Ooh no, something went wrong!