My rails stylesheets are not loading/working - css

It's been a while since I've developed in Rails and I'm having trouble getting any scss stylesheet to work on my freshly created rails app.
layouts/application.html.erb has the default <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %> at the top.
For testing purposes, I created a main.scss file in assets/stylesheets/ that looks like this:
* {
border: 1px solid black;
}
I thought the application.scss file is supposed to grab all the stylesheets in it's folder and child folders but it's not. (Oddly, the .js files load just fine.)
I've tried RAILS_ENV=production rake assets:precompile but it didn't do anything. Could someone explain what it even does?
I've tried adding *= require main and *= require main.scss to application.scss. I even changed the file ext to css for both files. The only way I've gotten any css to render is by directly adding the code to application.scss, which I don't want to do.
Please help me with this.
EDIT 1
I'm going to add some more info since I'm getting generic answers. I mentioned that it's a fresh rails app so the basic things are already pre-generated. This is how my application.scss looks:
/*
* 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 any plugin's vendor/assets/stylesheets directory 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 bottom of the
* compiled file so the styles you add here take precedence over styles defined in any styles
* defined in the other CSS/SCSS files in this directory. It is generally better to create a new
* file per style scope.
*
*= require_tree .
*= require main
*= require_self
*/
Still, nothing works

/*
*= require_tree .
*= require_self
*/
Add the above to your application.scss or application.css.scss file

Looks like the only way I can get it to work is by adding #import main; to application.scss. It seems like the styles end up being used on every page (is this the default in rails?).
This is not my ideal solution but it's the only thing I've been able to do to get any styles to work via requiring methods.

in your application.css, try adding
*= require main
*= require_self
hope this helps

I could not get the above answers to work but I think I found an easy work-around that makes Rails act the way expected.
In your HEAD section add
<%= stylesheet_link_tag params["controller"], media: 'screen' %>
And now css/SASS/Scss files in app/assets/stylesheets/ will load.
Sadly, you'll still need to precomile things. Grr.

I thought the application.scss file is supposed to grab all the
stylesheets in it's folder and child folders but it's not. (Oddly, the
.js files load just fine.)
By adding *= require_tree . to application.scss I think it should load all of the files recursively like you expect.

When Using Rails 4:
Make sure your Gemfile is using the sass-rails gem
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
You've created app/assets/stylesheets/main.scss, great!
Now update application.scss to include it:
#import "main";
You'll also need to include various gem stylesheets this way too:
#import "bootstrap";
You shouldn't have an application.css file - if you made one, remove it.
It's a bad practice to include stylesheets recursively, since order does matter, and conflicting styles will cascade, thus clobbering each other. I don't have an answer to your question on recursion, since it's not something I've done since upgrading to Rails 4

For me this was the solution
Is to link css file to your root
Also make sure your application.html.erb has this line:
<%= stylesheet_link_tag 'application', media: 'all' %>

In my case with Rails 6, the styles from app/javascript/stylesheets couldn't be loaded in production (Heroku). The styles were only work in development.
So I add this gem in Gemfile and install it.
gem 'jquery-rails'
Then my app can load the styles both in development and production environment.

Related

Rails How to include a scss file in a specific erb view? [duplicate]

This question already has answers here:
After gem update: test fail with "Asset was not declared to be precompiled in production"
(4 answers)
Closed 6 years ago.
I want to include the assets/stylesheets/work.scss file in view/work/index.html.erb
I've check this question
How to include a css or javascript in an erb that is outside the layout?
and add this in layout/application.html.erb
<head>
...
<%= yield(:header) if content_for? :header%>
</head>
Then, add this in index.html.erb
<% content_for :header do -%>
<%= stylesheet_link_tag 'work' %>
<% end -%>
However, it threw this error
I was doing this in development mode instead of production, why I need precompile?
Because you are including it independently, and not including it in your application.css. The default for the asset pipeline is to compile applicaiton.css/application.js and anything that is included in those files.
/*
* 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 any plugin's vendor/assets/stylesheets directory 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 bottom of the
* compiled file so the styles you add here take precedence over styles defined in any styles
* defined in the other CSS/SCSS files in this directory. It is generally better to create a new
* file per style scope.
*
*= require_tree .
*= require c3
*= require purecss
*= require_self
*= require pure_css_reset
*/
If you want it to be seperate and not compiled into the one monolithic file, being called from your stylesheet_link_tag, you have to manually add it to the precompile array.
http://guides.rubyonrails.org/asset_pipeline.html#precompiling-assets is a good reference.

Rails application.css.scss not aware of other scss files in use?

I'm following M Hartl's Rails Tutorial, and trying to add a bootswatch theme.
I have succeeded by using the boostrap-sass gem as defined in the tutorial, and twitter-bootswatch-rails gem from the net.
However, in Hartl's tutorial, all the CSS that we write in addition to default bootstrap is in a separate custom.css.scss file.
My application.css.scss file (Renamed from Rails default .css) contains
#import "bootstrap-sprockets";
// Import cerulean variables
#import "bootswatch/cerulean/variables";
// Then bootstrap itself
#import "bootstrap";
// And finally bootswatch style itself
#import "bootswatch/cerulean/bootswatch";
#import "custom";
Which works, however the custom.css.scss file has a reference to $gray-light, a variable set in bootstrap. The server will return an error at the variable reference in the css file, unless I add
#import "boostrap-sprockets";
#import "bootstrap";
to custom.css.
End result though, is I now have two gigantic CSS files being used, for what I would think is no reason.
I thought the idea of
#import "custom";
was to include my custom.css.scss file into the application.css.scss file so that it would all be in one place, and variables would work nicely.
The other method that works is to dump my entire custom.css.scss contents into application.css.scss but that defeats the point of having separate files.
Am I doing something wrong?
Edit: To add more fuel to the fire, I deleted the two lines from custom.css, and instead `#import bootswatch/cerulean/variables"; and it works. However, the resulting CSS that's on the website itself has NOTHING from that file.
This could well be wrong, but I post an answer to my own question as follows:
It appears that the sprockets lines //= require_self and //= require_tree, even when listed inside the comment section of the manifest as they are by default, are actually running.
This then causes each of the files "required" to be compiled separately. As a result, instead of getting a single application-FINGERPRINT.css file, I was getting an application, a custom, and a static_pages one. I assume this is the "require_tree" line.
After removing these lines, the #import "custom"; line works as I expected it to. The files are all combined into an application-FINGERPRINT.css file and I no longer need to #import anything at the top of custom.scss.

rails 4 and compass, how import only one time my sass files?

I have a rail 4 project with "stylesheets/application/index.css.scss" with my all css files:
/*
*= require jquery.ui.all
*= require_tree ../shared
*= require_tree ../design
*= require_tree ../layout
*= require_self
*= require_tree .
*/
rails compile all css in only one, minimized (in prod).
I need to import #import "shared/header" in many files.
exemple: in "stylesheets/layout/main.css.scss"
#import 'shared/header';
.header
{
#extend .header_common_overview;
[...]
}
but I #import 'shared/header' in others files too. The result is :
when rails compile in only one file, there are many times the same rules ".header_common_overview", because I import it in different files.
I tried to put the "import" instruction directly in index.css.scss, but it does't works.
So how can I import only one time a file, and be abble to call the content in all others files?
First, don't use require_tree . You lose control over the include order of your CSS files, potentially leading to cascading issues - styles being overwritten that really should not be.
I've learned to avoid sprockets' require lines in the main SASS files for reasons similar to what you describe.
It can lead to duplication, particularly when using =require_tree all over the place
Variables/mixins/etc... can't be included via sprockets (I'd love to be proven wrong about this though)
In your index.css.scss you might consider simply putting
#import "vendor";
#import "shared";
#import "design";
#import "layout";
// Your main styling here.
#import "another_file";
These #import lines correspond to other sass files. shared.css.scss for example might look like
/*
*=require ./shared/header
*/
The idea is to
Keep clean separation/organization of your asset includes
Explicitly define each asset include so you retain full control over include order
Use SASS #importinstead of Sprockets =require directive to keep variables, mixins, etc... present in an included file available throughout.
My solution is : create all.css.scss with :
/*
*= require jquery.ui.all => static, don't need import
*/
#import 'included/**/*'; //all files included (at first time)
#import 'all/**/*'; //all real css files which requires included file (in second times)
The order is respected and controlled.
The included files is present only one time
the included files are shared in each real css files.
thx for help.

Bootstrap not working after minor change to custom.css.scss

Similarly to this issue:
rails: any change to custom.css.scss make the app crash
I have made a change to my custom.css.scss file and all the bootstrap references to colors stopped working. I have manually entered the hex values for these colors now and the app loads, but the classes for my navbar are now not working.
It seems that I have a problem referencing any of the bootstrap classes now for some reason. I also do not understand why making a minor change to a stylesheet has sent my bootstrap styles into a meltdown. I think I must have a setup problem elsewhere.
custom.css.scss:
#import "bootstrap";
/* mixins, variables, etc. */
$grayMediumLight: #eaeaea;
$gray-darker: #222;
$gray-dark: #333;
$gray: #555;
$gray-light: #999;
$gray-lighter: #eee;
Relevant gems
gem 'rails', '4.0.2'
gem 'bootstrap-sass', '2.3.2.0'
gem 'sprockets', '2.11.0'
gem 'sass-rails', '4.0.1'
gem 'uglifier', '2.1.1'
gem 'coffee-rails', '4.0.1'
gem 'jquery-rails', '3.0.4'
gem 'turbolinks', '1.1.1'
In addition to this, I recently added the bootstrap.css and bootstrap.min.css file to vendor/assets/stylesheets and the same .js files to use a collapsing panel class.
Any help much appreciated.
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 bootstrap.min
*= require_self
*= require_tree .
*/
UPDATE:
So I removed and then re-included the bootstrap.css and bootstrap.min.css files and the main navbar styles came back to life. The only things that are still not working are the references to colors and the styles for my drop-down list in the navbar.
removing bootstrap files from the vendor/assets/javascript and vendor/assets/stylesheets folders solved this issue for me too.

rails assets precompile css order

I'm using zurb with rails. I overrode some of zurbs default css by adding changes to app/assets/stylesheets/application.css
But precompilation seems to append all the other css files to this one. Is there any way to ensure that a certain css rule will be the last one to be precompiled to application.css?
the order in which the assets are compiled can be specified in the manifest files for js and css http://guides.rubyonrails.org/asset_pipeline.html#manifest-files-and-directives
Use many requires in your application.css.scss
*= require css1
*= require css2
...

Resources