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?)
In order for me to get this working on a new Sinatra app launched via config.ru, I had to do two of the things suggested in the other answers:
class MyApp < Sinatra::Base
set :static, true
set :root, File.dirname(__FILE__)
end
First create a folder named "public" in your sinatra project, then add a couple of folders
- stylesheets
- javascripts
- images
Add your CSS, JS and or JPG,PNG (images) to each folder
Finally as @sirfilip says add below lines to config.ru file
map "/public" do
run Rack::Directory.new("./public")
end
If generic Sinatra (no framework default)
views/layout.erb
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
...
<link rel="stylesheet" href="stylesheets/your_file.css">
<link rel="icon" type="image/ico" href="images/your_image.ico" />
</head>
<body>
<%= yield %>
...
<script src="javascripts/your_js.js"></script>
views/index.erb
<div class="margin-bottom-30">
<div class="row">
<div class="col-md-12">
<ul class="nav nav-pills">
<li class="active"><a href="#">Home <span class="badge">42</span></a></li>
<li>...</li>
</ul>
</div>
</div>
</div>
All of you images, stylesheets and javascripts will be available for any url registered in your Sinatra app , problem solved!