Quoter with Sinatra

Nico Bazzoni
3 min readMar 7, 2021

Creating Quoter was an adventure. Exploring and executing the concepts of CRUD and utilizing RESTful routes was a deeply challenging experience for me. With Representation State Transfer we migrate data based on get requests. Variables that represent data in tables built in Active Record are transferred to “erb’s” which the user can then interact with based on how the interface is designed and the data is ultimately displayed to the user.

Quoter is centered around the quote generator.

@quote = Quote.all[rand(0..Quote.all.count)]

@quote.quote

redirect “/quotes/#{@quote.id}”

in the erb i have …

<%= @quote.source %>

<%= @quote.quote %>

<%= @quote.philosophy %>

which looks to the view like this…

Quoter’s quote generator is a font for awesome Philosophy quotes.

cool, so then i made a drop down button menu that lets the user, comment, go back home or “add quote” to the user’s list. I used this cool erb syntax to embed the quote.id which has all the Quotes attributes harnessed by the previous iteration.

heres the button…

<a href= “/quote_list/<%= @quote.id %>” method=”post”>Add To My List </a>

heres how it looks in the users quote list

Thought i was hot stuff with a fancy dropdown menu

so heres how it looks on the users quote list page…

and here’s how it looks under the hood in the quote controller.

get ‘/quote_list’ do

@user = User.find(session[:user_id])

@quote_list = @user.quotes

erb :”/quotes/quote_list”

end

post ‘/quote_list/:id’ do

end

My join tables allow the user to have his own quotes and the post ‘/quote_list/.id’ corresponds with the button i have on the quote generator page which i think is cool.

<a href= “/quote_list/<%= @quote.id %>” method=”post”>Add To My List </a>

heres the button on the page where a user can edit their username

<form action=”/users/<%=@user.username%>” method=”post”>

this ultimately gets sent to…

patch “/users/:username” do

if params[:updated_username] == current_user.username

redirect “/users/#{current_user.username}/edit”

elsif User.find_by(username: params[:updated_username])

redirect “/users/#{current_user.username}/edit”

else

user = User.find_by(username:params[:username])

user.update(username:params[:updated_username])

session[:username] = user.username

redirect “/users/#{user.username}”

end

end

This is where we find the user by the new username they’ve typed in and sent to our edit get. Then it updates the new params, links in with session then redirects user to the same page, where they can go home.

comments is cool because along with the quote generator it utilizes new pages each time through the quotes/:id route and uses that awesome ruby key in a form that sends the comment under the relevant quote.

I made hash of attributes…

post ‘/comments’ do

quoter = {

content: params[:content],

user_id: session[:user_id],

quote_id: params[:quote_id]

}

Comment.create(quoter)

redirect “/quotes/#{params[:quote_id]}”

end

here’s a shot of my users model. I have a post table for a future feature i didn’t have time for.

--

--