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.

214 CHAPTER 9 File uploading<br />

By providing Ticketee the functionality to attach files to the ticket, you provide the<br />

project owners a useful context that will help them more easily understand what the<br />

ticket creator means. Luckily, there’s a gem called Paperclip that allows you to implement<br />

this feature easily.<br />

Once you’re familiar with Paperclip, you’ll change your application to accept multiple<br />

files attached to the same ticket using a JavaScript library called Prototype (which<br />

comes with Rails) and some custom JavaScript code of your own. Because you’re using<br />

JavaScript, you have to alter the way you test parts of your application. To test<br />

JavaScript functionality, you’ll be using WebDriver, 1 which is a framework built for<br />

automatic control of web browsers. WebDriver is especially useful because you can use<br />

the same steps you use for standard Cucumber and Capybara tests and because Capybara<br />

will take care of driving the browser. By running the tests inside the browser, you<br />

ensure the JavaScript on the page will be executed, and then you can run the tests on<br />

the results. Pretty handy!<br />

Finally, you’ll see how you can restrict access to the files contained within the projects<br />

of your application so that confidential information isn’t shared with people who<br />

don’t have access to a particular project.<br />

File uploading is also useful in other types of applications. Suppose you wrote a<br />

Rails application for a book. You could upload the chapters to this application, and<br />

then people could provide notes on those chapters. Another example is a photo gallery<br />

application that allows you to upload images of your favorite cars for people to<br />

vote on. File uploading has many different uses and is a cornerstone of many Rails<br />

applications.<br />

9.1 Attaching a file<br />

You start off by letting users attach files when they begin creating a ticket. As<br />

explained before, files attached to tickets can provide useful context as to what feature<br />

a user is requesting or can point out a specific bug. A picture is worth a thousand<br />

words, as they say. It doesn’t have to be an image; it can be any type of file. This kind of<br />

context is key to solving tickets.<br />

To provide this functionality, you must add a file-upload box to the new ticket<br />

page, which allows users to select a file to upload. When the form is submitted, the file<br />

is submitted along with it. You use the Paperclip gem to store the file inside your application’s<br />

directory.<br />

9.1.1 A feature featuring files<br />

You first need to write a scenario to make sure the functionality works. This scenario<br />

shows you how to deal with file uploads when creating a ticket. Users should be able to<br />

create a ticket, select a file, and upload it. Then they should be able see this file, along<br />

with the other ticket details, on the ticket’s page. They may choose to click the filename,<br />

which would download the file. Let’s test all this by adding a scenario at the<br />

1 There’s a great post explaining WebDriver on the Google Open Source blog: http://google-opensource<br />

.blogspot.com/2009/05/introducing-webdriver.html.

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

Saved successfully!

Ooh no, something went wrong!