How To Fix ActiveRecord::ConnectionTimeoutError with Sinatra
If you get this error message ActiveRecord::ConnectionTimeoutError could not obtain a database connection within 5.000 seconds (waited 5.001 seconds) Try closing the connection after each request using "after" in Sintara. # app.rb after { A...
Written by Sean Behan on 11/09/2013
Roll Your Own Session Based Flash Messaging w/ Sinatra
Session based flash messages are fairly common in web apps. They work well when you have a temporary status to report back to the user, such as successfully saving a form. If you use Rails it comes with the Framework. If you are using Sinatra there ar...
Written by Sean Behan on 11/01/2013
How To Write Your Own Time Comparison Function in Ruby Like time_ago_in_words Without Rails
# time_ago_in_words(Time.now, 1.day.ago) # => 1 day # time_ago_in_words(Time.now, 1.hour.ago) # => 1 hour def time_ago_in_words(t1, t2) s = t1.to_i - t2.to_i # distance between t1 and t2 in seconds resolution = if s > 29030400 # seco...
Written by Sean Behan on 10/17/2013
How to Boot Up Multiple Sinatra Applications at the Same Time with Foreman
Foreman is a process management gem for ruby applications. It is used in combination with a Procfile for configuration instructions. A typical Procfile looks something like this [name] : [script to execute] Example Rack application Procfile ...
Written by Sean Behan on 10/15/2013
How to Render Partials with an Underscore in Sinatra
Sinatra doesn't have a method for rendering partials. It defers to the template language of your choice. For instance, if you're using ERB, it's as simple as erb :'path/to/partial' Rails has a nice convention of using an underscore to mark file...
Written by Sean Behan on 10/13/2013
How To Enable IFrame Support on Heroku with Ruby on Rails and Sinatra
This actually has more to do with Rails and/or Sinatra than it does with Heroku. You have to enable IFrame support in your headers explicitly. With Rails you can add this to your config/application.rb file config.action_dispatch.default_headers = ...
Written by Sean Behan on 08/25/2013
Roll Your Own Full Page Caching in Sinatra
It's as simple as this. # app.rb get '/:slug' do @slug = File.basename("#{params[:slug]}") @cache = "public/cache/#{@slug}.html" if File.exists?(@cache) File.read(@cache) else @post = Post.find_by(page: params[:slug]) ...
Written by Sean Behan on 08/23/2013
JSON::GeneratorError: only generation of JSON objects or arrays allowed
If you run into this error message try switching to an earlier version of ExecJs. JSON::GeneratorError: only generation of JSON objects or arrays allowed # Gemfile gem 'execjs', '~> 1.4' gem 'coffee-script' gem 'sass' ...
Written by Sean Behan on 08/22/2013
Testing current_user and Sessions with Sinatra and Rack Test
Assume you have your standard Sinatra application. # app.rb require 'sinatra' configure do enable :sessions end get '/' do if session[:user_id] "OK" else raise "NOT OK!" end end To test this you need to make a re...
Written by Sean Behan on 08/22/2013
How To Use Rake to Run Your Test Suite with Sinatra
If you're using Sinatra and you want to use rake to run your test suite, you will need to create a Rakefile and put this in it. require 'rake/testtask' task :default do ENV['RACK_ENV'] = 'test' Rake::Task['test'].invoke end Rake::Test...
Written by Sean Behan on 08/22/2013
Install do_mysql Ruby Gem on Mac OS X
I ran into the same problem when installing mysql gem for Rails development. This fix worked for me http://seanbehan.com/programming/fixing-mysql-for-rails-2-2-development-on-mac-os-x/ The same thing works with the data objects gem. Just specify the path...
Written by Sean Behan on 06/17/2012
Manage Sinatra Server in Development Mode with Shotgun
Sinatra won't reload your files. So if you're developing your app and want to see any changes made in the browser, install the shotgun gem. gem install shotgun You can then use shotgun to run your server shotgun your_sinatra_ditty.rb Presto, your ditt...
Written by Sean Behan on 06/17/2012
Deploy Sintra App on Ubuntu Using Apache2 and Phusion Passenger Module
Check it out http://sinatra.seanbehan.com/ This assumes Apache2 and the Phusion Passenger module have already been installed. If not you can get up to speed w/ this resource http://seanbehan.com/ruby-on-rails/new-ubuntu-slice-apache-mysql-php-ruby-on-rail...
Written by Sean Behan on 06/17/2012