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.

Summary<br />

Now if you refresh this page and attempt to upload two files, you should see that it<br />

works. Does your scenario agree? Let’s find out by running bin/cucumber features/<br />

creating_tickets.feature:38:<br />

1 scenario (1 passed)<br />

18 steps (18 passed)<br />

Yup, all working! Great. You’ve switched the ticket form back to only providing one<br />

file field but providing a link called Add Another File, which adds another file field on<br />

the page every time it’s clicked. You originally had implemented this link using the<br />

:remote option for link_to, but switched to using CoffeeScript when you needed to<br />

pass the number parameter through. A couple of other small changes, and you got it<br />

all working very neatly again!<br />

This is a great point to see how the application is faring before committing. Let’s<br />

run the tests with rake cucumber:ok spec. You should see the following:<br />

46 scenarios (46 passed)<br />

469 steps (469 passed)<br />

# and<br />

30 examples, 0 failures, 13 pending<br />

Awesome! Let’s commit it:<br />

git add .<br />

git commit -m "Provide an 'Add another file link' that uses Javascript<br />

so that users can upload more than one file"<br />

git push<br />

This section showed how you can use JavaScript and CoffeeScript to provide the user<br />

with another file field on the page using some basic helpers. JavaScript is a powerful<br />

language and is a mainstay of web development that has gained a lot of traction in<br />

recent years thanks to libraries such as the two you saw here, jQuery and CoffeeScript,<br />

as well as others such as Prototype and Raphael.<br />

By using JavaScript, you can provide some great functionality to your users. The<br />

best part? Just as you can test your Rails code, you can make sure JavaScript is working<br />

by writing tests that use WebDriver.<br />

9.5 Summary<br />

This chapter covered two flavors of file uploading: single-file uploading and multiplefile<br />

uploading.<br />

You first saw how to upload a single file by adding the file_field helper to your<br />

view, making your form multipart, and using the Paperclip gem to handle the file<br />

when it arrives in your application.<br />

After you conquered single-file uploading, you tackled multiple-file uploading.<br />

You off loaded the file handling to another class called Asset, which kept a record for<br />

each file you uploaded. You passed the files from your form by using nested attributes,<br />

which allowed you to create Asset objects related to the ticket being created through<br />

the form.<br />

241

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

Saved successfully!

Ooh no, something went wrong!