unable to display images on heroku - ruby-on-rails

When i run the website locally, it works fine.However, after pushing to heroku, its displaying as a broken image
The pictures are in my /assets/images/ folder
images are called in my static page like this
<img src="assets/blank_avatar_male.jpg" alt="blank_male_avatar">
Ive tried changing the path to /assets/images/jpeg and it and repushed. No luck so far.
Any suggestions on how to fix this?

My guess is that you don't have config.serve_static_assets set to true in your production.rb file.
The simplest solution is to include the rails_12factor gem which will do this for you

try compiling your assets locally like this:
RAILS_ENV=production bundle exec rake assets:precompile
and then push to heroku

Related

Rails 6 deployed on Heroku won't update Stylesheets

I built a Rails 6 app and deployed it to Heroku. But any changes I make to the stylesheet are not reflected. All the Heroku documentation and SO questions/answers appear to no longer be relevant to the current Rails setup in this regard. I could precompile the assets before pushing to heroku but I'd prefer not to. And actually I did find a "solution" but it feels more like a hack than a real solution. If I open config/initializers/assets.rb and change the statement:
Rails.application.config.assets.version = '1.0'
to
Rails.application.config.assets.version = '1.1'
then it will update the assets. But that means if I am experimenting with the look of the site I would be changing the version all the time. I mean if that's they way it's supposed to work I'll live with it, but it doesn't seem right. Anyone know a way to get Heroku to just update it on every push?
You've to compile the assets from the heroku end(i.e. in your server on Heroku), so after pushing your code to Heroku, run:
$ heroku run rake assets:precompile
# If above doesn't work
$ heroku run RAILS_ENV=production rake assets:precompile
Also, it's a good practice to add public/assets(or whatever folder that precompiles to) folder to your .gitignore file, so incase if you precompile assets in your local environment this will not mess up with your production env when you push to Heroku. But everytime you change some CSS and push to heroku, you'll need to precomile assets on Heroku end from the above command.

Heroku not displaying background image/fonts

I'm running into a bit of an issue on one of my first rails projects.
Locally, in development, my website is displaying perfectly. Fonts are being loaded correctly and the background image is displaying on every page. On Heroku however, the fonts do not appear, nor does the background image.
I have tried running the following commands
RAILS_ENV=production rake assets:precompile
RAILS_ENV=production rake assets:clobber assets:precompile
RAILS_ENV=production rake assets:clean assets:precompile
heroku run rake assets:precompile
heroku run rake assets:clobber assets:precompile
heroku run rake assets:clean assets:precompile
None of these commands changed the way the app was displaying, I also made sure to clear cookies before each attempt and even view in incognito; everything to no avail.
I also attempted to change how the background image was being introduced into the project.
* I changed the custom css file to end with a css.scss
* I altered the css call from background: to background-image:
... This caused the background image to fail to load locally as well
*I altered the css call from url('../img/bg.jpg') no-repeat center center fixed; to
image-url('../img/bg.jpg') no-repeat center center fixed;
... Again all this did was cause the image to not be loaded locally
I tried doing a combination of those two, again, to no avail.
I have both my css files and my image files located in the appropriate folders inside of the app/assets directory. The files are actually showing up inside of the public/assets folder as well, but still, the website does not display the fonts nor the background image.
I am very confused, and frankly out of ideas. Is there any thing I missed? Can anyone offer me some more ideas?
In you gemfile add gem
rails_12factor
Try this ..........
By default Rails 4 will not serve your assets. To enable this functionality you need to go into config/application.rb and add this line:
config.serve_static_assets = true
Alternatively you can achieve the same result by including the rails_12factor gem in your Gemfile:
gem 'rails_12factor', group: :production
This gem will configure your application to serve static assets so that you do not need to do this manually in a config file.
Please have a look here https://github.com/heroku/rails_12factor#rails-4-serve-static-assets
Hope this will work for you.

Ruby on Rails: Ran rake assets:precompile and now both local and heroku deployment don't include bootstrap

I was having issues deploying my project to a heroku server (Precompile fail). So I found this response, https://stackoverflow.com/a/13713753/2989437, and followed up on the advice. I added one line to my application.rb file:
application.rb
module FirstEdc
class Application < Rails::Application
config.assets.initialize_on_precompile = false # I added this line
...
end
end
I then ran the precompile command, committed the changes, and managed to deploy successfully to heroku. However, now my bootstrap/css appears to have stopped functioning both on the heroku deployment, and my local deployment.
I learned that I was supposed to add another line to my deployment.rb file:
deployment.rb
FirstEdc::Application.configure do
...
# Allows for local precompilling --added by Ian
config.assets.prefix = '/dev-assets'
end
So I added this, recompiled and redeployed, but to no avail.
Finally, I ran a rake assets:clean in an attempt to at least get my local deployment back to normal, but it did not work.
Any advice would be greatly appreciated. I'm reading more into the asset pipeline now, but I feel like this could be a cache problem or something. I'll update as I figure out what's going on.
edit. Just to clarify, I've tried removing both additions, running a rake assets:clean and rake assets:clean:all, but neither fix my local deployment.
I don't use config.assets.prefix in any of my apps and they work just fine with Heroku, so not sure what that's doing.
Try removing that line, then running rake assets:clean. Now your local server should be using the files as you change them. When you want to push, run rake assets:precompile first, then push.
If you want to make changes locally after that, run rake assets:clean again to get rid of the precompiled files on your local machine.
If Heroku detects any files in public/assets it will not attempt to precompile your assets again. This is by design.
So, you need to make a decision to either always precompile your assets with rake assets:precompile, or remove any files in public/assets before pushing to Heroku.
(The recommended way is to allow Heroku to precompile them during push)

Heroku thinks I'm using bootstrap? Won't let me rake assets:precompile

I was originally using bootstrap for a bit of prototyping but then I removed it. Now, I'm trying to push to Heroku, but keep getting this error:
Running: rake assets:precompile
rake aborted!
File to import not found or unreadable: bootstrap.
Load path: Sass::Rails::Importer(/tmp/build_1xogapzflu9oq/app/assets/stylesheets/custom.css.scss)
(in /tmp/build_1xogapzflu9oq/app/assets/stylesheets/custom.css.scss)
Of course it's unreadable because I removed bootstrap. Why is it still looking for it? I deleted everything in tmp, custom.css.scss doesn't even exist in my stylesheets, and there's no mention of bootsrap anywhere in my app! I've also tried deleted and recreating Heroku app.
What's going on here that I'm missing? Thanks!
Edit: rake assets:clean did not do anything either before heroku push or after.
Check your app/assets/stylesheets/custom & app/assets/stylesheets/application files & remove bootstrap's //=require directive, if present.
Further, if problem still persists. do the following
$ git add .
$ git commit -am "bootstrap fix"
$ git push heroku master
Lastly, if you still get the error. Paste your app/assets/stylesheets/application file, so we can debug what's going on
If you have bootstrap gem etc, remove it from Gemfile, and put your bootstrap css and js files manually in proper assets folder.

Heroku Assets Precompile

Today I was working with an application I've had running on Heroku for a few months now and in an attempt to get something working I ran in my development environment:
rake assets:precompile
When I committed my changes and pushed to Heroku, I get 500 errors on my request:
ActionView::Template::Error (jquery.flexslider-min.js isn't precompiled):
I'm at a bit of a loss as to what to do, I've tried a few things:
Lazily compile in production (which I really don't want to do):
Bundler.require(:default, :assets, Rails.env)
Specifically list all of the files that need to be "precompiled" (also don't really want to do this, doesn't seem very efficient):
config.assets.precompile += ...
So far I've simply done a rollback to my last working version. I'm currently stuck unable to push new code. Will be setting up a staging environment (like I should've done long ago) but not sure what to do next or what might fix this issue? Why didn't this throw an error before?
UPDATE
rake assets:clean
Appears to have resolved the problem, although I don't understand why. Can someone share some insight into this?
If you running Rails 4.0 or above, rake assets:clean has been replaced with rake assets:clobber.
However, there are some current problems with clean and clobber with regards to permanently deleting assets. You can follow the issue here. https://github.com/heroku/heroku-buildpack-ruby/issues/123
I'm willing to bet a compiled version/filename inside of manifest.yml in the public/assets folder was out of date/wrong.
If you've made changes to the flexslider.js file, you'll need to recompile with rake assets:precompile and push the updated version to github. I believe you can set the version of the assets inside of the manifest.yml file.
Also, I believe you can run heroku run rake assets:clean or heroku run rake assets:precompile.
I don't think it would be a good idea to precompile assets inside of heroku because of versioning and name conflicts/not stored in github.
You can clean the assets in heroku and push the repo again, so you wouldn't need to precompile locally and push to github, unless there was indeed an issue in the local compilation.
I'd also take a few minutes to read http://guides.rubyonrails.org/asset_pipeline.html
Another possibility is your file name is having issues with sprockets. Why not use the development version of the flexslider.js, rename it to something slightly more convenient, and allow sprockets to do the minification.
Found the solution in the GitHub thread:
increment the config.assets.version variable in
${project-root}/config/application.rb
Assets were refreshed after I'd added config.assets.version = '1.1' at the end of my config file.

Resources