background preloader

Sintraintro

Facebook Twitter

Sinatra: Frequently Asked Questions. How do I make my Sinatra app reload on changes?

Sinatra: Frequently Asked Questions

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.

Using Sinatra Helpers to Clean Up Your Code

While I was building the site I started thinking about the best way to add JavaScript files to the pages. After playing with it for a while, I ended up using some custom helper methods to add JavaScript files to any page. It’s actually very easy to add javascript files to pages in Sinatra using the layout file. For example, if you want to include JQuery and a custom JavaScript file (called application.js) on all pages, then all you need to do is put the following lines of code in your layout file: (This assumes that the Javascript files are in the public folder). Putting this code in the layout file means that they will be included on every page. I can then add an extra line to my layout file that will include the relevant script tag if the @js variable has been set. This is a big improvement. Now all that’s needed in the layout file is this line: Much cleaner. Helpers — Documentation for sinatra/sinatra (master) Laser/sinatra-best-practices at part-one. Sinatra Best Practices: Part One.

Testing

Tutorial. Singing with Sinatra. Welcome to Track 1 of "Singing with Sinatra.

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.

Uno! Use Sinatra to Implement a REST API

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".

Create, read, update and 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[edit] 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[edit] 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[edit] Notes[edit] Singing with Sinatra - The Encore. Welcome back to Singing with Sinatra!

Singing with Sinatra - The Encore

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.