I have a simple Sinatra app that is configured using the modular style. When I start the app using rackup -p 4567
as recommended in the readme file, the static assets in the public folder are not served. But when I start it using shotgun ./config.ru -p 4567
then they are served. Why does this happen? Could this happen in production?
Here is my code:
# config.ru
require 'rubygems'
require 'bundler'
require 'sinatra'
require 'jammit'
Bundler.require
Jammit.package!
require File.expand_path('./stick.rb')
run Stick
and this is the app ruby file
require 'sinatra/base'
class Stick < Sinatra::Base
get '/' do
haml :index
end
end
Looks like there are two good answers to this one (neither of the existing ones worked for me).
First off, in your config.ru file, you can include the following:
# Replace the directory names to taste
use Rack::Static, :urls => ['/stylesheets', '/javascripts'], :root => 'public'
Alternatively, if you're running your app via rackup, the :static
option is set to false
by default. You can remedy this by the following incantation:
class MyApp < Sinatra::Base
set :static, true
# ...
end
I had the same problem and i solved like this. I have added this line in my config.ru .
map "/public" do
run Rack::Directory.new("./public")
end
And i use the static files in my views like this
%link{:type => 'text/css', :rel => 'stylesheet', :href => '/public/css/reset.css'}
%link{:type => 'text/css', :rel => 'stylesheet', :href => '/public/css/text.css'}
%link{:type => 'text/css', :rel => 'stylesheet', :href => '/public/css/960.css'}
%link{:type => 'text/css', :rel => 'stylesheet', :href => '/public/css/app.css'}
Not positive, but you may need to set :root, Stick.root
?
(Based on How to deploy a modular Sinatra app to Heroku?)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With