Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sinatra static assets are not found when using rackup

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
like image 644
picardo Avatar asked Feb 20 '11 05:02

picardo


3 Answers

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
like image 120
texel Avatar answered Oct 20 '22 18:10

texel


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'}
like image 31
sirfilip Avatar answered Oct 20 '22 17:10

sirfilip


Not positive, but you may need to set :root, Stick.root?

(Based on How to deploy a modular Sinatra app to Heroku?)

like image 28
wuputah Avatar answered Oct 20 '22 17:10

wuputah