Adding Postgres and ActiveRecord
Getting started - adding dependencies to your project
Guides, tutorials and labs to accompany CMU's Programming for Online Prototypes (49-714).
Everything you need to know about building microservices for the web with Ruby and Sinatra.
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
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
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
Remember to set up a Rakefile
set :database, "sqlite3:db/my_database<databasename>.db"
rake db:create
rake db:seed
rake db:migrate
rake db:rollback
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
Set up your project for Heroku as outlined in the guide
To deploy:
git push heroku master