How do I tell Sprockets 4 to compile assets for a vendored gem? - ruby-on-rails-5

We have a Rails 5 application and recently updated Sprockets from the 3.x series to 4.0.2. Now Rails can't find an asset (CSS file) belonging to a vendored gem. Note that the gem is an engine and the asset is called from a template inside the engine.
The asset is referenced inside a file at vendor/gems/our_vendored_gem/app/views/layouts/ like this:
<%= stylesheet_link_tag "our_vendored_gem/application", :media => "all" %>
The file is at vendor/gems/our_vendored_gem/app/assets/stylesheets/our_vendored_gem/application.css
Then we get the error Sprockets::Rails::Helper::AssetNotPrecompiled in OurVendoredGem::Mocks#index.
Our app/assets/config/manifest.js looks like this:
//= link_tree ../images
//= link_directory ../javascripts .js
//= link_directory ../stylesheets .css
I tried adding
//= link_tree ../../../vendor/gems/our_vendored_gem/app/assets/stylesheets/ .css
...but that did not help. The vendored gem has a manifest file of its own but it is unclear to me if Sprockets is already reading it, or if it needs to be called somewhere.
What needs to happen to make this file available to Rails again?

After some digging, I found this issue comment which explains that a Rails engine should have its own manifest file; that file can then be included in the application manifest like this:
//= link my_engine
This will find a manifest file at my_engine/app/assets/config/my_engine.js and add those directives to those in the application manifest.

Related

javascript_include_tag :defaults does not work

I'm migrating an app to Rails 3. The following - which I've seen recommended in a few places - does not work:
<%= javascript_include_tag :defaults %>
In my case, it expands to this:
<script src="/assets/defaults.js" type="text/javascript"></script>
... which results in a 404. As I understand it, the :defaults is not supposed to include a file called "defaults.js"; it's supposed to include a few essential things like prototype.js and application.js.
Note that in my case, the following works fine. It's just that I'd rather use the official recommended way, if possible:
<%= javascript_include_tag :prototype %>
<%= javascript_include_tag :application %>
I'm running Rails 3.2.8 with ruby 1.9.3.
I do not have the following line in my config/application.rb (in any form). To migrate to Rails 3, I created a new Rails 3 application, and used that application.rb as a starting point:
config.action_view.javascript_expansions[:defaults] = %w(foo.js bar.js)
In app/assets/javascripts, I have:
Util.js
application.js
controls.js
dragdrop.js
effects.js
prototype.js
... and a bunch of stuff specific to my application.
Since Rails 3.1, it uses assets pipeline. It means you need to change your management of assets.
You have a assets/javascripts/application.rb file which contains something like this:
//= require jquery
//= require jquery_ujs
//= require_tree .
It seems you include jquery, jquery_ujs and all other files in the javascripts repository. With the last line you don't need to do anything else in this file. You just need to include the application file in your view and rails will manage everything:
<%= javascript_include_tag "application" %>
It's exactly the same thing with stylesheets.
Then in production environment, assets (images, stylesheets, javascripts) will be compiled and minified in order to be more efficient.
I suggest you read more about here
Hope this helps
Here's a lot more about the Asset Pipeline - http://guides.rubyonrails.org/asset_pipeline.html
It's meant to make it easier to break up your Javascript into separate files for development, yet compile and minimize them in production.

How to configure Rails 3 for JavaScript development

I'm moving an app from Rails 2 to Rails 3. I have a bunch of JavaScript files in the app, most of which are for working with Google Maps. Previously, my JS files were in public/javascripts/*.js. I noticed that they were not being loaded by any of the pages that used javascript_include_tag, which writes a JavaScript tag that tries to load the JS file from /assets/*.js. (e.g. /assets/application.js).
I read that Rails 3 expects JS files to be located in app/assets/javascripts/*.js. So I moved my JS files there, but they still won't be accessible at /assets/application.js, unless I run bundle exec rake assets:precompile first.
I can't have my JS development cycle be:
Make change to JS
Run bundle exec rake assets:precompile
Reload browser
Thats just too long. My question is how can I configure my app so that the development cycle is as follows?
Make change to JS
Reload browser
So far I've tried:
Enabling the line that reads "Bundler.require(:default, :assets,
Rails.env)" in config/application.rb
Setting config.assets.enabled = false (and true) in config/application.rb
Thanks in advance.
The Rails asset pipeline should be used with a manifest file. This allows Rails to concatenate all of your javascript when in production. In order to do this, you should have an application.js file that looks something like.
//= require jquery
//= require jquery_ujs
//= require_tree .
The important part here is the require_tree part. This tells Rails to include all of the other javascript files in the same folder. This means that you want both the manifest version of application.js and all of your custom javascript files to live in app/assets/javascripts.
A good guide for transitioning to the asset pipeline from Rails 2 can be found in this RailsCast: http://railscasts.com/episodes/282-upgrading-to-rails-3-1
recently i have this issues
I change file config/environments/development.rb
config.assets.digest = false
then that working

rails 3.1 upgrade with backbone, can't find ujs and remotipart files

I upgraded to rails 3.1.3 a few weeks ago, and moved a bunch of stuff around to use backbone.
Mostly, things are going well, but I just realized that the rails-ujs.js file and remotipart.js files are not in the assets directory.
I've looked for them in the old public/javascripts folder, as well as everywhere else, but no luck finding them.
I also can't seem to find the files in the git repos. Is there somewhere to download these files in Javascript? or do they need to be generated by rails? Is there a way to regenerate them?
---------------this could be my error ----------------------------
I just realized that with the assets packed, you can't just open a file from view source, they all show as non-existent.
I think theses files lived in the rails ujs plugins in asset directory. so you need to add it to your Gemfile and the asset pipeline should see them automaticaly.
all files here https://github.com/rails/jquery-ujs/tree/master/src are available to include in your js file with
//= require jquery
//= require jquery_ujs
see plugin readme for more informations
You need to include this in your Gemfile:
gem 'jquery-rails'
That is the official plugin for this code.
Run bundle install.
Then you add the lines:
//= require jquery
//= require jquery_ujs
to you application.js file.

Using Google CDN with the sprockets assets compiler

This seems quite trivial but couldnt find it in the forums or rails asset pipeline guides.
In short
How can I inform sprockets to skip the asset pipeline for the jquery ui css of the 'jquery-rails' gem.
In long:
I am trying to use 'jquery-rails' gems with the Google jquery CDN's.
Hence in the I am just including the jquery_ujs and adding the jquery related scripts in the application layout:
application.js.erb:
//= require bootstrap-twipsy.js
//= require jquery-glowing
//= require jquery_ujs
application.html.erb:
= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"
= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jqueryui.min.js"
For the css', I am not including the jquery-ui css in the application.css and adding it in the layouts file too.
application.css.scss.erb:
*= require_directory .
*= require active_scaffold
*= require_self
In production environment, the compiled css file includes the jquery-css code too, since its in the assets pipeline of the 'jquery-rails' gem.
So how can I inform sprockets to skip the asset pipeline for the jquery ui css of the 'jquery-rails' gem.
If this is not possible in rails, what is the best way to handle this sittuation.
I thought about removing the jquery-rails gem, and adding the jquery_ujs javascript manually to the system, but then I have to deal with the installation of new releases rather than executing a simple 'bundle update'
Thanks in advance,
The problem is in the active_scaffold gem, right here if you want the code reference. It includes the jquery-ui CSS.
To remove this you'll need to replicate the entire active scaffold css code in your app/assets/stylsheets folder (as my_active_scaffold), removing the link to jquery-ui. I am assuming that the ActiveScaffold erb at the end of the file will still run in this context.
Then just include it:
*= require my_active_scaffold

What is the purpose of config.assets.precompile?

In Rails 3.1, you must whitelist files that you want included in asset precompilation. You must open up config/environments/production.rb and explicitly include assets you want precompiled:
config.assets.precompile += ['somestylesheet.css']
If you don't do this this and you run rake assets:precompile, your asset will not be copied to public/assets, and your app with raise an exception(therefore causing a 500 error in production) when an asset is not found.
Why is this necessary? Why aren't all assets automatically precompiled?
This current approach creates extra code and stress when deploying. Wouldn't it be easier to blacklist/exclude assets so things worked right out of the box? Anyone else share these feelings?
Most assets are automatically included in asset precompilation. According to the RoR Guide on the Asset Pipeline:
The default matcher for compiling files includes application.js, application.css and all files that do not end in js or css: [ /\w+\.(?!js|css).+/, /application.(css|js)$/ ]
You would use config.assets.precompile if you have additional assets to include:
config.assets.precompile += ['admin.js', 'admin.css', 'swfObject.js']
Or you could overwrite it.
I think it has to do with the pipeline/sprockets ability to require separate files.
For example, I have an admin.js file in my app/assets/javascripts folder. But all it does is require several other .js files.
//= require jquery
//= require jquery_ujs
//= require jquery.colorpicker.js
//= require jquery.wysiwyg.js
//= require wysiwyg.image.js
//= require jquery.fileupload.js
//= require jquery.fileupload-ui.js
//= require codemirror.js
//= require css.js
//= require admin_load
This is because (a) I'm using external js plugins and (b) I like to keep things like jQuery onload handlers in separate files.
If every .js file was precompiled, then it would precompile each one of these individual files–which is totally unnecessary. All I want/need is the single admin.js file precompiled.
Same goes for CSS files.
The assets precompile to me is cool so you dont end up deploying assets that you do not want. Dont also forget about the uglifer gem that helps compress your javascripts. Imaging all this are not existing and you just deploy your app and you find out that you have unused css files and uncompressed javascripts. how would you feel. this is just my own opinion and i say the asset pipeline is the coolest thing in rails.. Being able to manage all your assets properly.
And mind you if i am rails i would not want to compile assets that you would not want so you would say in your mind why did this guy compile these assets.. :)