My Sinatra web based application

Yisroel Malamud
2 min readSep 11, 2019

After working on my Sinatra web based application, i worked on a few parts

there was the relational aspects of it ie. a song belongs to a user and a user has many songs

# song.rb
class Song < ActiveRecord::Base
belongs_to :user
end
#user.rb
class User < ActiveRecord::Base
has_many :songs, dependent: :destroy
has_secure_password
validates :username, :email, presence: true
validates :username, :email, uniqueness: true
end

there was the session implementation aspect which saves that user session when it is created

#users_controller.rbclass UsersController < ApplicationController   get '/signup' do   
erb :'users/signup'
end

post '/signup' do @user = User.new(params)
if @user.save
session[:user_id] = @user.id
redirect '/songs'
else
redirect '/signup'
end
end

get '/login' do
flash[:login] = "Have An Account?"
erb :'users/login'
end

post '/login' do
#make sure email exists in db
@user = User.find_by(username:params[:username])
#check if password is valid
if @user && @user.authenticate(params[:password])
session[:user_id] = @user.id
flash[:login] = "Login Successful!"
redirect "/songs"
else
flash[:login] = "Please Enter A Valid Username/Password, Or Simply Create An Account"
redirect "/login"
end
end

the by-crypt end which allows a user to have a secure password and have a password digest that not even the developer can view

has_secure_password

the user validation with a email and username

     validates :username, :email, presence: true  
validates :username, :email, uniqueness: true

a user is created when signing up which is done through the form i also crates a user session which is then deleted upon logging out a user can login persisting that session

get '/signup' do   
erb :'users/signup'
end

post '/signup' do @user = User.new(params)
if @user.save
session[:user_id] = @user.id
redirect '/songs'
else
redirect '/signup'
end
end

get '/login' do
flash[:login] = "Have An Account?"
erb :'users/login'
end

post '/login' do
#make sure email exists in db
@user = User.find_by(username:params[:username])
#check if password is valid
if @user && @user.authenticate(params[:password])
session[:user_id] = @user.id
flash[:login] = "Login Successful!"
redirect "/songs"
else
flash[:login] = "Please Enter A Valid Username/Password, Or Simply Create An Account"
redirect "/login"
end
end

once logged in a user can create a song edit that song and delete that song

#creates a song
post '/songs' do
@song = Song.new(params)
@song.user_id = session[:user_id]

if @song.save
redirect to "/songs/#{@song.id}"
else
redirect '/songs'
end
end
#updates a song
patch '/songs/:id' do
@song = Song.find_by_id(params[:id])
@song.title = params[:title]
@song.artist = params[:artist]
@song.genre = params[:genre]
@song.save
redirect to "/songs/#{@song.id}"
end
#destroys a song
delete '/songs/:id' do
# binding.pry
@song = Song.find_by_id(params[:id])
@song.delete redirect to '/songs'
end

a user can edit themselves (email, username)

get '/users/:id/edit'
do
@user = User.find_by_id(params[:id])
if @user
erb :'users/edit'
else
redirect to "/users/:id"
end
end
get '/users/:id'
do
redirect to '/songs'
end
patch '/users/:id'
do
@user = User.find_by_id(params[:id])
if @user && @user.authenticate(params[:user][:password])
@user.username = params[:username]
@user.email = params[:email]
@user.save
redirect to "/users/#{@user.id}"
else
redirect to "/users/#{@user.id}/edit"
end

the only way to do this successfully would be to input their original password otherwise it won’t persist to the database

--

--