RailsCasts

I'm curious about a conflict between railscasts and rails 3.1 documention.

Do I put javascript code into application.js in the assets folder or not?
And what's the ramifications of either approach?

As it says in application.js:

"

// This is a manifest file that’ll be compiled into including all the files listed below.

// Add new JavaScript/Coffee code in separate files in this directory and they’ll automatically
// be included in the compiled file accessible from http://example.com/assets/application.js
// It’s not advisable to add code directly here, but if you do, it’ll appear at the bottom of the
// the compiled file."

I find it good to put the code in different files. It makes it easier to find the correct code when debugging.

So I could put my code into another file "foo.js" and 'require' it in application.js.

I believe these other .js files would be in the assets directory to be picked up?

That is correct. The .js (or .js.coffee if using coffeescript) files should be in app/assets/javascript/.

You could use require tree which is specified from the beginning and will include all javascript files. I do prefer to require each file separately though as it gives me more control.

Cheers!

Linus

That is correct. The .js (or .js.coffee if using coffeescript) files should be in app/assets/javascript/.

You could use require tree which is specified from the beginning and will include all javascript files. I do prefer to require each file separately though as it gives me more control.

Especially important if you are requiring files in a particular order. Require tree sorts them in *alphabetical* order, which can play merry hob with your library code (libraries usually have to come first, in a particular order).

Walter

I find it confusing, because the language in application.js indicates
that separate javascript files need only be included in that directory
and they'll automatically be compiled, while other sources say files
must be specifically required.

In fact application.js says "not advisable to add code directly here,"
which I read as "don't do your requiring here, pal," implying that it
isn't necessary.

Guides didn't make it any clearer.

And I'm also interpreting "(don't) add code directly here" as not to
uncomment require_tree, because isn't uncommenting equivalent to
adding?

But again other sources say to use require_tree to include separate
files in that directory.

I guess if … lets say you have 2 files

tasks.js

activities.js

if you put require_tree inside your application.js it will include that files using this

<%= javascript_include_tag “application” %>

but if you just only need tasks.js then

<%= javascript_include_tag “tasks” %>

and that’s all

Javier