Questions tagged [sinatra]

Sinatra is a Domain Specific Language (DSL) for quickly creating web applications in Ruby with minimal effort. It is an alternative to other Ruby web application frameworks such as Ruby on Rails, Nitro, Camping, and Rango.

Sinatra is a small and flexible designed and developed by Blake Mizerany (bmizerany) in California. The project was financed mainly by , and Engine Yard in the past and is now taken care of by .

It does not follow the typical model–view–controller pattern used in other frameworks; instead, Sinatra focuses on providing a small set of functionality to help you deal with common web tasks such as templates, routes, sessions, cookies and more.

Hello World with Sinatra:

require 'sinatra'

get '/hi' do
  "Hello World!"
end

Books:

Websites:

5369 questions
34
votes
3 answers

in Sinatra how do you make a "before" filter that matches all routes except some

I have a Ruby Sinatra app and I have some code which I need to execute on all routes except for a few exceptions. How do I do this? If I wanted to execute the code on selected routes (whitelist style) I'd do this: ['/join', "/join/*",…
lmirosevic
  • 15,787
  • 13
  • 70
  • 116
32
votes
2 answers

How to do a Post/Redirect/Get using Sinatra?

What's Sinatra's equivalent of Rails' redirect_to method? I need to follow a Post/Redirect/Get flow for a form submission whilst preserving the instance variables that are passed to my view. The instance variables are lost when using the redirect…
John Topley
  • 113,588
  • 46
  • 195
  • 237
31
votes
2 answers

Ruby / Sinatra - serving up css, javascript, or image files

What is the correct way to route your request through Sinatra so that it serves up the file with no processing? I'm looking for the most common way people do this in the Sinatra framework? I normally place all of my static content in a "content"…
BuddyJoe
  • 69,735
  • 114
  • 291
  • 466
31
votes
9 answers

How do you debug a Sinatra app like a Rails app?

In my main Sinatra controller, I want to debug the params hash after it is POSTed from a form. I have added: puts params.inspect and set :logging, :true The params.inspect works if everything goes well. But if an error happens before the…
nova
  • 355
  • 1
  • 3
  • 6
30
votes
2 answers

Including local (JS and CSS) files in local Sinatra Development

I've been trying out Sinatra on my local Windows machine. I want to include some local CSS and JS files. This is how the code looks in layout.erb
Prakhar
  • 3,486
  • 17
  • 44
  • 61
30
votes
5 answers

Contact form in ruby, sinatra, and haml

I'm new to all three, and I'm trying to write a simple contact form for a website. The code I have come up with is below, but I know there are some fundamental problems with it (due to my inexperience with sinatra). Any help at getting this…
dcb
  • 940
  • 1
  • 8
  • 12
29
votes
5 answers

In Sinatra(Ruby), how should I create global variables which are assigned values only once in the application lifetime?

In Sinatra, I'm unable to create global variables which are assigned values only once in the application lifetime. Am I missing something? My simplified code looks like this: require 'rubygems' if RUBY_VERSION < "1.9" require 'sinatra/base' class…
arrac
  • 597
  • 1
  • 5
  • 15
28
votes
4 answers

Verb-agnostic matching in Sinatra

We can write get '/foo' do ... end and post '/foo' do ... end which is fine. But can I combine multiple HTTP verbs in one route?
punund
  • 4,321
  • 3
  • 34
  • 45
28
votes
3 answers

How to render a plain HTML file with Sinatra?

I have a simple sinatra application. All I want to do is use it as a wrapper to serve a static HTML file at a specific route. My directory structure looks like this: /directory myhtmlfile.html app.rb My app.rb file looks like this: require…
Andrew
  • 227,796
  • 193
  • 515
  • 708
28
votes
10 answers

Sending/Parsing multiple JSON objects

I have a Sinatra server that is returning multiple JSON objects from the database in a streaming manner. The objects would look like: {"a": 1, "b": 2, "c": 3} {"a": 4, "b": 5, "c": 6} ... but this is invalid JSON. I can add a hack into Sinatra's…
Kenny Peng
  • 1,891
  • 4
  • 18
  • 26
28
votes
5 answers

Use Rack::CommonLogger in Sinatra

I have a small web-server that I wrote with Sinatra. I want to be able to log messages to a log file. I've read through http://www.sinatrarb.com/api/index.html and www.sinatrarb.com/intro.html, and I see that Rack has something called…
Lawrence I. Siden
  • 9,191
  • 10
  • 43
  • 56
28
votes
2 answers

Sinatra Sub-Directory Views

I want to be able to get Sinatra views from sub-directories of ./views (such as ./views/admin). I know you can set the views like so: set :views, Proc.new { File.join(root, "templates") } But how would I be able to set this for only part of the…
Ethan Turkeltaub
  • 2,931
  • 8
  • 30
  • 45
28
votes
4 answers

What do you use Sinatra for?

Im confused about Sinatra (the ruby framework). Is it a lightweight Rails replacement or you can have them running side by side? Can you do a web application (as in Rails)? For example a twitter clone?
Victor
  • 23,172
  • 30
  • 86
  • 125
27
votes
2 answers

Sinatra - how do I get the server's domain name

I'm trying to get the domain name in my Sinatra app but as a newbie I really am struggling to figure out how to do this, and I know it must be possible! Rack::Request#host_with_port looks promising, but I don't know how to get this from my app - how…
Louis Sayers
  • 2,162
  • 3
  • 30
  • 53
27
votes
2 answers

Python equivalent of Sinatra

I know that many people use Sinatra when programming webapps in Ruby because they don't want their framework to get in the way by forcing them to use MVC. Is there something similar in Python? I'm looking for a framework which provides the glue…
Kudu
  • 6,570
  • 8
  • 27
  • 27