Tips For Starting a Sinatra App

create_table :users do |t|
t.string :username
t.string :password_digest
class User < ActiveRecord::Base
User.create(username: 'sample1', password: 'password1')#<User id: 8, username: "sample1", password_digest: "$2a$12$GP5mAsFKx5JBuvtd3gcjS.uaCeWZxfGdHE/a/VifQ1t...">
@user = user.find_by(username: 'sample1')@user.authenticate('password1')
#<User id: 8, username: "sample1" password_digest:"$2a$">...
# Require environment file here
class ApplicationController < Sinatra::Base
enable :sessions
before '/samples*' do # Before all routes that start with /samples
# Redirect somewhere, perhaps homepage, perhaps login
# For this example we will redirect to login
redirect '/login' unless logged_in?
helpers do
def current_user
@current_user ||= User.find_by_id(session[:user])

def logged_in?
current_user != nil




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Mysql table building best practice

How To Mint A Free Anonymouse Through Our Smart Contract

What Was Your Experience at a Coding Bootcamp Like?

Sustainable Native Firebase Analytics Event Logging from Mobile Web View

Programming: Things They Don’t Teach You At University — Part Three of Five

Hassle-free Payment Integration for Seamless Onboarding

Hassle-free Payment Integration for Seamless Onboarding

Supercharging your LabJack: Add Rules, Email Notifications and Dashboards in Minutes

Controlling Sidekiq Workers on AWS with Fargate and SSM Commands

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
Julian Rosenthal

Julian Rosenthal

More from Medium

Taking Time to Appreciate Netflix: Reflections on my First JavaScript Project

How to Build a Custom Date Component using Vanilla JavaScript

Explaining Basic of Web Streaming in Simple Term

Coding Space Invaders in JavaScript Complete Tutorial Every Step Explained with HTML5 Canvas