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
45
votes
5 answers

How to parse JSON request body in Sinatra just once and expose it to all routes?

I am writing an API and it receives a JSON payload as the request body. To get at it currently, I am doing something like this: post '/doSomething' do request.body.rewind request_payload = JSON.parse request.body.read #do something with…
lmirosevic
  • 15,787
  • 13
  • 70
  • 116
44
votes
5 answers

Encoding::UndefinedConversionError

I keep getting an Encoding::UndefinedConversionError - "\xC2" from ASCII-8BIT to UTF-8 every time I try to convert a hash into a JSON string. I tried with [.encode | .force_encoding](["UTF-8" | "ASCII-8BIT" ]), chaining .encode with .force_encoding,…
martriay
  • 5,632
  • 3
  • 29
  • 39
43
votes
3 answers

Passing parameters to erb view

I'm trying to pass parameters to an erb view using Ruby and Sinatra. For example, I can do: get '/hello/:name' do "Hello #{params[:name]}!" end How do I pass :name to the view? get '/hello/:name' do erb :hello end And how do I read the…
Fábio Perez
  • 23,850
  • 22
  • 76
  • 100
43
votes
3 answers

Cannot access local Sinatra server from another computer on same network

I have a simple Sinatra server that I run through textmate but I can't access from another computer on the same network. I'm running Ruby 1.9.3p327 and Sinatra 1.4.1 on a Mac OS 10.8.3. Firewall is disabled. I tested the same scenario on different…
Thiago Peres
  • 824
  • 1
  • 11
  • 16
40
votes
5 answers

What is a very simple authentication scheme for Sinatra/Rack

I am busy porting a very small web app from ASP.NET MVC 2 to Ruby/Sinatra. In the MVC app, FormsAuthentication.SetAuthCookie was being used to set a persistent cookie when the users login was validated correctly against the database. I was wondering…
AndrewVos
  • 1,297
  • 2
  • 14
  • 25
39
votes
3 answers

What is this file config.ru, and what is it for?

What is this file config.ru, and what is it for in Sinatra projects? In my lanyard of the project, such code is written: require './app' run Sinatra::Application
Stas Chehovskih
  • 515
  • 1
  • 4
  • 7
39
votes
6 answers

How to make Sinatra work over HTTPS/SSL?

As the title says, Google doesn't give anything useful concerning this. How do I set up and configure HTTPS/SSL for Sinatra apps? How do I create a HTTPS route? I have never used HTTPS for my apps before and have no experience tweaking…
apirogov
  • 1,296
  • 1
  • 12
  • 22
39
votes
2 answers

Get client's IP address in Sinatra?

This is a really simple question, but I cannot find any mention of this, anywhere.. How do I get the client's IP address from in Sinatra? get '/' do "Your IP address is #{....}" end
dbr
  • 165,801
  • 69
  • 278
  • 343
37
votes
5 answers

Any success with Sinatra working together with EventMachine WebSockets?

I have been using Sinatra for sometime now and I would like to add some realtime features to my web-app by pushing the data via websockets. I have successfully used the gem 'em-websocket' on its own, but have not been able to write one ruby file…
Poul
  • 3,426
  • 5
  • 37
  • 43
37
votes
5 answers

Sinatra + Bundler?

I'm wondering how one can use Bundler with Sinatra. The idea is to use the gems that Bundler downloads inside the .gems folder.
khelll
  • 23,590
  • 15
  • 91
  • 109
37
votes
4 answers

Ruby Sinatra Webservice running on localhost:4567 but not on IP

I have a ruby(using sinatra) webservice on windows 7 32 bit OS. Its running on port 4567. Its working fine when I use localhost:4567 but when I replace localhost with the local ip of my machine say, 192.168.103.99:4567 it doesn't work, and fails to…
foobar
  • 2,887
  • 2
  • 30
  • 55
36
votes
5 answers

Is there a .NET framework similar to Ruby's Sinatra?

Does anyone know if there is a .NET library/API similar to Ruby's Sinatra? Just wondering since with the new Routing API in ASP.NET MVC, WCF and .NET 3.5, it seems like a possibility. UPDATED: Best answer link is stale so check out Nancy @…
kenny
  • 21,522
  • 8
  • 49
  • 87
35
votes
6 answers

How do I get Sinatra to refrain from adding the X-Frame-Options header?

I am using Sinatra to return some IFRAME contents, and I'd like to allow cross-domain src. Unfortunately, Sinatra is automatically adding an X-Frame-Options header to my response. How do I turn that off?
Bruce
  • 8,202
  • 6
  • 37
  • 49
35
votes
4 answers

How to use Pry with Sinatra?

I am writing my first Sinatra application and would like to use Pry to inspect/debug some things going on in the application. I haven't used Pry before either, but I would like to try it out. How would I get started using Pry with my Sinatra…
Andrew
  • 227,796
  • 193
  • 515
  • 708
34
votes
3 answers

What's the best way to talk to a database while using Sinatra?

As I understand it, the Sinatra framework, unlike Rails, does not provide an ORM. In that case, how do you talk to a DB in a Sinatra app? Or is Sinatra only for apps that don't use a DB?
Chris Collins
  • 3,440
  • 5
  • 27
  • 24