Sinatra: Frequently Asked Questions. How do I make my Sinatra app reload on changes?
First off, in-process code reloading in Ruby is hard and having a solution that works for every scenario is technically impossible. Which is why we recommend you to do out-of-process reloading. First you need to install rerun if you haven’t already: $ gem install rerun Now if you start your Sinatra app like this: $ ruby app.rb All you have to do for reloading is instead do this: $ rerun 'ruby app.rb' If you are for instance using rackup, instead do the following: $ rerun 'rackup' You get the idea.
If you still want in-process reloading, check out Sinatra::Reloader. What are my deployment options? See the book. How do I use sessions? Using Sinatra Helpers to Clean Up Your Code. Image courtesy of Shutterstock A couple of months ago, I wrote an article about how I rapidly built my personal site using Sinatra.
Tutorial. Singing with Sinatra. Welcome to Track 1 of "Singing with Sinatra.
" In this mini-series we'll be taking a look at Sinatra; a small, yet incredibly powerful DSL for quickly creating Ruby web applications. In this part, we'll get started with Sinatra by playing around with a few routes, learning how to access URL parameters and how to POST data between pages. If you haven't worked with Ruby before, you should check out the Ruby for Newbies session, where Andrew will guide you through the process of installing Ruby on your system and learning the basics of the language.
First thing we need to do is install the Sinatra RubyGem. Enter the following into the Terminal: Also install the 'shotgun' gem, which we'll use later: Depending on how you have RubyGems set up on your system, you may need to prefix the gem install commands with sudo. The Very Basics Open your text editor and create a new file named basics.rb. Let's start off by creating the classic "Hello World". Uno! Use Sinatra to Implement a REST API. The REpresentational State Transfer (REST) architecture provides a very convenient mechanism to shuttle data between clients and servers.
Web services and protocols, like HTTP, have been using the REST architecture for many years, so it is a well-tested and mature concept. Sinatra can be used to effectively implement the REST architecture. Sinatra’s DSL (Domain Specific Language) seems custom-tuned to the spirit of REST. In fact, Sinatra’s routing language includes the same verbs used by HTTP, including GET, POST, PUT and DELETE.
Because these REST API transactions look and behave very much like HTTP, network devices will treat your REST transactions in virtually the same way. Sinatra for REST services. Create, read, update and delete. Another variation of CRUD is BREAD, an acronym for "Browse, Read, Edit, Add, Delete".
DRULAB is also a variation, where "L" stands for Locking the access to the data (Delete, Read, Update, Lock, Add, Browse). This concept is mostly used in context with data protection concepts. Database applications The acronym CRUD refers to all of the major functions that are implemented in relational database applications. Each letter in the acronym can map to a standard SQL statement, HTTP method or DDS operation: Although a relational database provides a common persistence layer in software applications, numerous other persistence layers exist.
User interface Create or add new entriesRead, retrieve, search, or view existing entriesUpdate or edit existing entriesDelete/deactivate existing entries Without at least these four operations, the software cannot be considered complete. See also Notes Singing with Sinatra - The Encore. Welcome back to Singing with Sinatra!
In this third and final part we'll be extending the "Recall" app we built in the previous lesson. We're going to add an RSS feed to the app with the incredibly useful Builder gem, which makes creating XML files in Ruby a piece of cake. We'll learn just how easy Sinatra makes escaping HTML from user input to prevent XSS attacks, and we'll improve on some of the error handling code. Users Are Bad, m'kay The general rule when building web apps is to be paranoid. Currently our users are free to enter whatever HTML they like. To do this, add the following block of code to your recall.rb file, for example under the DataMapper.auto_upgrade! This includes a set of methods provided by Rack. To escape HTML on the home page, open the views/home.erb view file, and change the <%= note.content %> line (around line 11) to: Alternatively we could have written this as <%= h(note.content) %>, but the style above is much more common in the Ruby community.
Singing with Sinatra - The Recall App. Singing with Sinatra.