Using Heroku for static site: Assets won't show - ruby

I've just loaded up a static app to Heroku using this tutorial and everything works quite well, except my images aren't showing up. When the same site is hosted on my own server as a plain static site (not through Heroku), all of the assets load up without a problem.
Currently, I have a Gemfile, Gemfile.lock, app.rb, config.ru and public (static site directory) in my repository that I'm loading to Heroku through git push heroku master to push to Heroku.
My images are in public/img and even the assets directly referenced from html aren't showing up. When I use firebug lite in Chrome to check the asset directory, it seems as though the image files are there, but they don't seem to have the image data (from what I could tell).
I do not have any further ruby/rails files. Should I have a production.rb file somewhere? Am I missing out on something?
Currently, my setup on Heroku is the free package. Will I need to upgrade to a paid package to see my assets (I only have 2MB of assets)? I've tried creating an "assets" directory inside the "public" directory and placing the img directory in there, but still no luck.
Here is my config.ru
use Rack::Static,
:urls => ["/img", "/js", "/css"],
:root => "public"
run lambda { |env|
[
200,
{
'Content-Type' => 'text/html',
'Cache-Control' => 'public, max-age=86400'
},
File.open('public/index.html', File::RDONLY)
]
}

To diagnose issues like this where you believe the file contents on your dyno don't match the ones in your source, use heroku run bash to login into a remote, on-off dyno. This will drop you into a bash shell where you can explore the file system as seen by your dyno (although the dyno your shell is attached to is not the one actively serving your requests it will have the same filesystem contents).
$ heroku run bash
Running `bash` attached to terminal... up, run.4065
~ $ ls
pubic Gemfile Gemfile.lock app.rb config.ru
~ $ cd public/img
~/public/img $ ls -l
total 40
-rw------- 1 u36831 36831 2743 2013-02-15 18:54 facebook-1652d049.png
-rw------- 1 u36831 36831 2291 2013-02-15 18:54 feed-e8d78a2f.png
From here you should be able to see:
If the image files even exist on the dyno
If their contents are what you expect (do the file sizes match what you see in your local env?)

Related

Assets loading issue on Rails 5 app with Heroku

I am facing asset loading issue in Rails 5 application deployed on Heroku.
App Configuration is,
ruby => ‘2.3.1’
rails => '~> 5.0.1'
When image is stored on path,
app/assets/home/image1.jpg
I am accessing it in view as,
= image_tag('/assets/home/image1.jpg’)
which is working properly in Development ENV, but not in Production ENV.
As per Heroku log,
ActionController::RoutingError (No route matches [GET]
"/assets/home/image1.jpg")
If I am moving image directly to
app/assets/image1.jpg
then its working on Production ENV.
Please guide about it.
Thanks
It looks like you assets are not compile on heroku.
Follow below code:
config/environments/production.rb
config.assets.compile = true
then run commands:
RAILS_ENV=production rake assets:precompile
then push all compiled files with menifest file to heroku.

Heroku: Creating a simple, static one page website on Cedar

I'm attempting to upload a simple one page web page using Heroku.
I was previously using the Play! Framework but it seems overkill for a single page with some javascript.
+ project/
+ public/
+ css/
...
+ img/
...
+ js/
...
index.html
How do I upload a basic set of static files to Heroku? There seems to be no documentation on their website on how to do this.
It is not the purpose of Heroku to host static websites. However, you still can do it but you have to create either a Ruby on Rails, Play, etc. project, add the HTML in the folders.
Heroku doesn't really support static web pages, it supports Apps. However, a static web page can trivially be 'enhanced' to be a PHP application by adding a dummy index.php.
So if you want to host a file foo.html as a Heroku app Foo, then:
1. Create Foo on Heroku.
2. [Clone empty repository to local directory] git clone git#heroku.com:Foo.git -o heroku
3. touch index.php
4. [add foo.html]
5. git add .
6. git commit -m 'test'
7. git push heroku master
Heroku has a guide to doing this with Rack: https://devcenter.heroku.com/articles/static-sites-ruby
Basically, create a simple Rack app with a config.ru file:
use Rack::Static,
:urls => ["/images", "/js", "/css"],
:root => "public"
run lambda { |env|
[
200,
{
'Content-Type' => 'text/html',
'Cache-Control' => 'public, max-age=86400'
},
File.open('public/index.html', File::RDONLY)
]
}
a Gemfile:
source "https://rubygems.org"
gem 'rack'
and a public folder with an index.html file and folders for other assets, structured like this:
my_site/config.ru
my_site/Gemfile
my_site/public/index.html
my_site/public/css
my_site/public/images
my_site/public/js
And someone has made a site that will generate all the necessary files for you: http://herokustaticmagico.herokuapp.com/

no route matches for assets/images in Rails

Working on rails, images are not visible and giving error.
Started GET "/assets/home.png" for 127.0.0.1 at 2012-06-19 12:23:24 +0530
Served asset /home.png - 404 Not Found (24ms)
ActionController::RoutingError (No route matches [GET] "/assets/home.png"):
I have used command
rake assets:precompile
production.rb
config.assets.compress = true
config.assets.compile = false
application.rb
config.assets.enabled = true
config.assets.version = '1.0'
Thanks for any help!
Actually you cannot reference your image with /assets/home.png path.
It will work in development mode, but in production all of your assets have a fingerprint in their filename (read this http://guides.rubyonrails.org/asset_pipeline.html#what-is-fingerprinting-and-why-should-i-care-questionmark)
That's why, in assets-pipeline enabled applications you need to reference all of your assets using helper methods. Read this doc to learn about the different helpers available in Ruby, JS and Sass files: http://guides.rubyonrails.org/asset_pipeline.html#coding-links-to-assets
The lack of a fingerprint in the file request suggests that you are running this in development. I am also going to guess that this is an app upgraded from an older version of Rails.
Any images need to be in the folder /assets/images for the pipeline to work.
Also, you do not need to precompile when in development mode.
Delete the public/assets folder, delete the folder tmp/cache/assets, and restart your server.
If this images are in the correct location, it should work.

Asset Precompile on the development machine before capistrano deployment

I want the asset precompile to happen on my dev machine beofore the code is packed (tar ball'ed) by capistrano and have the precompiled assets already included in the final deployment package.
When I try the inbuilt capistrano recipe thats in
load 'deploy/assets' it runs
rake RAILS_GROUPS=assets assets:precompile on the server.
The reason I am looking for this because at the moment the precompile is taking too long on my EC2 micro-instance (and also at times just hangs),
It would great if asset compile could be done even before the deploy starts so that I can save the server from this heavy duty work load - until at least I have better configured servers available.
The workflow is still a little bumpy at the moment, but you may find some success using Guard-Rails-Assets. It's a little slow, especially if you are making a lot of asset changes, but it will compile assets when they are changed and you can just check them in to your repo to be deployed later.
I've just written a gem to solve this problem inside Rails, called turbo-sprockets-rails3. It speeds up your assets:precompile by only recompiling changed files, and only compiling once to generate all assets. It works out of the box for Capistrano, since your assets directory is shared between releases.
It would be really awesome if you could help me test out the turbo-sprockets-rails3 gem, and let me know if you have any problems.
Remove load 'deploy/assets' from Capfile or config/deploy.rb, and add the following lines to the config/deploy.rb:
set :assets_role, [ :web, :app ]
set :normalize_asset_timestamps, false
set :assets_tar_path, "#{release_name}-assets.tar.gz"
before "deploy:update" do
run_locally "rake assets:precompile"
run_locally "cd public; tar czf #{Dir.tmpdir}/#{assets_tar_path} assets"
end
before "deploy:finalize_update", :roles => assets_role, :except => { :no_release => true } do
upload "#{Dir.tmpdir}/#{assets_tar_path}", "#{shared_path}/#{assets_tar_path}"
run "cd #{shared_path}; /bin/tar xzf #{assets_tar_path}"
run "/bin/ln -s #{shared_path}/assets #{release_path}/public"
run "/bin/rm #{shared_path}/#{assets_tar_path}"
end
If you use turbo-sprockets-rails3, add this to the last block:
run "cd #{release_path}; #{rake} assets:clean_expired 2> /dev/null"

rails 3.1 and heroku : routes modifications and problem removing the public/index.html

Here is a problem while changing the target of the :root of my rails 3.1 project :
I updated my config/routes.rb to point the root to a custom page.
root :to => 'pages#home'
Then, I removed the existing public/index.html.
Everything is ok in local.
I ran git commit -am "message", the result was :
delete mode 100644 public/index.html
I also committed my new routes.rb and the needed controllers and views.
I pushed to heroku : git push heroku master
And then I ran heroku rake routes the result was :
pages_home GET /pages/home(.:format) {:controller=>"pages", :action=>"home"}
pages_formations GET /pages/formations(.:format) {:controller=>"pages", :action=>"formations"}
pages_music GET /pages/music(.:format) {:controller=>"pages", :action=>"music"}
root / {:controller=>"pages", :action=>"home"}
home /home(.:format) {:controller=>"pages", :action=>"home"}
formations /formations(.:format) {:controller=>"pages", :action=>"formations"}
music /music(.:format) {:controller=>"pages", :action=>"music"}
But if I go to my application page, I have an empty white screen.
If I reset the public/index.html, I have this one displayed.
So it seems like Heroku does not take my modifications into account. But the commit works : if I clone the project after deleting the file, I can see that it is deleted.
White page usually means you have some sort of error in production. The best way to handle this is to check your production log on heroku with:
heroku logs
And to address an actual problem.

Resources