Using Sinatra in Ruby

Learning to Use Sinatra

In the previous article in this series of articles, we talked about what Sinatra is. In this article, we'll look at some real functional Sinatra code, touching on a few Sinatra features, all of which will be explored in depth in upcoming articles in this series.

Before you get started, you'll have to go ahead and install Sinatra. Installing Sinatra is as easy as any other gem. Sinatra does have a few dependencies, but nothing major and you shouldn't have any problems installing it on any platform.

$ gem install sinatra

Hello, World!

The Sinatra "Hello world" application is shockingly simple. Not including the require lines, shebang and whitespace, it's just three lines. This is not just some small part of your application, like a controller in a Rails application, this is the entire thing. Another thing you may notice is that you didn't need to run anything like the Rails generator to generate an application. Just paste the following code into a new Ruby file and you're done.

#!/usr/bin/env ruby
require 'rubygems'
require 'sinatra'
get '/' do
'Hello, world!'

Of course this isn't a very useful program, it's just "Hello world," but even more useful applications in Sinatra aren't much larger. So, how do you run this tiny Web application? Some kind of complex script/server command? Nope, just run the file. It's just a Ruby program, run it!

inatra$ ./hello.rb
== Sinatra/0.9.4 has taken the stage on 4567 for development with backup from Mongrel

Not very exciting yet. It's started the server and bound to port 4567, so go ahead and point your Web browser to http://localhost:4567/. There's your "Hello world" message. Web applications have never been so easy in Ruby before.

Using Parameters

So let's look at something a little more interesting. Let's make an application that greets you by name. To do this, we'll need to use a parameter. Parameters in Sinatra are like everything else--simple and straightforward.

#!/usr/bin/env ruby
require 'rubygems'
require 'sinatra'
get '/hello/:name' do
"Hello #{params[:name]}!"

Once you've made this change, you'll need to restart the Sinatra application. Kill it with Ctrl-C and run it again. (There's a way around this, but we'll look at that in a future article.) Now, the parameters are straightforward. We've made an action called /hello/:name. This syntax is imitating what the URLs will look like, so go to http://localhost:4567/hello/Your Name to see it in action.

The /hello portion matches that portion of the URL from the reqest you made, and :name will absorb any other text you give it and put it in the params hash under the key :name. Parameters are just that easy. There is of course much more you can do with these, including regexp-based parameters, but this is all you'll need in almost every case.

Adding HTML

Finally, let's spiff this application up with a little bit of HTML. Sinatra will return whatever it gets from your URL handler to the web browser. So far, we've just been returning a string of text, but we can add some HTML in there with no problem. We'll use ERB here, just like is used in Rails. There are other (arguably better) options, but this is perhaps the most familiar, as it comes with Ruby, and will do fine here.

First, Sinatra will render a view called layout if one exists. This layout view should have a yield statement. This yield statement will capture the output of the specific view being rendered. This allows you to create layouts very simply. Finally, we have a hello view, which generates the actual hello message. This is the view that was rendered using the erb :hello method call. You'll notice that there are no seperate view files. There can be, but for such a small application, it's best to keep all the code in a single file. Though the views are sepeated at the end of the file.

#!/usr/bin/env ruby
require 'rubygems'
require 'sinatra'
get '/hello/:name' do
@name = params[:name]
erb :hello
@@ layout
<%= yield %>
@@ hello
<h3>Hello <%= @name %>!</h3>

And there you have it. We have a complete, functional hello world application in about 15 lines of code including the views. The following articles, we'll take a closer look at the routes, how you can store and retrieve data, and how to do better views with HAML.