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.. :)
Related
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
I am close but seem to be missing something. I have dropped OpenLayers.js in app/assets/javascripts, the theme folder in app/assets/stylesheets and the img folder in app/assets.
I have *= require theme/default/style.css in my application.css file and //= require OpenLayers in my application.js file.
I am trying to load an OpenLayers map in my locations.js.coffee file but the img folder fails. OpenLayers tries to load the image files from /locations/img/... vs. /img/... which doesn't seem to work either.
I am still fuzzy on the asset pipeline in Rails and I am sure I am doing this wrong. I can't seem to find a concrete example of the best way to install OpenLayers in a Rails app. Any ideas or suggestions?
Drop the complete openlayers directory in vendor/assets/javascripts (to save some space I made a symbolic link):
vendor/assests/javascripts/openlayers
Add //= openlayers/OpenLayers to the app/assets/javascripts/application.js:
//= require openlayers/OpenLayers
//= require jquery
//= require jquery_ujs
//= require_tree .
First of all put your images in assets/images folder. That will make them accessible via http://localhost/assets/yourimage.png
Default behaviour in OpenLayers is to fetch images from img folder that should be located on the same level as OpenLayers.js in tree structure, which is not the case in Rails application.
To override this behaviour and make OpenLayers read images from Rails images catalog you should set the global variable OpenLayers.ImgPath = "/assets"
I have all the javascript files under app/assets/javascripts
Some of the javascript files
application.js
effect.js
rails.js
prototype.js
scriptaculous.js
When pre compiling assets rake assets:precompile it creating application.js in public/assets
But the application.js file included only the content present in app/assets/javascript/application.js ,not including other js file contents(efect,prototype).
If I add javascript files to config.assets.precompile like this
config.assets.precompile += %w( rails.js menu.js )
this create separate js file for each in public/assets
In my production.rb file
# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = true
# Compress JavaScripts and CSS
config.assets.compress = true
I need to combine all javascript files into singlefile.
I'm using prototype.
Anything I am missing?
On application.js you have to include the files you want to combine adding this in the beggining:
//= require effect
//= require rails
//= require prototype
//= require scriptaculous
or if they are all in the same folder
//= require_tree .
Then in your application you must include tags only for application.js, not for each one.
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.
My application is using both prototype and jquery and all files are being getting load by application.js file but i need to add jquery.noconflict(), how can i handle that and where to add this noconflict line.
By thinking some time got its solution. Beside of using noconflict of jquery i was also require to load files with particular order so they dont get conflict.
For this i overwrite application.js file and list down all required js files in required order and one of the js was having jquery.noconflict line to make jquery and protype both compatible.
like
// This is a manifest file that'll be compiled into including all the files listed below.
// Add new JavaScript/Coffee code in separate files in this directory and they'll automatically
// be included in the compiled file accessible from http://example.com/assets/application.js
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
//= require jquery
//= require jquery_ujs
//= require noconflict
//= require prototype
//= require compose
//= require s3_upload
and remove the last line require_tree so assets compiler does not add files by its own.
Thanks every one who look in to this question.