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.

106 CHAPTER 5 Nested resources<br />

Then create the view for this action at app/views/tickets/show.html.erb using this code:<br />

<br />

<br />

<br />

<br />

The new method, simple_format, converts the line breaks 1 entered into the description<br />

field into HTML break tags () so that the description renders exactly how the<br />

user intends it to.<br />

Based solely on the changes that you’ve made so far, your first scenario should be<br />

passing. Let’s see with a quick run of bin/cucumber features/creating_tickets<br />

.feature:<br />

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

...<br />

2 scenarios (1 failed, 1 passed)<br />

16 steps (1 failed, 2 skipped, 13 passed)<br />

This means that you’ve got the first scenario under control and that users of your<br />

application can create tickets within a project. Next, you need to add validations to<br />

the Ticket model to get the second scenario to pass.<br />

5.1.6 Ticket validations<br />

The second scenario fails because the @ticket that it saves is valid, at least according<br />

to your application in its current state:<br />

expected there to be content "Ticket has not been created" in "[text]"<br />

You need to ensure that when somebody enters a ticket into the application, the<br />

title and description attributes are filled in. To do this, define the following validations<br />

inside the Ticket model.<br />

Listing 5.6 app/models/ticket.rb<br />

validates :title, :presence => true<br />

validates :description, :presence => true<br />

Now when you run bin/cucumber features/creating_tickets.feature, the entire<br />

feature passes:<br />

2 scenarios (2 passed)<br />

16 steps (16 passed)<br />

Before we wrap up here, let’s add one more scenario to ensure that what is entered<br />

into the description field is longer than 10 characters. You want the descriptions to be<br />

useful! Let’s add this scenario to the features/creating_tickets.feature file:<br />

Scenario: Description must be longer than 10 characters<br />

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

And I fill in "Description" with "it sucks"<br />

1 Line breaks are represented as \n and \r\n in strings in Ruby rather than as visible line breaks.

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

Saved successfully!

Ooh no, something went wrong!