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.

Using JavaScript<br />

What does matter here is the number variable that identifies the number of file<br />

fields you are currently up to. You specify the child_index option in your fields_for<br />

so that each time these fields are rendered, they’re given different identifiers. The<br />

assets form partial gets this number variable only from the tickets partial; you’ve yet to<br />

set up a way the new action sets this variable. Without this variable set in the new<br />

action, you can’t render the app/views/files/_form.html.erb for a new asset without<br />

knowing what number you’re up to. Let’s set that up now.<br />

9.4.5 Sending parameters for an asynchronous request<br />

The number variable indicates what file field you are up to, so you need a way to tell<br />

the new action in FilesController how many file fields are currently on the page.<br />

Previous versions of Rails had an option for this called :with, which has now, unfortunately,<br />

been removed. No matter, you can do it in JavaScript. It’s better to put this<br />

code in JavaScript anyway, because it’ll already be using some to determine the number<br />

to pass through. Rather than using pure JavaScript, you’ll be using CoffeeScript,<br />

which comes with Rails 3.1 but can be used in any other language. Let’s learn some<br />

CoffeeScript now.<br />

LEARNING COFFEESCRIPT<br />

CoffeeScript is, in the words of its website, “a little language that compiles into<br />

JavaScript.” It’s written in a simple syntax, like this:<br />

square = (x) -> x * x<br />

This code compiles into the following JavaScript code:<br />

var square;<br />

square = function(x) {<br />

return x * x;<br />

};<br />

In the CoffeeScript version, you define a variable called square. Because this isn’t yet<br />

initialized, it is set up using var square; in the JavaScript output. You assign a function<br />

to this variable, specifying the arguments using parentheses (x) and then specifying<br />

the code of the function after ->. The code inside the function in this case is<br />

converted into literal JavaScript, making this function take an argument, multiply it by<br />

itself, and return the result.<br />

Although this is a pretty basic example of CoffeeScript, it shows off its power. What<br />

you would write on four lines of JavaScript requires just one line of extremely easy-tounderstand<br />

CoffeeScript.<br />

Each time you generate a controller using Rails, a new file called app/assets/<br />

javascripts/[controller_name].js.coffee is created. 8 This file is created so you have a<br />

location to put CoffeeScript code that is specific to views for the relevant controller.<br />

This is really helpful in your situation, because you’re going to use some CoffeeScript<br />

to tell your Add Another File link what to do when it’s clicked.<br />

8 If you have the coffee-rails gem in your Gemfile.<br />

237

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

Saved successfully!

Ooh no, something went wrong!