0

I don't quite understand how Rails includes JavaScript files. Google and the following documentations didn't solve my problem:
http://railsapps.github.io/rails-javascript-include-external.html
http://guides.rubyonrails.org/asset_pipeline.html

I understand that Rails uses manifest-files (e.g. applications.js) to pack every included JavaScript file into one big file (performance reasons, etc.).

In application.html.erb you need to include this manifest, like

<%= javascript_include_tag :application %>

I added a JavaScript file to /app/assets/ (e.g. my_javascript.js).
So, if I look at the Source-Code, I can find

<script src="/assets/application.js" type="text/javascript"></script>

and if I open the file, the content of my_javascript.js is rendered in the application.js file.

My Problem: The function is not working. But if I directly include the my_javascript.js in the application.html.erb, like

<%= javascript_include_tag "my_javascript" %>

it is working! Of course, it renders a second script-tag, like:

<script src="/assets/my_javascript.js" type="text/javascript"></script>

So finally, why does the function work if I include it in specific and not if I use the manifest (how it's supposed to)? Do I need to consider something special about the Assets-Pipeline? My manifest includes

//= require_tree .

but I even tried it with

//= require my_javascript

I'm on Rails 3.2.3 with Ruby 1.9.3.

Thank you in advance!

michi
  • 103
  • 1
  • 8
  • Does this problem appear in development env or in production? – hedgesky Sep 03 '13 at 12:37
  • 1
    When you say, *the content of my_javascript.js is rendered in the application.js file* are there other things in the `application.js` file as well? When it's all together in that file, are any javascript syntax errors introduced? A small error could cause it to ignore the entire script. – lurker Sep 03 '13 at 12:40
  • Are both ` – Geeky Guy Sep 03 '13 at 12:40
  • @hedgesky: It happen in development and production @mbratch: I just deleted everything in the the `application.js` and just included `my_javascript.js` still the same – michi Sep 03 '13 at 13:13
  • OK, I need to take that back. If I exclude everything else of the js. file it is working! It's not working if I include controls.js, dragdrop.js, effects.js or rails.js (which are default-rails javascrpits I think) one of my includes are `jquery-1.9.1` `jquery-ui-1.10.3`. Might this cause a conflict? – michi Sep 03 '13 at 13:22
  • You need to look at your `application.js` very carefully after including your new script from `my_javascript.js` to make sure you aren't introducing a syntax error. Without details on the file contents themselves, it's difficult for us to solve here. – lurker Sep 03 '13 at 14:19
  • You said that you added the javascript file to **app/assets/**, but with the asset pipeline, you should add it to **app/assets/javascripts/** instead. – jvperrin Sep 03 '13 at 16:25
  • @jvperrin: I actually added it to app/assets/javascripts/, sorry for the misunderstanding. – michi Sep 08 '13 at 09:00
  • @mbratch: you're probably right. I'll check the syntax of the JavaScript. It seems it is probably more an issue of the JavaScript Code, than any Rails problem. Thanks guys for your help! – michi Sep 08 '13 at 09:01

1 Answers1

0

Try putting in Application Layout

<%= yield :head %> in the between head tags.

Hamza Ouaghad
  • 589
  • 10
  • 23