Step1: Deciding type of website
I was thinking either making dog website or cocktail website since I love both of them. I ended up deciding cocktail website.
TheCocktailDB API: https://www.thecocktaildb.com/api.php
Step2: Getting Data
I want to get all the cocktails so I used my base as List all cocktails by first letter (https://www.thecocktaildb.com/api/json/v1/1/search.php?f=a) and changed f= as all the alphabet characters. I used gem 'httparty'.
@base_endpoint= "https://www.thecocktaildb.com/api/json/v1/1/search.php?f="
def get_cocktails_data
char=[*('a'..'z')]
#delete u and x since api doesn't contain any cocktail.
char.delete('u')
char.delete('x')
char.each do |alphabet|
cocktail_url=@base_endpoint+alphabet
cocktails_array=HTTParty.get(cocktail_url)["drinks"]
self.create_cocktail_objects(cocktails_array)
end
end
Step3: Making database
Using rake and sqlite3, created tables(cocktails, users) and added data that I got from API.
rake db:create_migration NAME=create_cocktails
rake db:create_migration NAME=create_users
class CreateCocktails < ActiveRecord::Migration[5.2]
def change
create_table :cocktails do |t|
t.string :name
t.string :glass
t.string :ingredient
t.string :instruction
t.string :image
end
end
end
class CreateUsers < ActiveRecord::Migration[5.2]
def change
create_table :users do |t|
t.string :name
t.date :birthday
t.string :email
t.string :password_digest
t.timestamps
end
end
end
rake db:migrate
rake db:seed
Step4: Building MVC
Model
- cocktail.rb (belongs_to user)
- user.rb (has_many cocktails)
View
- layout.erb
- welcome.erb
- index.erb
- users: new.erb
- sessions: new.erb
- cocktails:
- edit.erb
- index.erb
- new.erb
- show.erb
Controller
- application_controller.rb
- cocktail_controller.rb
- sessions_controller.rb
- users_controller.rb
Step5: CRUD(Create, Read, Update, Delete)
Create
get '/cocktails/new' do
redirect_if_not_logged_in
erb :'cocktails/new'
end
post '/cocktails' do
cocktail = current_user.cocktails.build(params[:cocktail])
if cocktail.save
redirect "/cocktails/#{cocktail.id}"
else
flash[:error] = "#{cocktail.errors.full_messages.join(", ")}"
redirect "/cocktails/new"
end
end
Read
get '/cocktails' do
@cocktails = current_user.cocktails
erb :'cocktails/index'
end
get '/cocktails/:id' do
@cocktail=Cocktail.find(params[:id])
erb :'cocktails/show'
end
Update
get '/cocktails/:id/edit' do
erb :'cocktails/edit'
end
patch '/cocktails/:id' do
redirect_if_not_logged_in
redirect_if_not_authorized
if @cocktail.update(params["cocktail"])
redirect "/cocktails/#{@cocktail.id}"
else
redirect "/cocktails/#{@cocktail.id}/edit"
end
end
Delete
delete "/cocktails/:id" do
@cocktail.destroy
redirect "/cocktails"
end
Step6: Front-end (Making look nicer)
I used below links mostly to make my website look nicer and I got help from Laura Berge and Eriberto Guzman (thank you).
Bootstrap: https://getbootstrap.com/
W3schools: https://www.w3schools.com/
Future
I will make another join table called party which users can share their cocktail list to make a shopping list for the party.
Top comments (0)