Sinatra, an Oldie but Goodie

Sinatra is a free and open source software web application library and domain-specific language (DSL) written in Ruby. My introduction to it was my first opportunity to take a need within my immediate community and channel that into a web application. This blog post is a followup to the original that I wrote as a Phase II student of Flatiron School’s Self-Paced Program. I wanted to reflect and share my feelings about this DSL for “quickly creating web applications in Ruby with minimal effort.”

require 'sinatra'
get '/frank-says' do
'Put this in your pipe & smoke it!'
end

For my project, I chose to make a listings app for people who live in close knit community developments and looking to sell or give away random items. At the time, I ran into a few newbie hiccups:

I thought it would be fun to use the Corneal gem, but incompatibilities within my Gemfile presented some issues that were difficult to fix. I made the mistake of bundling the gem before updating ActiveRecord to the latest version, which broke my app. I researched online, found some good fixes, and I was able to get it working again. However, I could not db:migrate anything new. Possible solutions that I found online did not work for me here. So I had to revert to ActiveRecord 4.2 to get migrations running again.

The greater challenge of the Sinatra Model-View-Controller (MVC) was params-to-views. That is, following through every step and using binding.pry to ensure that params data was being carried with every line of code. “NoMethodError: undefined method for nil:NilClass…” can be a nuisance if you let it.

@item = current_user.items.build(params[:item]) may have worked in building an item, but params were not passed to the READ aspect of Create-Read-Update-Delete (CRUD). After revisiting the code and working through several approaches to handling this, I found a working solution with:

@item = user.items.build(name: params[“item”][“name”]

… breaking down each of the params as outlined in the documentation and lessons for Sinatra nested forms. My last hurdle was getting my edited, Updated Item to show, which was fixed by reordering my lines of code.

When I began this project, my Category model had a name and a category_id. For the sake of keeping things simple regarding passing params, category_id was moved to the Item model. This got me out of a stuck point and made Post Item work efficiently. The downside is that items were no longer showing up for the category items pages. I plan to revisit and expand on this later in time with a new git branch.

Sinatra is a domain-specific language written in Ruby

In summary, since I’ve moved on to Ruby on Rails for a number of later projects I have to marvel at the simplicity of the Sinatra microframework. My favorite aspect is the up front, clear establishment of route structure. I also hereby admit that I am the owner of a real fedora. Maybe even 2 of them. I can’t wear them again without thinking of the late entertainer Frank Sinatra and how a DSL was named in his honor. I tip my hat with a smile to Sinatra author Blake Mizerany and developers who contribute to its open source.

For more information about Sinatra, visit http://sinatrarb.com.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store