Wednesday, March 11, 2020

Using Sinatra in Ruby - Introduction

Using Sinatra in Ruby - Introduction In the previous article in this series of articles, we talked about what Sinatra is. In this article, well 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, youll 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 shouldnt 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, its 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 didnt need to run anything like the Rails generator to generate an application. Just paste the following code into a new Ruby file and youre done. #!/usr/bin/env rubyrequire rubygemsrequire sinatraget / doHello, world!end Of course this isnt a very useful program, its just Hello world, but even more useful applications in Sinatra arent much larger. So, how do you run this tiny Web application? Some kind of complex script/server command? Nope, just run the file. Its 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. Its started the server and bound to port 4567, so go ahead and point your Web browser to http://localhost:4567/. Theres your Hello world message. Web applications have never been so easy in Ruby before. Using Parameters So lets look at something a little more interesting. Lets make an application that greets you by name. To do this, well need to use a parameter. Parameters in Sinatra are like everything elsesimple and straightforward. #!/usr/bin/env rubyrequire rubygemsrequire sinatraget /hello/:name doHello #{params[:name]}!end Once youve made this change, youll need to restart the Sinatra application. Kill it with Ctrl-C and run it again. (Theres a way around this, but well look at that in a future article.) Now, the parameters are straightforward. Weve 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 youll need in almost every case. Adding HTML Finally, lets 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, weve just been returning a string of text, but we can add some HTML in there with no problem. Well 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. Youll notice that there are no seperate view files. There can be, but for such a small application, its best to keep all the code in a single file. Though the views are sepeated at the end of the file. #!/usr/bin/env rubyrequire rubygemsrequire sinatraget /hello/:name doname params[:name]erb :helloend__END__ layouthtmlbody% yield %/body/html helloh3Hello % 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, well take a closer look at the routes, how you can store and retrieve data, and how to do better views with HAML.