Sinatra static assets are not found when using rackup
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
Serving static files with Sinatra
Without any additional configuration, Sinatra will serve assets in public
. For the empty route, you'll want to render the index document.
require 'rubygems'
require 'sinatra'
get '/' do
File.read(File.join('public', 'index.html'))
end
Routes should return a String
which become the HTTP response body. File.read
opens a file, reads the file, closes the file and returns a String
.
Accessing public folder in Sinatra Rackup
You should prefix it with stylesheets
: href='stylesheets/main.css'
. It won't find and resolve static files recursively in the public directory.
Also rel
attribute of you link tag should be stylesheet
not stylesheets
.
issues finding public folder when requiring sinatra/base
Requiring Sinatra::Base
does not set any of the default configuration settings that requiring Sinatra
does. You'll need to set :public_folder ...
to a suitable value yourself, e.g:
set :public_folder, 'public'
Can't launch simple Sinatra app using rackup and jRuby (no response from web server)
Well, this is hardly sufficient to explain what is going on, but I can make it work if in config.ru I replace
run Sinatra::Application
with
Sinatra::Application.run!
In fact, knowing that makes me even more confused. Some sort of bug in Rack?
Sinatra CSS/Images Issue
I don't think the location of the view template should make any difference but if you have relative URLs like css/style.css
then the browser will append this to the path part of the URL of the page that hosts it. So if the page URL is contact/new
the effective URL becomes contact/css/style.css
.
If you prefix the relative URL with a '/' like /css/style.css
it should be relative to the root path of the site which you are mapping to the public directory. So that might be the best way to go:
%link{ :href => '/css/style.css', :rel => 'stylesheet', :type => 'text/css' }
I haven't tried using the rackup file to configure static files. This is what works for me in the application itself:
set :static, true
set :root, File.dirname(__FILE__)
set :public, 'public'
But I think that Sinatra will serve static files from the public by default so I don't think the last line is required.
Sinatra - Render index.html on every request
Shotgun has its own static server that it will use (to avoid forking when not required). Sinatra will also serve static files if configured to do so, but using the modular style app disables this by default.
The fix is to enable the static server:
module SK
module Routes
class Base < Sinatra::Base
# add this line:
enable :static
include Models
get '/*' do
File.read 'public/index.html'
end
helpers Helpers::API
end
end
end
Now Sinatra will serve static files from the public dir before trying to match the route.
Related Topics
How to Trace and Check Dependencies in Bundled Ruby Gems
How to Configure Action Mailer (Should I Register Domain)
How to Validate a Specific Attribute on an Activerecord Without Instantiating an Object First
How to Convert Character Encoding with Ruby 1.9
How to Create a List of Months Between Two Dates in Rails
Ruby Does Not 'Ensure' When I 'Retry' in 'Rescue'
How to Loop Through a Daterange with Different Intervals
Webkit_Server Hangs Periodically When Run from Capybara in Ruby
What Does Class Classname < ::Otherclassname Do in Ruby
What's the Difference Between Bundle.Setup and Bundle.Require
How Does Ruby Serialization (Marshaling) Work
How to Convert a Net::Http Response to a Certain Encoding in Ruby 1.9.1
Activerecord 3.1.0 Multiple Databases
How to Generate Coordinates in Between Two Known Points
Rendering Haml Partials from Within Haml Outside of Rails
How to Highlight Fields on Rails Validation Errors
Advantages and Disadvantages of Ruby on Rails Polymorphic Relationships