Best practices for debugging the Rails asset pipeline - ruby-on-rails

I'm working with a Rails 3 app, bootstrap-sass, and the asset pipeline, and I'm looking for some ideas on how to debug asset pipelines woes.
When working in development, and compiling on the fly (i.e. including from lots of compiled css files), my app looks exactly how I expect it to.
However when I use the asset pipeline to to compile assets into a single file, to test how it will behave in production, I think validation errors in the concatenated file application.css are causing browsers to stop evaluating the css correctly, and I'm seeing a number of display issues.
When I look at the generated css files in development, and pass them through a css3 validator, I can see that validation errors occur when working with the vendored css files (in most cases these are due to vendor specific hacks, setting off the validator) - the files I've generated are passing validation when I run them through the w3c validator, and I've listed the pass/fail results in the sprockets files below:
/*
* sprocket file in application.css
*
*= require_self
*= require vendor
*= require bootstrap-include # FAILS 474 ERRORS - presumably down to css hacks
*= require DT_bootstrap # FAILS TWO ERRORS - presumably down to css hacks again
*= require navbar # PASSES
*= require footer # PASSES
*= require main # PASSES
*= require home # PASSES
*= require widget # PASSES
*= require search # PASSES
*= require devise # PASSES
*= require doohickeys # PASSES
*= require datepicker # FAILS - again yet more cross browser css hacks suspected
*/
How should you start prepping files for production when concatenation breaks an app's visuals like this?
I'm asking on here to get an idea of how best to debug a compiled stylesheet like this - it sounds like something that has been solved before elegantly, but I'm not aware of a reliable, approach when dealing with a problem like this.
For example, I can see that some rules are not being evaluated by using web developer inspector tools in Firefox and Webkit, but it's not clear to me how to go beyond that, short of doing some laborious binary search on the compiled CSS.
Surely there are some more specialised tools available for situations like this already, like being able to compile only some files in application.css, and link to a precompiled bootstrap-sass file separately, or something similar?

You should not use Sprocket directives if you are using something like bootstrap-sass, you should use #import.
Rename your manifest file from application.css to application.css.scss, then #import away, example:
#import "frameworks";
#import "bootstrap-responsive";

Related

AngularJs - Ruby on Rails - Bootstrap - Not showing glyphicons on Heroku

After a while looking for similar issues I couldn't find a solution. Most of them is for "only Ruby and Heroku" type of error.
But here I'm using AngularJS to import all my css and js files (including Bootstrap). The thing is, I cannot visualize my glyphicons on Heroku when locally works like a charm.
I tried in production.rb
config.assets.compile = true
Also to precompile my assets and pushing:
$ rake assets:precompile RAILS_ENV=production
Didn't work.
What else can I do?
My application.css
*= require bootstrap/dist/css/bootstrap
*= require_tree .
*= require_self
*= require angular-material/angular-material
*= require font-awesome/css/font-awesome
*= require bootstrap-social/bootstrap-social
*= require bootstrap-datepicker3
Thank you.
I had similar issue with icons in the past days. Could you post 2 images, one visualizing you Chrome/Mozilla Developer Console focusing on the css and html of that icon not visualized. This should be done for production and development. Basically my problem was in the asset pipeline.
1. The asset-url method did not work ...
2. The icons were based on a separate CSS Stylesheet. This CSS stylesheet was imported with #import from a stylesheet in the asset pipeline. Anyway you will notice the difference. Post the image, it may help.
What I am trying to say is, that you should focus on the difference between the fingerprinted application-fingerprint.css file from production and the one in development. It may be that you are not loading the css in development from the application.css, but from other file, then that file is not available in production.
Additionally I do not use require_tree
You can read my post at the following link and there are many post about the asset pipeline on stackoverflow, but is better to just interact and comment me so that i can try to help you.
Javascript does not work from a bootstrap template in rails
Rails 5 problems with multiple manifest files

Why is require_tree before require_self in default application.css?

I noticed that after generating a new Rails 4.2 application the order for the requires in application.css has been changed.
*= require_tree .
*= require_self
Shouldn't this be the other way around? Even in the Guides it isn't this way...
The change was made in order to allow styles defined in application.css to override previously included styles.
See Rails issue #11639 and this commit which changed the order of the require instructions.
As described in the official documentation here, and also explained in this SO answer here:
This puts the CSS contained within the file (if any) at the precise location of the require_self call.
This is slightly different than what I wrote in the comments since, as indicated by the above quote, the JS or CSS in the manifest will be inserted at the location of require_self. This becomes important if you have later assets that depend on something you wrote in the manifest.
Of course if you're doing that though, it would probably be better to place that "inline" asset in a separate file anyways, just to keep the manifest clean.

Rails Active Admin css conflicting with Twitter Bootstrap css

I'm somewhat new to the Rails asset pipeline so I might be doing something wrong. I'm trying to use Active Admin for my backend and twitter bootstrap css for my front end application.
I added the bootstrap.css to /app/assets/stylesheets then also added:
//= require bootstrap
to application.css - then I did a precompile of the assets locally
It seems to work fine but some of the styling isn't coming through exactly and I think it's because active admin's css is overriding it.
My understanding is that the application compiles the css assets into the application css public asset and the application uses that file when running.
I need to somehow separate the two and make it use twitter bootstrap css as the main css on the front end and maybe tell it not to use active admin's css files on the front end.
What's the best way to do this?
I had the same problem, and was able to fix it by moving
app/assets/stylesheets/active_admin.css.scss
to
vendor/assets/stylesheets/active_admin.css.scss
The active admin assets should be in vendor/ as mentioned in the rails guide:
"Vendor/assets is for assets that are owned by outside entities, such as code for JavaScript plugins and CSS frameworks."
Have you watched the RailsCasts video on using ActiveAdmin? In the video, Ryan shows you how to prevent the ActiveAdmin CSS from stepping on your main app CSS.
http://railscasts.com/episodes/284-active-admin
Moving info from Video into answer
In the application.css you remove:
*= require_tree .
For rails 4, Jiten K suggests adding this to production.rb:
config.assets.precompile += ['active_admin.css']
However one of the comments on that SO answer says this is not needed. I have not needed it so far.
For me changing application.css to following solves the problem:
*= require bootstrap
*= require_tree .
*= stub "active_admin"

Developing rails engines and using the asset pipeline

So I've started building a gem, which is difficult, but it seems to be working out so far..
Except for one little thing, the assets. I've tried putting them in my lib/assets, app/assets and vendor/assets. But they don't seem to load.
I have a test-app which loads my gem to test it, but nothing seems to work. I have an engine in my gem which I'm sure is being loaded.
What more do I need to know for this to start working?
EDIT:
here is my engine (located in lib/baco/engine.rb):
require 'rails'
module Baco
class Engine < Rails::Engine
end
end
EDIT 2:
This is my css file (located in vendor/assets/stylesheets/application.css.scss):
/*
* This is a manifest file that'll automatically include all the stylesheets available in this directory
* and any sub-directories. You're free to add application-wide styles to this file and they'll appear at
* the top of the compiled file, but it's generally better to create a new file per style scope.
*= require_self
*= require baco
*/
input {
padding:10px;
}
Turns out I still need to require the the css from the app that is loading the gem..
Is this a normal situation? Because I haven't read about that anywhere..
Anyways, got it to work, thanks for looking into it, hope this topic can helps some others out..

Rails 3.2.0.rc1 assets not showing on Heroku

My application works locally, but the stylesheets are not working on Heroku. I'm having difficulty understanding why.
I'm using Heroku's Cedar stack.
One of my ideas, is that my assets are not precompiling successfully:
RAILS_ENV=production bundle exec rake assets:precompile
rake aborted!
Invalid CSS after "*/": expected "}", was ""
(in /path/to/app/assets/stylesheets/application.css)
Can anyone explain why precompile is complaining about the */ on the very last line of application.css? It's meant to be there. If anyone has a fix, that would be ideal as well.
application.css
/*
* This is a manifest file that'll be compiled into application.css, which will include all the files
* listed below.
*
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
* or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
*
* You're free to add application-wide styles to this file and they'll appear at the top of the
* compiled file, but it's generally better to create a new file per style scope.
*
*= require reset
*= require style
*= require projects
*= require_self
*/
Update
I attempted to add quotes in my application.css, and it doesn't make a difference. Something isn't setting right I guess. But I'm unable to precompile to send to heroku. When I load the site, there is rarely any formatting at all.
First up, how come you're using Rails 3.1rc5? - that came out almost 6 months ago, can you not up it to 3.1.3 which is the latest release? You could be fighting bugs (and certainly Rails security fixes) which have since been fixed and certainly using updated dependent gems.
Your syntax is exactly right for the manifest files (see http://guides.rubyonrails.org/asset_pipeline.html) - The problem may also lie in the files you are requiring, so the error may be being misreported. You could try a process of elimination by removing some or the require statements trying a precompile and seeing if it's successful or not, then readding them to find the troublesome file?
I'm pretty sure you need to quote your files:
*= require 'reset'
*= require 'style'
*= require 'projects'
*= require_self

Resources