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.

238 CHAPTER 9 File uploading<br />

Open app/assets/javascripts/tickets.js.coffee, and we’ll build up your function line<br />

by line so you can understand what you’re doing. Let’s begin by putting this line first:<br />

$(-><br />

It seems like a random amalgamation of characters, but this line is really helpful. It<br />

calls the jQuery $ 9 function and passes it a function as an argument. This line runs the<br />

function only when the page has fully loaded. 10 You need this because the JavaScript<br />

otherwise would be executed before the link you’re going to be referencing is loaded.<br />

Let’s add a second line to this:<br />

$(-><br />

$('a#add_another_file').click(-><br />

This line uses jQuery’s $ function to select an element on the page called a, which has<br />

an id attribute of add_another_file that will soon be your Add Another File link. This<br />

would happen only after the page is ready. After this, you call the click function on it<br />

and pass it a function that runs when you click on this link. Let’s now add a third line:<br />

$(-><br />

$('a#add_another_file').click(-><br />

url = "/files/new?number=" + $('#files input').length<br />

The double-space indent here indicates to CoffeeScript that this code belongs inside<br />

the function passed to click. 11 Here, you define a variable called url, which will be<br />

the URL you use to request a new file field on your page. At the end of this URL you<br />

specify the number parameter with some additional jQuery code. This code selects all<br />

the input elements inside the element on the page with the id attribute of files<br />

and stores them in an array. To find out how many elements are in that array, you<br />

call length on it. The URL for the first time you click this link would now look something<br />

like /files/new?number=1, indicating that you already have one file field on<br />

your page.<br />

Let’s make the fourth line now:<br />

$(-><br />

$('a#add_another_file').click(-><br />

url = "/files/new?number=" + $('#files input').length<br />

$.get(url,<br />

This line is pretty simple; you call the jQuery function $, and then call the get 12 function<br />

on it, which starts an asynchronous request to the specified URL that is the first<br />

argument here, using the variable you set up on the previous line. Another line:<br />

$(-><br />

$('a#add_another_file').click(-><br />

url = "/files/new?number=" + $('#files input').length<br />

9 Aliased from the jQuery function: http://api.jquery.com/jquery/.<br />

10 For the meaning of “loaded,” see this: http://api.jquery.com/ready<br />

11 http://api.jquery.org/click.<br />

12 http://api.jquery.com/jQuery.get.

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

Saved successfully!

Ooh no, something went wrong!