Rails 5—Assets not loading in production (url not being found) - asset-pipeline

On my local machine, I have a play button that shows this in the inspector:
background-image: url(/assets/play-icon-ee4bd1dd973b8a19ce1ff95cc5ee63e393ea65b86b28d29d0cf7ac8dec98951b.svg);
But in production it can't find the asset and the inspector shows this:
background-image: url(play-icon.svg);
Any ideas on what's going wrong?

Please try
background: image-url("play-icon.svg")
then run rake assets:precompile

Related

Rails: application.css not in asset pipeline

I know this is a old question but no answer is fixing my problem.
I'm new to Ruby on Rails and just created project with a PostgreSQL database just to be able to upload the project to Heroku.
When i start the rails server im getting the error "application.css is not present in the asset pipeline."
I'm using bootstrap 4 gem and this requires that you rename the applications.css to application.scss.
I dont know what is wrong.
I really tried every answer that is on stackoverflow without any success :(
Please help me, what am i doing wrong?
This is the error im getting:
Ok, first thing.
Do you have config.serve_static_files = true set in your production.rb file under config/environment folder.
Since we run behind NGINX, in our case it looks like config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
Second thing. Did you do rails assets:precompile before the uploading it to the server?
And the third thing is. Have you tried calling your file application.css.scss, and redoing the rails assets:precompile?
Last, and not least thing. How does your application.scss file look like?
Did you remove all those *= and used #import instead for Bootstrap
It is nicely described in the documentation:
Import Bootstrap styles in app/assets/stylesheets/application.scss:
// Custom bootstrap variables must be set or imported before
bootstrap. #import "bootstrap";
And then it says:
Make sure the file has .scss extension (or .sass for Sass syntax). If
you have just generated a new Rails app, it may come with a .css file
instead. If this file exists, it will be served instead of Sass, so
rename it:
$ mv app/assets/stylesheets/application.css
app/assets/stylesheets/application.scss Then, remove all the *=
require and *= require_tree statements from the Sass file. Instead,
use #import to import Sass files.
Do not use *= require in Sass or your other stylesheets will not be
able to access the Bootstrap mixins and variables.
Read more here
origin answer
in my case, I forgot to install yarn command in my server.
so, please install yarn before running rails server. otherwise assets:precompile will do nothing and give no warning.
update
also, make sure all of these things:
file exists: app/assets/stylesheets/application.css
its content looks like:
/* ...
*= require_self
*= require_tree .
*/
also check file app/assets/config/manifest.js,
//= link application.css
To give more clarity to mutantkeyboard's answer
I had this issue when deploying a Rails application as a docker image that would run in a docker container without a web server like Nginx.
Here's how I got it fixed:
This issue is primarily caused in production when you do not want to serve your static files from the /public folder using a web server like Nginx or Apache.
To serve files your static files from the /public folder without using a web server like Nginx or Apache, do the following:
Ensure you precompile your assets using:
bundle exec rails assets:precompile
OR
rails assets:precompile
This will compile your assets into the /public folder
Next, in your config/environments/production.rb file, add the following:
Instead of this:
config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
Use this:
config.public_file_server.enabled = true
This allows rails to serve the static files instead of a web server
Note: For improved performance it's best to serve the static files using a Web server like Apache or Nginx.
Just reinstall yarn et precompile our assets:
yarn's parkages don't push on github... to fix this, reinstall yarn:
yarn
and precompile assets:
bundle exec rails assets:precompile
Rails 7.0.4 ruby 3.1.2 I solved issue with yarn build when missing application.js but in case missing application.css just did yarn build:css

Laravel (5.5) - Third party JS libraries in production version

I'm using third party JS libraries in my Laravel 5.5 project (leaflet and leaflet-draw). I installed the libraries using npm:
npm install leaflet
npm install leaflet-draw
I added the folowing lines in resources/assets/js/app.js:
require('leaflet');
require('leaflet-draw');
and in resources/assets/sass/app.scss:
#import "~leaflet/dist/leaflet.css";
#import "~leaflet-draw/dist/leaflet.draw.css";
after that, I run:
npm run dev
Assets compile and my application works fine on my localhost (I run basic laravel server with php artisan serve).
I tried deploying this application to server. I deployed it to a subfolder, so the address is:
https://example.com/laravelapplication
I followed this instruction:
https://www.digitalocean.com/community/tutorials/how-to-deploy-a-laravel-application-with-nginx-on-ubuntu-16-04
All routes and urls work fine. I don't get any code errors. But, leaflet and leaflet draw libraries have trouble fetching their icons and fonts. I get these errors:
GET https://example.com/fonts/vendor/leaflet-draw/dist/images/spritesheet.svg?fd5728f... 404 (Not found)
GET https://example.com/images/vendor/leaflet/dist/images/layers.png?a613745... 404 (Not found)
I'm not sure, but it looks like the library is searching for fonts and icons in the wrong place. It should look in https://example.com/laravelapplication instead of https://example.com
Does anyone know why this is happening?
If you know the actual URL these images are located in once your app is deployed, then a workaround would be to simply hard-code these locations:
For Leaflet, you can use the L.Icon.Default's imagePath option: (in your JavaScript)
L.Icon.Default.prototype.options.imagePath =
"https://example.com/laravelapplication/images/vendor/leaflet/dist/images/";
For Leaflet.draw plugin, you have to override the CSS rules: (in your CSS, make sure it is evaluated after Leaflet.draw's CSS)
.leaflet-draw-toolbar a {
background-image: url('https://example.com/laravelapplication/fonts/vendor/leaflet-draw/dist/images/spritesheet.svg');
background-image: linear-gradient(transparent, transparent), url('https://example.com/laravelapplication/fonts/vendor/leaflet-draw/dist/images/spritesheet.svg');
}
.leaflet-retina .leaflet-draw-toolbar a {
background-image: url('https://example.com/laravelapplication/fonts/vendor/leaflet-draw/dist/images/spritesheet-2x.png');
background-image: linear-gradient(transparent, transparent), url('https://example.com/laravelapplication/fonts/vendor/leaflet-draw/dist/images/spritesheet.svg');
}

Impossible to see CKeditor in production

I have a rails 4 app that use CKeditor. In dev mode it works fine but in production ckeditor does not apear and there is a blank space at the location of the textarea.
According to the logs, the following file is missing :
ActionController::RoutingError (No route matches [GET] "/assets/ckeditor/styles.js")
And if i precompile the asset i can see the following file generated :
public/assets/ckeditor/styles-65fee53acf063b3d207bc00b4f7ce0d5.js
Here is the ckeditor line of my application.rb :
config.assets.precompile += Ckeditor.assets
It seems that's the file CKeditor want but it is not looking for the precompiled one. How can i fix that?
i have pre-compiled my assets and pushed my repo to production server and got similar bug. And ckeditor was not loading.
Following worked for me:
1) Under config > environment > productio.rb file there is a line
config.assets.compile = false
i changed false to true and pushed the code to production site. Stopped and started unicorn. That did the trick now ckeditor is loading.
hope that helps.
For Rails 5 you have use in Gemfile
gem 'ckeditor', github: 'galetahub/ckeditor'
look here:
https://github.com/galetahub/ckeditor/issues/719
Did you follow these steps in the Readme? https://github.com/galetahub/ckeditor#usage-with-rails-4-assets
Specifying the CKEditor BasePath in the <head> section of my application.html.erb file fixed my issue
<script type="text/javascript">
var CKEDITOR_BASEPATH = '/assets/ckeditor/';
</script>
I had the same problem, i had included config.assets.compile = true and didn´t work...
Finally i did rake
rake assets:precompile
from the console.
If you are uploading images directly to AWS S3 you need to comment out the carrierwave or paperclip initializer while precompiling.
Then pushed it to production and worked fine!

Heroku not loading Google fonts

I'm running a site on Heroku, and having some troubles getting a google font to load.
My typography.sass file contains this:
#import url(http://fonts.googleapis.com/css?family=Bitter)
h1
font-family: 'Bitter', Helvetica, serif
My production.rb file contains the lines:
config.serve_static_assets = true
config.assets.compile = true
config.assets.digest = true
My Gemfile includes:
gem 'rails_12factor', group: :production
I've gone through all the Heroku issues with the asset pipeline, and I've gotten all of my images and css files to load properly in production, but for whatever reason, the font only works in development.
I had the same problem and found this answer:
https://stackoverflow.com/a/18216759
It appears that Heroku wants https. So throwing in https solved the problem for me.
I used to have the same issue but deleting the assets folder located in the public folder solved the problem for me. This will let Heroku precompile the css files for you and not use the files generate by running rake assets:precompile locally.
This fellow seemed to have a similar problem to yourself:
http://robert-reiz.com/2012/11/16/google-fonts-on-heroku/
Try his method of fixing it and see if that works. Good luck!

CSS3 and Heroku

I am having a problem with CSS3 code and heroku. I am currently hosting my website temporary on heroku, and this piece of code doesn't not seem to work:
#my_footer
{
margin-top:170px;
background-color:rgba(17,142,36,0.3); /* added this because of the backgroud */
border-radius: 3px 3px 3px 3px;
}
especially this line of code: background-color:rgba(17,142,36,0.3); the strange part is that it works on my localhost, but not on heroku! and I am using the same browser!
Note: I have checked the code on GitHub and the code is EXACTLY as above! so when I push to heroku, and then check the source code from the browser, the code for "my_footer" is NOT the same as the one in GitHub!! It is like GitHub is not pushing my code to heroku!!
btw I have this in my config/environments/production.rb
...
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = true #changed to true!
...
otherwise heroku returns me an error, and does not display the page if it is 'false' I am using twitter bootstrap btw!
Thanks!
try to run rake assets:precompile on your locale machine.
After add the generated files to git
git add .
git commit -m "assets precompiled"
git push heroku master
assuming your remote branch is called 'heroku'.

Resources