Stefan Dunn
Stefan Dunn

Reputation: 5513

Ruby on Rails duplicate of JS files included in HTML

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 .

Upvotes: 2

Views: 1708

Answers (4)

tiagotex
tiagotex

Reputation: 56

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.

Upvotes: 0

medBouzid
medBouzid

Reputation: 8382

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 ;)

Upvotes: 0

CuriousMind
CuriousMind

Reputation: 34135

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

Upvotes: 3

Mike Campbell
Mike Campbell

Reputation: 7978

//= 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.

Upvotes: 0

Related Questions