require_relative 'config/environment'
require 'sinatra/activerecord/rake'
desc 'start console'
task :console do
ActiveRecord::Base.logger = Logger.new(STDOUT)
Pry.start
end
ActiveRecord Models
for every table, there should be a cooresponding model
mkdir app
mkdir app/models
lets say we have a table called notes...
touch app/models/note.rb
note.rb
class Note < ActiveRecord::Base
end
Setting up Rack API - config.ru
touch config.ru
config.ru
require_relative "./config/environment.rb"
# enables cross-origin resource sharing
# this allows any site (note origins '*') to make any request to our API
use Rack::Cors do
allow do
origins '*' # can also put like origins 'http://localhost:3001' and only allow that domain to send a request
resource '/*', headers: :any, methods: [:get, :post, :patch, :put, :delete]
end
end
# We'll create the Application class
run Application.new
Setting up Routes
touch app/application.rb
application.rb
class Application
def call(env)
response = Rack::Response.new
request = Rack::Request.new(env)
# handle all routes and send appropriate responses
end
end