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
.
serving static files on sinatra
You can check the settings
object.
irb(main):001:0> require "sinatra"
=> true
irb(main):002:0> settings.public_folder
=> "/usr/lib/ruby/2.5.0/irb/public"
This allows you to create a route which returns the path, something like this
require 'sinatra'
get '/' do
settings.public_folder
end
Without more information I would guess that your public folder points to a wrong directory inside your docker because the project :root
points to a different directory that what you expect.
Trouble serving static files in Sinatra
For one, I don't believe you need the 'public' in your layout file.
href="/css/main.css"
Secondly, I recommend using scss/sass for styling. You just keep the .scss file within your primary /views directory.
app.rb
- views
- index.haml/html
- style.scss
In your .rb app file:
get '/style.css' do
scss :style
end
Then, in your view:
href="/style.css"
Hope that helps!
Serving large static files with Sinatra
I think I can just use send_file
(see here) - but if anyone has any other suggestions I have open ears!
Serving static files in Sinatra... with beautiful routes?
Got it!
get '/path_to_file/:number/:file' do
File.read(File.join('path_to_file', "#{params[:number]}", "#{params[:file]}"))
end
get '/path_to_file/:number' do
File.read(File.join('path_to_file', "#{params[:number]}", "index.html"))
end
Order is important, since if these two methods are reversed, get '/path_to_file/:number'
becomes a superset of get '/path_to_file/:number/:file'
.
Serving static files from Sinatra with case-insensitive URLs
Here is an idea for your routing:
get '/' do
send_file(File.join(File.dirname(__FILE__), 'public', 'index.html'))
end
get '*' do
file_path = File.join(File.dirname(__FILE__), 'public', request.path.downcase)
File.exist?(file_path) ? send_file(file_path) : halt 404
end
not_found do
send_file(File.join(File.dirname(__FILE__), 'public', '404.html'))
end
This is untested, since I probably won't be able to reproduce your environment anyways. But I think that it would work for your purpose, assuming all your pages are lowercase (as you mentioned) and that you also have an actual 404.html
page.
Nginx only serving static files for a Sinatra application
So thanks to @tadman the configuration was ok, the problem was just missing config.ru file.
Static page routing in Sinatra (Ruby)
Probably a better answer will eventually come, until then this is my shot at it.
If this is not what you want:
get '/' do
redirect '/index.html'
end
You might do something like this:
get '/' do
File.new('public/index.html').readlines
end
I'd go with the first one though, Not sure why you want to avoid that redirect
Related Topics
Best Practices For Reusing Code Between Controllers in Ruby on Rails
Why Do I Get a "Permission Denied" Error While Installing a Gem
How to Use Rvm and Create Globally Available Gems
What Is the Use of Gemfile in Rails
How to Search a Folder and All of Its Subfolders For Files of a Certain Type
Automatic Counter in Ruby For Each
How to Save a Base64 String as an Image Using Ruby
How to Use Global Variables or Constant Values in Ruby
Getting Fields_For and Accepts_Nested_Attributes_For to Work With a Belongs_To Relationship
Rails 3 Disabling Session Cookies
Find Unused Code in a Rails App
How to Read a User Uploaded File, Without Saving It to the Database
Which Style of Ruby String Quoting Do You Favour
Rails Console: Reload! Not Reflecting Changes in Model Files? What Could Be Possible Reason
Invalid Active Developer Path on MAC Os X After Installing Ruby