Cheat Sheet

Common Commands for Sinatra Development

Project Folder

Don’t forget that each project/Sinatra program should be contained in its own unique folder. Avoid spaces in the names tool At the very least, each project folder should contain one ruby file (the main code of the project) and that’s normally named app.rb

Working with Gems

It’s good practice to add your gems to a Gemfile. This makes managing them and deploying required gems to a server a breeze.

To add a GemFile create a file named Gemfile (no extension) in your folder. To add a gem jsut add a new line to it like this

require 'gem_name'

After you’ve made changes, update/install your gemset for the project using bundle

bundle install

Launching your Project Locally

Once you’ve made sure your Gems are up to date, and from a terminal window simply type:

ruby app.rb

This will load up a new webserver and your sinatra project at http://localhost:4567

If you get an error that says you can’t launch it for some reason, an old Sinatra project may not have properly loaded and might be still running on the default port. You can test this by opening a web browser and typing in the URL above. If you get a response, you’ve got an old version running. There’s two ways to solve this: 1) restart your computer or 2) start sinatra on another port like so: ruby app.rb -p 4568

Working with ActiveRecord

Remember to set up a Rakefile

Set your database in your app.rb

set :database, "sqlite3:db/my_database<databasename>.db"

Common Rake Commands

rake db:create 
rake db:seed              
rake db:migrate    
rake db:rollback 


Create a migration

rake db:create_migration NAME=a_descriptive_name_for_my_migration

    create_table :table_name do |t|
	t.boolean :column_name 	#a boolean (true or false)
	t.integer :column_name 	#a whole number
	t.string :column_name 	#a text string up to 256 characters long
	t.text :column_name 	# a long text string
	t.datetime :column_name # a date time object 
	t.decimal  :column_name # a decimal number 
	t.float  :column_name 	# a floating point decimal number 
    t.timestamps
	
   end   
   drop_table :table_name
   add_column :table_name, :column_name, :column_type, :options
   remove_column :table_name, :column_name
   

Deploying to Heroku

Set up your project for Heroku as outlined in the guide

To deploy:

  1. Commit any changes to your project’s git repo through GitHub desktop
  2. git push heroku master
×

Subscribe

The latest tutorials sent straight to your inbox.