Rails Asset Pipeline not including same file in multiple manifests - ruby-on-rails

This is my application.js:
//= require jquery
//= require jquery_ujs
//= require_tree .
//= stub subscriptions
It has always worked as expected and subscription.js is another file which is compiled separately.
If I add this line at the top of subscriptions.js:
//= require jquery
The subscriptions.js is compiled as expected and includes jQuery, but jQuery disappears from application.js: in the browser console I get errors like
ReferenceError: jQuery is not defined.
I cannot find a way to have jquery compiled/included in both the manifest files.
I use a development environment with Rails 4.2.4 + Puma and the default jquery-rails gem.

It's the expected behavior of the stub directive: not only subscriptions but also all its dependencies will be stubbed.
The only solution is to avoid using require_tree and stub.

Related

RoR Spree Asset Differs on Production & Heroku

I'm tryin to solve this for nearly 2 days. I'm pretty mad at now.
I'm using Spree (latest version) with Rails 5. I'm trying to use W3's Shape template as theme. Css is ok and working fine but I can't manage to get workin javascript assets.
Here is my vendor/javascripts/spree/frontend/all.js
//= require jquery
//= require jquery_ujs
//= require accounting.min
//= require shape/all ---> As you can see i require my template's assets
//= require_tree .
//= require spree/frontend/spree_auth
//= require spree/frontend/spree_i18n
And here is the assets/javascripts/shape/all.js
//= require_tree .
And inside that Shape folder, there are javascript assets.
When in development mode, everything works perfect. There is no issue. But when i try to switch production mode or deploy to heroku, template's javascript assets does not included.
Thank you all.
Edit: For a temporary solution, I did include my problematic js assets by old classic html way. But still I'm really curios about how can this happen.

Grails 2.4 Include jquery-ui

My grails application is using jquery-ui and twitter bootstrap plugin. I use the default application.js structure. Then I add the following line to include the jquery-ui and bootstrap.js :
...
//= require jquery
//= require jquery-ui
//= require bootstrap
...
The bootstrap.js in loaded properly, but the jquery-ui.js is not included.
I had to use the following settings (for these versions):
grails-app/conf/BuildConfig.groovy
compile ":asset-pipeline:1.9.9"
runtime ":jquery:1.11.1"
runtime ":jquery-ui:1.10.3"
grails-app/assets/javascripts/foobar.js
//= require jquery
//= require js/jquery-ui-1.10.3.custom
grails-app/assets/stylesheets/foobar.css
/*
*= require themes/ui-lightness/jquery-ui-1.10.3.custom
*/
Assuming that you are using the jquery-ui grails plugin, it is not included, because the current version has no directive file under grails-app/assets/jquery-ui. Instead it uses the web-app directory, to put the javascript files in the subfolder jquery-ui/js as you see in the sources.
In order to get jquery-ui working you have to put the following line in your directive file:
//= require jquery
//= require jquery-ui/js/jquery-ui-1.10.3.custom.min
//= require bootstrap
In bootstrap it works out of the box, because they use a directive file under grails-app/assets/javascript as you see here.
this line also worked for me :
//= require jquery-ui/js/jquery-ui-1.10.3.custom.min

Bootstrap drowdown works locally but not in heroku

Currently working on the Ruby on Rails tutorial (railstutorial.org) and in Chap 8. you make a dropdown menu using bootstrap classes for signed in users.
To make this work the book says to include
//= require jquery
//= require jquery_ujs
//= require bootstrap
in your application.js file.
This does not work locally or on heroku.
However:
//= require bootstrap
//= require jquery
//= require jquery_ujs
works locally, not on heroku. This also creates a ton of error messages in the console (Uncaught TypeError: cannot read property 'fn' of undefined)
Every stack thread and googled page I go to says that bootstrap goes first in local and jquery first in production. Meanwile I'm pulling my hair out.
git hub: https://github.com/nelsonkhan/sample_app
heroku: https://guarded-shelf-3017.herokuapp.com/
you need to have all 4 of these:
//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require_tree .
Make sure your assets are properly compiled when you deploy on heroku. Check here for more details: https://devcenter.heroku.com/articles/rails-asset-pipeline
Changing production.rb helped for me
config.assets.compile = true

Rails. Large vendor.js?

I have vendor.js file in my rails project containing several vendor libraries altogether and after adding some new libraries I'm facing into this problem in development environment
Started GET "/assets/vendor.js" for 10.40.129.65 at 2013-03-03 14:01:24 +0400
Cache write: 0d6281da1957dd9c5e063fb636c50bd260dae5e2
Cache read: 0d6281da1957dd9c5e063fb636c50bd260dae5e2
Cache read: http://work:3000/assets/vendor.js?
Cache write: http://work:3000/assets/vendor.js?
!! Rack application returned nil body. Probably you wanted it to be an empty string?
!! Unexpected error while processing request: undefined method `each' for nil:NilClass
Trying to remove some libraries fixes the issue and precompiled assets works fine. But I want to make it work in development. Help me please.
vendor.js:
//= require ./vendor/ext/stacktrace.js
//= require jquery
//= require jquery_ujs
//= require underscore
//= require backbone
//= require twitter/bootstrap
//= require js_notifier
//= require ./vendor/ext/mustache-1.4.2
//= require ./vendor/ext/strftime
//= require ./vendor/ext/moment
//= require ./vendor/ext/langs.js
//= require ./vendor/ext/jquery.jgrowl_minimized.js
//= require ./vendor/ext/jquery-ui-1.10.0.custom.min.js
//= require ./vendor/ext/jquery-ui-sliderAccess.js
//= require ./vendor/ext/jquery-ui-timepicker-addon.js
//= require ./vendor/ext/backbone.paginator.js
//= require ./vendor/ext/jquery-deparam.min.js
//= require ./vendor/ext/select2.js
//= require_tree ./templates
//= require ./vendor/channels.js.coffee
//= require ./vendor/ext/bootstrap.switch.js
//= require ./vendor/ext/private_pub
//= require ./vendor/ext/spin.js
//= require ./vendor/ext/jquery.spin.js
I'm using Memcached + Dalli
UPD:
I've set cache_store in every environment explicitly (developemnt.rb, production.rb and test.rb) and this fixed the issue until I opened Google Chrome for which the issue raised again. As a result, in FF everything works fine and Chrome fails to load vendor.js (the same server instance (thin) and in the same time for both browsers)
log for ff request:
Cache read: http://work:3000/assets/vendor.js?
Cache read: 577d6ed0d98975b583329c312bd25667620f2497
log for chrome request (same as above):
Started GET "/assets/vendor.js" for 10.40.129.65 at 2013-03-03 20:37:11 +0400
Cache write: 577d6ed0d98975b583329c312bd25667620f2497
Cache read: 577d6ed0d98975b583329c312bd25667620f2497
Cache read: http://work:3000/assets/vendor.js?
Cache write: http://work:3000/assets/vendor.js?
!! Rack application returned nil body. Probably you wanted it to be an empty string?
!! Unexpected error while processing request: undefined method `each' for nil:NilClass
You may consider to apply index.js technique , as described in this Rails guide (2.1.2). Try to split your big file and include the parts in a manifest file , for example:
In your /app/vendor/assets/javascripts you create a new directory my_libraries and place parts in it . Then in the same directory you create index.js manifest file and include files as normal:
//= require file1
//= require file2
and in your application.js :
//= require my_libraries
EDIT: One solution would be : in your /vendor/ext (the correct path is app/vendor/assets/javascripts/ext, if you have a path like /vendor/ext it will not be clear it is meant to be managed as asset dir , and also it will not be clear what asset you include - js or css) to create index.js with content:
//= require_tree .
and in application.js :
//= require ext
and then you should remove all the references to vendor dir from your application.js manifest . Another mistake is including files in your manifest with .js prefix. In the manifest file you should state inclusions without prefix.
Take a look at the asset-pipeline's documentation explaining the require_tree usage.

Rails and codekit - how to disable rails coffescript compilation

I'm using rails 3.2, but I'm compiling my coffee files with CodeKit.
I still want my coffee files to live inside 'assets', but each time I restart rails, it finds them in there and tries to compile them itself.
My files live in /assets/cafe/myscript.coffee, and codekit compiles them into /assets/javascripts/myscript.js
The coffee-rails gem is already commented out in my gemfile (when rails tries to (re) compile it it gives "throw Error("LoadError: no such file to load -- coffee_script)" - though I really dont want it to even try compiling.
Setting "config.assets.compile = false" in application.rb results in "application.js isn't precompiled"
you should probably just configure your sprockets manifest to not require the whole tree.
edit app/assets/javascripts/application.js
typically it looks like this:
//= require jquery
//= require jquery_ujs
//= require_tree .
change that to
//= require jquery
//= require jquery_ujs
//= require myscript
An (inferior) workaround is to put my coffeescripts inside "App" rather than "assets" (so one more branch up the tree.)
This sucks because it's not where they should go, but at least it does put it outside rails' stalker-tendencies to find coffeescript files anywhere in assets and try to compile them.

Resources