Ruby Rack Heroku: Serving Static Files - ruby

I have followed the Heroku guide on deploying static files using Ruby Rack (https://devcenter.heroku.com/articles/static-sites-ruby), but I was unable to access any HTML file in \public apart from index.html. Namely, localhost:9292/test.html still maps to index.html. (All my style and js files serve correctly).
Below is my config.ru file. I know what's wrong, but not sure about a valid solution?
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) ] }

For your config.ru file, try:
use Rack::Static,
:urls => ["/images", "/js", "/css"],
:root => "public"
run Rack::File.new("public")
You could also replace the last line with run Rack::Directory.new("public") which would serve up all your files, but would give a file browser like interface if someone went to the url of a directory (like the root directory)

I have no Ruby experience and I found this boilerplate project helpful when trying to deploy a static website to Heroku:
heroku-static-site
Particularly, make sure you also have Gemfile and Gemfile.lock files besides the config.ru.
Author's project structure hosts everything in the root directory but you can easily move everything to public/ and correct the config.ru as #looby suggested.

Related

Rack/Sinatra Method Not Allowed

I am developing a simple web app using Sinatra and using rack as the middleware and hence have a config.ru.
To run the application I use shotgun config.ru.
I have no problem when the application does a GET request. But my app has a couple of POST requests, and when I submit a form via POST method, I get this strange error:
Method Not Allowed
Following is the content of my config.ru:
require "rack"
require 'rack/contrib/try_static'
require File.expand_path("app", File.dirname(__FILE__))
use Rack::TryStatic, :root => File.join(App::SETTINGS.source, App::SETTINGS.site.config['destination']), :urls => %w[/]
run App
Any idea what could resolve the issue?
Thank You
The following will not respond to posts:
get '/hi' do
"Hello World!"
end
It is quite possible that you will need to do something like this:
post '/hi' do
# do post stuff
end
I solved the issue.
It was a problem with rack.
I replaced
use Rack::TryStatic,
:root => File.join(App::SETTINGS.source, App::SETTINGS.site.config['destination']),
:urls => %w[/]
with:
use Rack::Static,
:urls => ["/#{App::SETTINGS.site.config['destination']}"],
:root => File.join(App::SETTINGS.source, App::SETTINGS.site.config['destination'])

Using Heroku for static site: Assets won't show

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?)

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/

reload rails.root/app/resources/* per request

I have a rails app with a folder rails.root/app/resources where I keep some library code.
This library code is used in a rack app that I mount in routes.rb in my Rails application
# rails.root/app/resources/file_resource.rb
# routes.rb
mount DAV4Rack::Handler.new(
:root => Rails.root.to_s,
:resource_class => FileResource
), :at => '/', :constraints => {:subdomain => "w"}
How can I make the FileResource reload on each request in development?
I tried autoload_paths, reload_plugin, none seem to work.
I think it has something to do with the code in routes.rb.
If I make a file rails.root/app/resources/my_helper.rb and use MyHelper.test() inside FileResource, the MyHelper gets reloaded.
No, I don't want to move this in 'lib' folder.
I am currently using the following hack, if anyone has a better solution please do share!
# config/environments/development.rb
root = config.root
config.to_prepare do
load "#{root}/app/resources/file_resource.rb"
end

Cannot load 'paperclip/storage/ftp' when using paperclipftp in Rails 3

I've just installed paperclip 2.3.3 and paperclipftp 0.1.0.
Paperclip was working fine, the attachments were saving and everything was great.
Enter paperclipftp.
I've included both gems in my Gemfile, installed it with bundle and made sure all dependencies were satisfied. I've also double checked that all my ftp info is correct and the server is working fine.
When I try to attach a file using ftp:
has_attached_file :photo,
:styles => {
:small => "204x159#",
:original => "460X370#"
},
:storage => :ftp,
:path => "/:attachment/:attachment/:id/:style/:filename",
:url => "http://kickassserver.com/_myfolder/:attachment/:attachment/:id/:style/:filename"
I get the following error:
Paperclip::StorageMethodNotFound in SetupsController#create
Cannot load 'paperclip/storage/ftp'
I'm thinking that paperclipftp isn't actually being loaded by my app. Is there a way I can check to see that it's actually being loaded, or has anyone else experienced this?
Thanks,
Matt
I have ruby 1.9.2p180 and the problem is that the timeout class being loaded.
Only add,
require 'timeout'
to the application.rb and this will fix your error.

Resources