RailsCasts

23 views
Skip to first unread message

Tom Allison

unread,
Jan 12, 2012, 2:52:36 PM1/12/12
to rubyonra...@googlegroups.com
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?

Linus Pettersson

unread,
Jan 12, 2012, 2:57:10 PM1/12/12
to rubyonra...@googlegroups.com
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.

Tom Allison

unread,
Jan 13, 2012, 12:32:40 PM1/13/12
to rubyonra...@googlegroups.com
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?

> --
> You received this message because you are subscribed to the Google
> Groups "Ruby on Rails: Talk" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/rubyonrails-talk/-/5lOOaMurUM8J.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To unsubscribe from this group, send email to
> rubyonrails-ta...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.

Linus Pettersson

unread,
Jan 13, 2012, 2:00:24 PM1/13/12
to rubyonra...@googlegroups.com
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

Walter Lee Davis

unread,
Jan 13, 2012, 2:06:31 PM1/13/12
to rubyonra...@googlegroups.com

On Jan 13, 2012, at 2:00 PM, Linus Pettersson wrote:

> 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

>
> Cheers!
> Linus


>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.

> To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/MpHSG8PaijgJ.

djangst

unread,
Jan 13, 2012, 2:43:44 PM1/13/12
to Ruby on Rails: Talk
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.

On Jan 13, 2:00 pm, Linus Pettersson <linus.petters...@gmail.com>
wrote:

djangst

unread,
Jan 13, 2012, 2:48:41 PM1/13/12
to Ruby on Rails: Talk
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.

Javier Quarite

unread,
Jan 13, 2012, 2:49:33 PM1/13/12
to rubyonra...@googlegroups.com
On Fri, Jan 13, 2012 at 2:43 PM, djangst <dja...@gmail.com> wrote:
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.


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
Reply all
Reply to author
Forward
0 new messages