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.

Attaching a file<br />

bottom of features/creating_tickets.feature that creates a ticket with an attachment, 2 as<br />

shown in the following listing.<br />

Listing 9.1 features/creating_tickets.feature<br />

Scenario: Creating a ticket with an attachment<br />

When I fill in "Title" with "Add documentation for blink tag"<br />

And I fill in "Description" with "The blink tag has a speed attribute"<br />

And I attach the file "spec/fixtures/speed.txt" to "File"<br />

And I press "Create Ticket"<br />

B<br />

Attach file<br />

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

Then I should see "speed.txt" within "#ticket .asset"<br />

In this feature you introduce a new concept: the Attach the File step B of this scenario,<br />

which attaches the file found at the specified path to the specified field. The<br />

path here is deliberately in the spec/fixtures directory because you may use this file for<br />

functional tests later. This directory would usually be used for test fixtures, except that<br />

at the moment, you don’t have any. 3 Create the spec/fixtures/speed.txt file now, and<br />

fill it with some random filler text like this:<br />

The blink tag can blink faster if you use the speed="hyper" attribute.<br />

Try running this feature using bin/cucumber features/creating_tickets.feature<br />

:37 and see how far you get. It fails on the attaching step because the File field isn’t<br />

yet available:<br />

And I attach the file "spec/fixtures/speed.txt" to "File"<br />

cannot attach file, no file field with id, name,<br />

or label 'File' found (Capybara::ElementNotFound)<br />

Add the File field to the ticket form partial directly underneath the p tag for the<br />

description field using the code in the following listing.<br />

Listing 9.2 app/views/tickets/_form.html.erb<br />

<br />

<br />

<br />

<br />

You call this field asset internally, but the user will see File. The reason for this is<br />

explained a little later.<br />

In earlier versions of Rails, you were required to specify that this form is multipart.<br />

A multipart form should be used for any HTML form that contains a file-upload field.<br />

In earlier versions, if you didn’t enable this form setting, you’d only get the filename<br />

from the field rather than the file itself. In Rails 3.1, you don’t need to do this because<br />

it’s done automatically if the form uses the file_field. But it’s preferable to indicate<br />

2 Please note that although the blink tag was once a part of HTML, it should never be used. Same goes for the<br />

marquee tag.<br />

3 Nor will you ever, as factories replace them in your application.<br />

215

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

Saved successfully!

Ooh no, something went wrong!