Welcome to Hack the North 2022 and to this workshop! We're Angela and Adison, your workshop leads. Ruby on Rails is a great tool to go with to kickstart your hackathon project and finish a working prototype within a short amount of time, so you have time for all the bells and whistles. In this workshop, you'll build your own web application from start to finish in only an hour with Rails!
a47616f0e20245230763f9dfd4c5616a
Before you start, please make sure that you've checked out the Hack Pack for this workshop. This includes instructions on how to install Ruby, SQLite, and Rails.
Additionally, please run bundle install
(mentioned early on in the slides).
[Work in Progress]
After cloning, please run bundle install as normal.
For every other command ran in the CLI for the remainder of the workshop, please insert "bundle exec" in front.
Navigate to the migration file created under db/migrate db/migrate
:
class CreateProductionCompanies < ActiveRecord::Migration[7.0]
def change
create_table :production_companies do |t|
t.string :name # company.name
t.string :homepage # company.homepage
t.timestamps
end
end
end
Navigate to the migration file (for movies) created under db/migrate db/migrate
:
class CreateMovies < ActiveRecord::Migration[7.0]
def change
create_table :movies do |t|
t.belongs_to :production_company
t.string :title # movie.title
t.text :overview # movie.overview
t.string :poster # movie.poster
t.date :release_date # movie.release_date
t.float :ratings # movie.vote_average
t.float :review # YOUR REVIEW (1.0-5.0)
t.boolean :watched # true if seen
t.timestamps
end
end
end
Inside the model created at app/models/movie.rb
:
class Movie < ApplicationRecord
belongs_to :production_company
validates :review, numericality: { in: 1.0..10.0 }, :allow_nil => true
end
Navigate to app/controllers/movies_controller.rb
:
class MoviesController < ApplicationController
def index
@movies = Movie.all
end
def show
@movie = Movie.find(params[:id])
end
end
Still inside app/controllers/movies_controller.rb
:
def new
@movie = Movie.new
end
def create
@movie = Movie.new(movie_params)
if @movie.save
redirect_to @movie
else
render :new, status: :unprocessable_entity
end
end
Still inside app/controllers/movies_controller.rb
:
def edit
@movie = Movie.find(params[:id])
end
def update
@movie = Movie.find(params[:id])
if @movie.update(movie_params)
redirect_to @movie
else
render :edit, status: :unprocessable_entity
end
end
Inside app/controllers/movies_controller.rb
:
def destroy
@movie = Movie.find(params[:id])
@movie.destroy!
redirect_to movies_path
end
At the very end of the same file, paste this:
private
def movie_params
params.require(:movie).permit(:watched,:review)
end
Under config/routes.rb
, paste:
Rails.application.routes.draw do
resources :movies
root "movies#index"
end
The Movie Database Api Ruby Wrapper for TMDB
Thank you so much for checking us out at Hack The North 2022, or on the interwebs. We hope you had as much fun as we did making this workshop. We'd love to see your modifications!
With love, Angela and Adison