rails asset pipeline asset group not precompiling datatables - ruby-on-rails

I started building a rails project that uses jquery-datatables-rails. When I put that into my Gemfile I did NOT put it into the assets group. Everything worked fine in development. When I went to put it into production I re-read the documentation and saw that it should be in the assets group so I moved that line in my Gemfile. Then I performed a rake assets:precompile and then ran rails server -e production.
The datatable doesn't work. In fact, the only way I can make it work is to take that line out of the assets group in my Gemfile and run in development mode. I've read a lot of conflicting information on the Internet about this.
Did I screw anything up by moving the line from outside the assets group into the assets group? I would like to be able to run this in production and I want to have a Gemfile that is consistent with the jquery-datatables-rails documentation.

A while back in response to a different problem that I was having, I moved require twitter/bootstrap above require jquery in app/assets/javascripts/application.js. As a result the javascript wasn't working right. Moving it into the right place solved the problem and inexplicably did not cause the old problem to return.
So the fix is, make sure that jquery is at the top of your application.js file.

Related

How to tell Rails to not clean some assets in the public folder

The issue here is that I have Bootstrap on production looking for the fonts at:
assets/spree/fonts/glyphicons-the-file-name.something
When in development mode, it looks for these assets in:
fonts/glyphicons-the-file-name.something
So what I did was I added the fonts folder into public and it all worked. I did the same for production. You can guess that I'm now dealing with a rails assets:clean issue that must be running and removing the files, hence not allowing them to appear.
Is there a way to tell Rails to not clean the files in assets/spree/fonts?
I'm assuming you installed the bootstrap files manually?
If you instead use a gem such as the following, then you won't have to worry about these issues:
gem "bootstrap-sass"
Alternatively, you should be installing everything into your vendor directory. As you've found you'll then have issues with any linked assets within these files. The correct fix for this would be to edit the bootstrap source to use the correct asset_path helpers.
Obviously that's quite a bit of maintenance overhead when you get round to doing the next bootstrap update.
I'd take a look at the bootstrap-sass gem, even if you decide not to use it.

Where can i find the chosen.jquery.js file in my rails app?

I'm guessing this is a silly question, but i'm a bit of a newb...
I've added the chosen gem to my rails app by adding it to my Gemfile and requiring chosen-jquery in my application.js file.
My question is: where can i find the actual javascript file for chosen? Is it downloading it automatically?
Simply include the JS and CSS files like described in the documentation on the main page of the Github project and it will be included when precompiling your assets. The files are not within your project directory but rather within the gem and will be resolved by the pipeline when the gem is included.
If you need to get to the file directly (for modifications), you need to put the JS in there manually. You can then still include it in the main application.js for the pipeline and have better control of the version in use. To me, this is the preferred method.
However, may I suggest switching to Select2 which is originally based on Chosen but under much more active development and better documentation:
http://ivaynberg.github.io/select2/
https://github.com/ivaynberg/select2
There's also a gem for it if you like:
https://github.com/argerim/select2-rails
It's in a gem itself. If you look at vendor/assets/javascripts in gem root, you'll see all javascripts that come with chosen gem. They are added by assets pipeline, with
//= require chosen-jquery
line in your application.js.
Look up your assets load path. Like this:
in terminal cd to your app root
then open rails console
then run Rails.application.config.assets.paths
Your chosen-jquery is in one of those folders.

Integrating CKEditor with Rails 3.1 Asset Pipline

I'm new to the Asset Pipeline, having just migrated over from Rails 3.0. I'm trying to get CKEditor into the pipeline, but all the gems for it are really unclear about how they work, and have little or no usage instructions.
I would prefer to do this without using a gem, since it seems that all I have to do is drop the source files into the vendor/assets directory and then include them in application.js. I've tried that, however, when I precompile and push to production, it seems that some of the files aren't being found (editor.css, for example), and the editor doesn't show up at all (just blank area).
application.js
//= require jquery
//= require jquery_ujs
//= require ckeditor/ckeditor
//= require_self
That's with the source files in vendor/assets/javascript/ckeditor/, and is pointing to ckeditor.js. I'm just not sure where to go from here. This code works fine in development but does not work in production. I am running rake assets:precompile before adding and committing to git, and then pushing to heroku.
I got this working (deployed on Heroku), by:
Including the ckeditor code in vendor/assets/javascripts/ckeditor
Adding config.assets.precompile += ['ckeditor/*'] to my production.rb
Setup your ckeditor base path in the application.html.erb var CKEDITOR_BASEPATH = '/assets/ckeditor/'; before the include of the application.js
In application.js, include //= require ckeditor/ckeditor
Bite the bullet and use a gem. Two options here:
CKEditor Engine
https://github.com/galetahub/ckeditor.
This runs as an engine and includes its own mountable CKEditor in assets. It also exposes Ckeditor.assets which you can add to your assets path. This references all the images, plugins, language files and miscellaneous little bits of junk that CKEditor requires.
It has a shot at handling image uploads and it also integrates nicely with ActiveAdmin.
CKEditor Rails
https://github.com/tsechingho/ckeditor-rails
This does less, you include it in your asset pipeline and it does the rest for you. Nice and simple and sufficient for all basic use cases.
Upshot
I have used both of these on live projects and both do the job. Use the former if you plan on using ActiveAdmin and you want a smooth ride. Use the latter if you prefer minimal.
CKEditor is pretty ugly. Keep it at arms length, then when you need to upgrade you can swap it out for another.
If you are on Rails 3.1.0, you should upgrade to 3.1.1. In this version the precompile rake task compiles assets into both original and digested filenames. This is so third-party code that is not pipeline aware will still work.
You will need to add the ckeditor directory and all its child directories to the precompile array so that the precompile task knows to compile them.
config.assets.precompile += your_files
your_files can be an array of files, regexs or Procs - whatever is need to capture the names of the ckeditor files. I don't have ckeditor handy to work out what needs to go in precompile, so others might appreciate it if you post what you come up with!
One thing to watch is that if you have far-future headers set for the /assets directory on your webserver, you'll need to exclude the CKeditor directory. Because those files won't be fingerprinted, there may be issues when you update CKeditor with some clients not getting the updated code because they have a cached copy that marked to only expire some time in the future.
Have similar issue. For me it was fixed by overriding default precompile task (I used Rails 4 and CkEditor 4).
Add to application.rb config.assets.precompile += ['ckeditor/*']
In application.js //= require ckeditor/init
Create file lib/tasks/precompile_hook and paste text from this answer Precompile hook
Had the same issue, I've adjusted fallback in production for the assets which had not digest until it will be fixed:
config/environments/production.rb
config.assets.compile = true
what about ckeditor_assets directory in /public ? uploaded photos and attachments go to those directories, as defined by default in app/models/ckeditor/[attachment.rb,photo.rb]
ckeditor_assets is outside of assets and images/files are not accessible (url like http://yourdomain.com/ckeditor_assets/pictures/1/file.jpg will not work, but the file is there)
I spent some time getting the ckeditor_rails gem to work; maybe I can save some time for others trying to do the same.
The gem worked just fine out-of-the-box in development, but when deployed to production using precompiled assets under Phusion Passenger it did not. It was clear to me that the problem was that it was looking for assets under:
http://myhost.com/assets/ckeditor
where in fact it needed to be looking under:
http://myhost.com/my_app_name/assets/ckeditor
It was also clear to me that I needed somehow to set:
var CKEDITOR_BASEPATH = '/my_app_name/assets/ckeditor'
but no matter where or how I tried to do this, it wouldn't take.
Finally, I found on this key sentence on the gem wiki:
You can create app/assets/javascripts/ckeditor/basepath.js.erb to
have your own CKEDITOR_BASEPATH.
I created the file as specified (alongside my config.js file for configuring the editor), added my CKEDITOR_BASEPATH setting to the file, re-compiled my assets, and all was good.
In your config/development.rb make sure to set
config.assets.precompile += ['ckeditor/*']
as well as set
config.assets.debug = true

Rails 3.1 Assets are being fingerprinted in production but rendered HTML is not

Everything works great in Development. And app deploys as normal with Capistrano. Assets (javascript & css) appear to be fully pre-compiled and each, along with images, are given a "fingerprint". Problem is when using image_tag("image-name.png") in my view the html it creates in production doesn't include the 'fingerprint'.
The rendered HTML we get in production:
<img alt="Image-name" src="/assets/image-name.png" />
instead of, what I would expect, should be:
<img alt="Image-name" src="/assets/image-name-b89cae2830c2901f844c353b3f3942fe.png" />
So which of Rails 3.1's myriad config options did we botch?
Edit
The troublesome images appear to be those included in a 3rd-party Colorbox image viewing tool we use. Rails 3.1 is fingerprinting its assets (border.png, etc.) but, clearly, the source code for this javascript library doesn't use helpers like image_tag. So in production it is still looking for images named /assets/colorbox/border.png. Currently images are in /vendor/assets/images and work fine in Development. Is there a way to prevent just these images from being "fingerprinted"?
Well, here's how I hacked the 3rd-party files to make things work:
The offending images were in files like vendor/assets/stylesheets/colorbox.css. I first changed the extension of the files to .scss then I changed each url(colorbox/image.png) to image_url("color box/image.png") and now everything is peachy. Assets are served normally in development and fingerprinted in production.
Still like to see the "proper" way to add 3rd-party (vendor) javascript libraries & css to a Rails 3.1 app. Rails team must have anticipate a drop-in solution that doesn't require editing?!? So, please, feel free to offer up other solutions.
Aside: where I previously had manually configured my Capistrano recipe with:
run "cd #{release_path}; RAILS_ENV=production bundle exec rake assets:precompile"
…and its accompanying after deploy:update_code …. I have now removed those lines and instead added load 'deploy/assets to my Capfile. I don't think this makes any difference in the above problem but I wanted to document it anyway as adding your own recipe for pipeline precompiling is no longer necessary in Capistrano 2.8 as it was in the 3.1rc days.

Rails 3.0 - Turn Compass/SASS off in Production - Stylesheets 404

I am running on Compass on Rails 3.0 on Heroku and its pretty much working fine, but I occasionally have an issue where (some?) stylesheets aren't compiled as fast as the rest of the page so they aren't served (the .css links cause 404's and the page is then displayed unstyled).
Do you guys know of a way to make Compass compile all stylesheets on server start up (in prod) and then not touch them again? That way it'll basically precompile all the necessary stylesheets and there won't be an issue?
Or alternately, would it make more sense to call some script before heroku deploy that compiled all the stylesheets to public/stylesheets, and then turn off Compass in production altogether?
Thanks!
Based on the discussion here:
Using Compass on Heroku: /tmp for stylesheets remotely and locally
I decided that I would rather turn off stylesheet compilation on the server entirely as you suggest as your alternate approach.
I have the following in my app's config.ru which is intended to achieve just that.
if (ENV['RACK_ENV'] || 'development') != 'development'
require 'sass/plugin/rack'
use Sass::Plugin::Rack
Sass::Plugin.options[:never_update] = true
end
UPDATE: I replaced this approach with the simpler one of adding
Sass::Plugin.options[:never_update] = true
to the very bottom of my production.rb environment file which works a charm on Heroku. As described here:
http://ariejan.net/2010/09/28/precompile-sass-to-css-for-deployment-to-heroku

Resources