I have //= require loader in my application JS file and I want to see the file. I do not see it in my JS folder. Where else could it be? How to I find the path to it?
Related
I can run jQuery on local production doing rails s -e production but when the same project is deployed to Heroku, jQuery doesn't work and I get an error in the console that says
"Uncaught ReferenceError: $ is not defined"
. I am not using Turbolinks so I don't think I need to do anything besides
$(document).ready(function() {
//everything js goes here });
Normal JavaScript works though.
I checked in the console on Chrome with $("#element-id") and it gave details of the element, so I think jQuery is being loaded. Or is it that Chrome console has jQuery by default?
My application.js
// 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, or any plugin's
// vendor/assets/javascripts directory 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
// compiled file. JavaScript code in this file should be added after the last require_* statement.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require Chart.min
//= require_tree .
Shouldn't the local production environment be the same as the Heroku? Why is the behavior different on Heroku and local production environment?
I installed a plugin (http://antenna.io/demo/jquery-bar-rating/examples/) using Bower.
Now I have a file:
vendor/assets/bower_components/jquery-bar-rating/jquery.barrating.js
Also in my application.rb I have:
config.assets.paths << Rails.root.join('vendor', 'assets', 'bower_components')
I restarted my Rails server but when I access the app it says:
Sprockets::FileNotFound: couldn't find file 'jquery.barrating' with type 'application/javascript'
Checked in these paths:
/Users/xxx/Documents/Dev/xxx/Backend/app/assets/documentation
/Users/xxx/Documents/Dev/xxx/Backend/app/assets/images
/Users/xxx/Documents/Dev/xxx/Backend/app/assets/javascripts
/Users/xxx/Documents/Dev/xxx/Backend/app/assets/stylesheets
/Users/xxx/Documents/Dev/xxx/Backend/vendor/assets/bower_components
So it looks in the right folder but still can't find the file.
Any idea what is happening?
I can't entirely remember how I did it a while ago, but I think basically if you need the file in the plugin within your js, you could do:
//= require jquery-bar-rating
//OR
//= require jquery-bar-rating/jquery.barrating
I tried to load i18n.js and translations.js in the application.js.erb
However the file i18n.js could be found, but the translations.js could not found.
it shows me this exception ActionView::Template::Error (couldn't find file 'translations.js')
How could I fix the problem? I've also tried to load ../../public/javascript/translations.js but it didn't work as well.
public/javascripts
-i18n.js
-translations.js
pplication.js.erb
//= require i18n
//= require i18n.js
//= require translations.js
The asset pipeline and public/ have nothing to do with each other.
If you want to include the files via the asset pipeline, they need to go under app/assets, lib/assets or vendor/assets.
Anything placed in public/ is not part of the asset pipeline and you'll need to include it via its own <script src="/i18n.js"> tag.
Just a few questions to clarify some confusing factors for me.
About application.js:
require_tree . will recursively include all the js files within app/assets/javascripts. If I put a javascript file in app/assets/javascripts/subfolder, it will be included. If I just want to include a specific directory, I should use require_directory
lib/assets/javascripts and vendor/assets/javascripts can be referenced from the manifest, application.js. Their javascript files WILL NOT be precompiled unless they are stated in the manifest.
When I install a gem that requires a set of javascripts(e.g. bootstrap) I require the related javascripts files in the manifest too (e.g. //= require bootstrap). The javascript files live in the Gem path, and they can be referenced by relative paths too.
Are my statements all true?
For sure I can say that 1 & 3 are true, I use both of those statements in my code.
When it comes to numer 2, as Rails Asset Pipeline docs says:
For example, these files:
app/assets/javascripts/home.js
lib/assets/javascripts/moovinator.js
vendor/assets/javascripts/slider.js
would be referenced in a manifest like this:
//= require home
//= require moovinator
//= require slider
http://guides.rubyonrails.org/asset_pipeline.html#asset-organization
so here is my problem
sprocket defines these directives
include
require_directory
require_tree
require_self
depend_on
stub
so what I miss is an include_tree directive to include all javascript sources (placed in another directory) in one file!
what is the best way to do that?
Let's say your js files are in the directory lib/assets/your_library/yourfiles.js (notice , that it should be an assets directory). You just have to create an index file , named index.js ( it is a manifest file too) and insert a directive like this :
//= require_tree .
(or files you wish to be included)
After that , in your application.js insert :
//= require your_library
More about using index files in asset-pipeline can be found in this Rails guide.