I've decided to use javascript_inlcude_tag 'google_analytics.js' in my templates, avoiding many of the gems that would get it done for me in favor of testing the new Google Analytics "Universal" features.
Thing is that Google Analytics provided code is not in Coffeescript, and so I add it to my vendor/assets/javascripts just like I've done with many other libraries used in my App. But unlike all the others, the javascript_include_tag approach is causing Rails to halt on the error:
Rails ActionView::Template::Error (google_analytics.js isn't precompiled)
The other vendor libraries are just being 'required', inside many of my Coofeescripts.
Does anybody know what is wrong with this current analytics javascript approach?
Likewise suggested by Dan McClain in this (related) post, the issue was solved by adding the following line in config/environments/production.rb:
config.assets.precompile += %w( google_analytics.js )
Done!
A previous comment:
Google Analytics provided code is not in Coffeescript, and so I add it to my vendor...
It is not necessary for the js to be in Coffeescript to be either included in your assets pipeline through a direct //= require google_analytics in application.js, being the extension of the file .js.coffee or just .js.
Said that and assuming that you are getting that error in the production environment it seems that you are not adding your js file to the assets precompile array. In your config/environments/production.rb the following line is needed:
config.assets.precompile += %w( google_analytics.js )
Hope that helps
Related
I am working on a fully functional, existing Rails app. A friend of mine just built a few new front-end pages for me, but he just built them in html, with some JavaScript files.
Adding in the HTML has been easy, but I cannot figure out how to get the corresponding JavaScript files working.
I have imported all the JavaScript files into the app/assets/javascripts folders, and added them to the application.js file like this:
//= require modules/alerts
//= require modules/dropdown
etc.
But none of the JavaScript shows up - I threw some console.log lines in them just to test, but the lines never show. What else do I need to do to connect these JavaScript files?
Current structure:
app
assets
javascripts
modules
application.js (also inside javascripts folder like modules folder)
I am also getting this alert in console:
mobile-nav.source.js:23 Uncaught ReferenceError: alerts is not defined
at mobile-nav.source.js:23
But just checked the mobile-nav.js file and line 23 has nothing about alerts.
I think my error is coming from modules/init-modules.js - I am adding in alerts: alerts to the var Modules object, and when I do that, it seems to break all the other js. Is there something I need to do first before I can add to this file?
EDIT: Found the issue! Thanks for everyone that helped. There was a variable being declared incorrectly, but the way it was throwing the error made it hard to find.
I already have these two gems in my application
gem 'faraday'
gem 'gettext_i18n_rails'
i wanted to get translations in my js file which resides in assets folder.
so i added gem
gem "gettext_i18n_rails_js", "~> 1.2.0"
and followed the guidelines in https://github.com/webhippie/gettext_i18n_rails_js. I didn't add any config files since i follow window.__ function. i called like this
window.__('sign_in.twitter')
in my js file but its throwing
Uncaught TypeError: window.__ is not a function
this error. Tried adding the config files as given in the documentation. Still not working. Please help.
Did you add following line to your js manifest?
//= require gettext/all
gettext/all.js is the file where window.__ function is defined. Check if it is really loaded.
I want to stay DRY in my code so I want to auto-load my javascripts file when it matches a controller or/and a method and the .js exists. I added this to my layout
= javascript_include_tag params[:controller] if ::Rails.application.assets.find_asset("#{params[:controller]}.js")
= javascript_include_tag "#{params[:controller]}/#{params[:action]}" if ::Rails.application.assets.find_asset("#{params[:controller]}/#{params[:action]}.js")
So now when I add javascripts/my_controller/my_method.js it automatically loads it, which's nice.
Sadly I must add another line to precompile the asset otherwise an error is thrown (which says I must precompile my .js file) and I didn't find any way around this.
Rails.application.config.assets.precompile += %w( orders/checkout.js )
Does anyone has a solution to avoid tu add manually elements in this configuration ?
NOTE : I already tried to use require_tree . which was just loading all the files on every page and was not working in my case.
You can use a wildcard to allow all JS files included in your views to be precompiled:
config.assets.precompile << '*.js'
My apologies if this has been posted somewhere else, but I'm not exactly sure how to even phrase a search for this problem.
Basically, I've got fullcalendar.js sitting in app/assets/javascripts/. Within my application.js file (in the same folder of course) I've got the following...
//= require jquery
//= require jquery_ujs
//= require foundation
//= require_tree .
So, as expected the fullcalender.js gets swept up in the asset compiling process and ends up in the application.js that is then served to the user.
The problem comes in when I try to reference a function defined in fullcalendar.js (and thus defined in application.js after compiling the assets) it throws an error stating TypeError: $(...).fullCalendar is not a function in the js console of firebug, and of course full calendar doesn't render.
Now, however, if I include <%= javascript_include_tag "fullcalendar.js" %> in the layout file after all of the other scripts and stylesheets have been pulled in, it works as expected.
Baffled, I looked into the js in each file and compared them and can't see a difference. Is there something going on with the asset pipeline that I'm not aware of or is this some freaky DOM issue? I just have no idea at this point.
For reference, here are the contents of both the application.js and fullcalendar.js that are linked to in the page source of the site. I've only included the relevant full calendar portion of each, as it's too large for gist or pastebin if I include everything.
What baffles me is that both the sources come from the same file, one is just linked to directly, the other flows through the asset pipeline process.
application.js - http://pastebin.com/byyNErB8
fullcalendar.js - http://pastebin.com/k4p29YmP
Any insight or help would be GREATLY appreciated.
Maybe a dependency error. I'll suggest you to use a better practice for such lib.
Put the fullcalendar scripts into vendor/assets/javascripts
Require this script manually in application.js before the tree and after jquery and any other lib it depends.
/=require 'fullcalendar`
Add
Check the loaded Javascript files in header, if application.js is the last, you have dependency error. The lib must be there before calling it.
I'm using Rails 3.2, It's set up for coffeescript. I know Coffeescript is an awesome language and it's not too hard to learn, but i'm JUST starting to wrap my head around Javascript and jQuery. So my question is this: Is there an easy way to set rails 3.2 up to use Javascript instead?
At the moment, my jQuery is in <script></script> tags in my view (timeline/index.html.erb). I'd like to move it into a .js file. I tried changing the name of my timeline.js.coffee to just timeline.js and putting the jQuery in there, but I get Uncaught SyntaxError: Unexpected token ILLEGAL.
What should I do besides the obvious answer of "learn coffeescript"?
I was on the right track. To switch to javascript in rails 3.2 you only need to remove the .coffee extension. However, you also need to make sure you use // for comments instead of #
//# Place all the behaviors and hooks related to the matching controller here.
//# All this logic will automatically be available in application.js.
//# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
Manifest Files and Directives
http://guides.rubyonrails.org/asset_pipeline.html#manifest-files-and-directives
add js files in app/assets/javascripts, then add the files in application.js
your js file
alert("here");
application.js
//= require_tree .
or
//= require your_js_file_name
then, it will be picked up from the code below in your app/views/layouts/application.html.erb
<%= javascript_include_tag "application" %>
$ rails -v
Rails 3.2.11
how about this?
http://bit.ly/VHEnBX
git clone it
cd js_test
bundle
rails s -d
open http://localhost:3000
# stop the detached server
kill -9 `cat tmp/pids/server.pid`
you should see here alert from the browser.
it's just a simple js file. no .coffee extension or whatsoever.
http://bit.ly/UPe2mp
you can simply comment out gem 'coffee-rails' in your Gemfile