2

I should probably first mention that I do not have precompiling on.

I have 8 different Js files (7, excluding Application.js) and when I use <%= javascript_include_tag 'application' %> it prints out:

<script src="/assets/admin.js?body=1" type="text/javascript"></script>
<script src="/assets/brand.js?body=1" type="text/javascript"></script>
<script src="/assets/category.js?body=1" type="text/javascript"></script>
<script src="/assets/home.js?body=1" type="text/javascript"></script>
<script src="/assets/product.js?body=1" type="text/javascript"></script>
<script src="/assets/setting.js?body=1" type="text/javascript"></script>
<script src="/assets/user.js?body=1" type="text/javascript"></script>
<script src="/assets/application.js?body=1" type="text/javascript"></script>

Because of this, some of my jQuery (which uses Toggles) do not work because they are being executed multiple times.

How do I get it to simply use application.js?

My Application.js file:

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery_ujs
//= require jquery.ui.all
//= require_tree .
Stefan Dunn
  • 5,363
  • 7
  • 48
  • 84
  • What does your `application.js` file look like? `application.js` is really just supposed to be a manifest for including all of your other files. Maybe I'm missing something and don't fully understand your question. What files are being duplicated? – theIV May 17 '13 at 14:37
  • All the Js files are merged into application.js, which means that they are no longer required to be linked in the HTML. Because they are, the code is being duplicated in the seperate JS files and the application.js file. I've updated my question with my application.js manifest – Stefan Dunn May 17 '13 at 14:41

4 Answers4

3

In addition to removing //= require_tree . as Mike said. Try the following command:

$ rake tmp:clear tmp:create assets:clean

This will clear your temporary files & cached asset files.

Further, if you simply want single application.js instead of 7 .js include script tags. set the following option config/environments/development.rb

# Expands the lines which load the assets
  config.assets.debug = false

Hope it helps

CuriousMind
  • 33,537
  • 28
  • 98
  • 137
0

//= require_tree . loads everything in the same directory as that manifest (.).

Simply remove that line if you want to include your javascripts manually. If you're including all your javascript on every page, however, leave that line in there and remove your includes.

Mike Campbell
  • 7,921
  • 2
  • 38
  • 51
0

your problèm is that application.js load all js files in the current directory because of the line "//= require_tree ." and probably you use the same names (of id and class) for your html elements in different pages , so one solution is to continue to use "//= require_tree ." in your application.js and give unique name for each elements in your pages, the other solution is to delete the "// = require_tree ." from your application.js and use this :

<%= javascript_include_tag "application", controller_name %>

here when you generate a new controller, rails will create a javascript file with the name of the controller automatically for you, and when you add "controller_name" option for javascript_include_tag, js file for the current controller we'll be added, finally you place your javascript instructions in these file switch controller and here we go.

i find this method verry good but there are other solutions you can find here some other answers in this subject :

Rails 3.1 asset pipeline: how to load controller-specific scripts?

good luck ;)

Community
  • 1
  • 1
medBouzid
  • 7,484
  • 10
  • 56
  • 86
0

I had the same problem. Check if you aren't adding on application.html.erb other js files. because if you have the //= require_tree .On the application.js it adds everything, so if you add it on application.html.erb they will repeat.

campagnolo_1
  • 2,710
  • 1
  • 17
  • 25
tiagotex
  • 56
  • 5