Steve
Steve

Reputation: 4566

Why does the rails 3 application.js file state that it's not advisable to add code there?

In rails 3 the application.js file was moved to assets>javascripts>application.js In this file there are pre-loaded comments that state:

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.

However, in many tutorials like railscasts they add code into the application.js file all the time. This was back when application.js was still in the public folder. Should this code not be inside application.js in rails 3? Thanks in advance

Upvotes: 1

Views: 864

Answers (3)

phoenixwizard
phoenixwizard

Reputation: 5209

First are we talking of the post Rails 3.1 application.js? If yes, then I would say it would be not a very good idea to write your code directly in application.js In the end of the day your assets are to be precompiled and all your JS is to be minified into a single application.js So I think to keep your code clean, you can write the relevant javascript in the respective files and might want to change the order of loading by explicitly mentioning them in application.js

Upvotes: 1

You knows who
You knows who

Reputation: 895

It's jut the coding convenience. If you put everything in one file (application.js), it will be messy in the long run and you will regret it.

But in tutorials, people just want to make things fast so they put all the code into application.js

Upvotes: 1

Wolfgang
Wolfgang

Reputation: 4925

I guess it depends on the application. If you have an application with just a few lines of javascript I would not put the JS in separate files. However for bigger applications it makes sense to have one file for each larger area of the app.

Additionally you still have the possibility to not include all the JS all the time, i.e. if you have a admin section, you could use the javascript_include_tag to only include admin.js in the relevant parts of your app.

Upvotes: 2

Related Questions