With ruby on rails application, does assets needs to be totally recompiled? or is RoR smart enough to pick out the assets that did not change?
ie) If I changed 1 js file out of 100 js files, is there a way to recompile the 1 js file? OR do I have to wait for 100 js files to be compressed?
Rails doesn't differentiate between modified and unmodified assets as of yet. However, you can install turbo-sprockets-rails3 gem for this purpose.
Related
Many jQuery plugins have the following directory structures:
/<plugin name>
../css
../images
../js
The CSS files usually have relative links to the images in them. What I want to do is include these plugins in the Rails Way under the Asset Pipeline, and hopefully that doesn't involve having to renamed the file references to remove the relative links. Is there such a Rails Way?
Could it also be that it's overkill to include an already-minified jQuery plugin in the Asset Pipeline?
You should try to add your assets to the load path which is the recommended way, as far as I know. If the application you're running has the assets-pipeline activated, it should find your assets after expanding the path in your application.rb
config.assets.paths << Rails.root.join("plugins/plugin_name/assets/")
Not shure, if this is what you asked for but if not, you should check: http://guides.rubyonrails.org/asset_pipeline.html#asset-organization
Remeber to restart your server
I had the same issue and also tried to find "the Rails way" to do this. And this is what I ended up with at the end of the day:
As Rob already mentioned:
vendor/assets is for assets that are owned by outside entities, such as code for JavaScript plugins and CSS frameworks.
Source: 2.1 Asset Organization
Lets take a practical example: using the jquery_datepicker gem (Note: we had to use a workaround because of this issue: bundle pack does not work with git sources).
1) Installing the gem (pretty straighforward):
cd vendor/gems
git clone https://github.com/albertopq/jquery_datepicker.git
2) Add this to your Gemfile
gem 'jquery_datepicker', :path => 'vendor/gems/jquery_datepicker'
3) Install a jquery-ui theme
From ThemeRoller select a theme, check Datepicker and Slider
and the jQUery version
Download and extract the content of the package
CSS/images from the css/theme-name folder move them:
jquery-ui-1.8.xx.custom.css to app/vendor/stylesheets/
the images folder to app/vendor/images/ (yes, move the entire folder images so you end up with something like this app/vendor/images/images/ui-icons_256x240.png
i18n from the development-bundle/ui/i18n folder (optional) move them to:
Create a folder i18n under app/vendor/javascripts/
move jquery.ui.datepicker-xx.js to this folder app/vendor/javascripts/i18n/
make sure the i18n folder is loaded so include in application.js
//= require_directory ./i18n
vendor/assets is loaded automatically AFAIK so you don't have to include the path in the asset pipeline.
I'd like to see how others are approaching this, it's a very good question.
I think the reason you haven't received an answer is because it's kind of unclear what you're asking. Are you asking if it's overkill to put your plugins in the asset pipeline? Are you asking if you have to rename file references?
I always put all my jquery plugins in my asset pipeline. Overkill or not, there all in one place and they only get compiled once so even if compiling them takes longer, it doesn't affect my app.
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'm in the process of upgrading a 3.0 Rails App to 3.1.4 including the Asset Pipeline.
I'm on Heroku, so I'm I have this in my application.rb
config.assets.initialize_on_precompile = false
I noticed that when I run:
bundle exec rake assets:precompile
it creates files in a public/assets directory (even though my assets are in app/assets already).
For example, it creates files like application-72b2779565ba79101724d7356ce7d2ee, as well as replicating the images I have in app/assets.
My questions are:
(1) should be uploading these files to my production server?
(2) if I'm suppose to be uploading these, am I suppose to update each application-xxxxxxxx or only the latest one?
To your first question: Heroku will not allow you to modify the filesystem. So your assertion is correct- You will need to pre-compile the asset pipeline before you send it up to Heroku, so that it can be utilized in your production environment.
And the latter: You'll want to make sure you have the latest compilation. Any others wont be used. The "xxxxxxx" portion is to make sure that your users have the latest and greatest version of your assets. It's a way of versioning what the browser gets, and making sure they're not caching a bad copy of the JavaScript, when you want to set up their cache to hold on to the JS and CSS files as long as they can, instead of constantly getting it from your web server.
Take my Heroku comments with a slight grain of salt, as I have not deployed to Heroku before. I just know how their system works to some degree.
I've fixed all the errors about "xxx" is not compiled and all the assets show up -- when running locally everything works fine:
All ajax requests work
Form submissions use the rails remote tag and fire off properly
However when running in production mode locally (and on Heroku):
Some ajax will work -- however things like PUT's that should be updatating records (and do in dev) don't... They will hit the page but not do the actual database update
Remote forms are completely broken and resulting in regular form submission
The source can be cloned from here: https://github.com/bluescripts/reru_scrum
Maybe I'm miscompiling the assets wrong or maybe I'm missing an appropriate include in my application.js file?
I've been compiling via:
rake assets:precompile
You're missing //= require jquery_ujs in your application.js. This file comes with jquery-rails gem and is responsible besides other things for handling remote links and forms.
Btw, I'd suggest removing .Gemfile.swp from your repo and adding .*.swp to .gitignore.
I am running rails 3.0.1, and while we intend to move to 3.1 and the asset pipeline I am looking for solutions/workarounds for asset generation during unicorn hot restarts.
Essentially what happens currently is we deploy to our site and when the deploy is complete the "current" symlink is changed to the new directory which does not have the static assets yet. Then approximately 60 seconds later all assets are generated and all is well, but for the 60 seconds before the assets are generated our site is basically down.
Questions:
If I copy the files from the old release dir, to the new dir will the asset generation overwrite the older files I've copied?
Where in the rails code is the asset generation done? ActionPack Dispatcher?
Is the generation of these assets done upon initialization of the app or at another stage?
If you layout is something like this:
/srv/yourapplication/current
You should have a shared folder
/srv/yourapplication/shared/assets
and symlink public/assets to /srv/yourapplication/shared/assets
This way each time your old assets and new assets will all be in the same folder and will be served properly.