Skip to content

Finish Lab #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ git_source(:github) do |repo_name|
"https://github.com/#{repo_name}.git"
end

gem 'bourbon'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.1.6'
# Use sqlite3 as the database for Active Record
Expand Down
10 changes: 4 additions & 6 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ GEM
base64 (0.2.0)
bcrypt (3.1.20)
bindex (0.8.1)
bourbon (7.3.0)
thor (~> 1.0)
builder (3.3.0)
byebug (11.1.3)
coffee-rails (4.2.2)
Expand All @@ -57,7 +59,6 @@ GEM
erubi (1.13.0)
execjs (2.9.1)
ffi (1.17.0)
ffi (1.17.0-arm64-darwin)
globalid (1.1.0)
activesupport (>= 5.0)
i18n (1.14.5)
Expand Down Expand Up @@ -97,8 +98,6 @@ GEM
nokogiri (1.15.6)
mini_portile2 (~> 2.8.2)
racc (~> 1.4)
nokogiri (1.15.6-arm64-darwin)
racc (~> 1.4)
puma (6.4.2)
nio4r (~> 2.0)
racc (1.8.1)
Expand Down Expand Up @@ -159,7 +158,6 @@ GEM
sprockets (>= 3.0.0)
sqlite3 (1.5.0)
mini_portile2 (~> 2.8.0)
sqlite3 (1.5.0-arm64-darwin)
thor (1.3.1)
thread_safe (0.3.6)
tilt (2.4.0)
Expand All @@ -181,11 +179,11 @@ GEM
websocket-extensions (0.1.5)

PLATFORMS
arm64-darwin-23
ruby

DEPENDENCIES
bcrypt (~> 3.1.7)
bourbon
byebug
coffee-rails (~> 4.2)
jbuilder (~> 2.5)
Expand All @@ -203,4 +201,4 @@ DEPENDENCIES
web-console (>= 3.3.0)

BUNDLED WITH
2.4.22
2.1.4
31 changes: 31 additions & 0 deletions app/controllers/pizzas_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class PizzasController < ApplicationController
def new
puts params
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 weeks of asking you to stop committing this!!!!! If I see this on the actual code challenge, I'm going to fail you immediately.

if params[:pizzeria_id]
@pizzeria = Pizzeria.find(params[:pizzeria_id])
@pizza = Pizza.new
else
@pizza = Pizza.new
end
end

def create
@pizza = Pizza.new(pizza_params)

if @pizza.save
redirect_to pizza_path @pizza, notice: 'Pizza was successfully created.'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're not handling the notice, don't include it in the redirect. As of now, it does nothing.

else
render :new
end
end

def show
@pizza = Pizza.find(params[:id])
end

private

def pizza_params
params.require(:pizza).permit(:pizzeria_id, :name, :description)
end
end
6 changes: 6 additions & 0 deletions app/models/pizza.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Pizza < ApplicationRecord
belongs_to :pizzeria
validates :name, presence: true
validates :description, presence: true
validates :pizzeria_id, presence: true
Comment on lines +2 to +5
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
belongs_to :pizzeria
validates :name, presence: true
validates :description, presence: true
validates :pizzeria_id, presence: true
belongs_to :pizzeria
validates :name, :description, :pizzeria_id, presence: true

end
4 changes: 3 additions & 1 deletion app/models/pizzeria.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
class Pizzeria < ApplicationRecord

has_many :pizzas
validates :name, presence: true, uniqueness: true
validates :address, presence: true, uniqueness: true
Comment on lines +2 to +4
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
has_many :pizzas
validates :name, presence: true, uniqueness: true
validates :address, presence: true, uniqueness: true
has_many :pizzas
validates :name, presence: true, uniqueness: true
validates :address, presence: true, uniqueness: true

end
23 changes: 23 additions & 0 deletions app/views/pizzas/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<% if @pizza.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@pizza.errors.count, "error") %> prohibited this pizza from being saved:</h2>
<ul>
<% @pizza.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

<%= form_with model: [@pizzeria, @pizza], local: true do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<% if @pizzeria.present? %>
<%= f.hidden_field :pizzeria_id, value: @pizzeria.id %>
<% end %>
Comment on lines +15 to +17
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The conditional wouldn't be needed if you set @pizzeria in the create action.


<%= f.label :description %>
<%= f.text_field :description %>

<%= f.submit %>
<% end %>
2 changes: 2 additions & 0 deletions app/views/pizzas/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1><%= @pizza.name %> from <%= link_to @pizza.pizzeria.name, pizzeria_path(@pizza.pizzeria) %></h1>
<p><%= @pizza.description %></p>
11 changes: 11 additions & 0 deletions app/views/pizzerias/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
<h1 style='margin-top:100px'>Pizzeria name: <%= @pizzeria.name %></h1>
<p>Address: <%= @pizzeria.address %></p>
<%= link_to "Tell us about a great pizza you had at #{@pizzeria.name}", new_pizzeria_pizza_path(@pizzeria.id) %>

<h6>Pizzas:</h6>
<ul>
<% @pizzeria.pizzas.each do |pizza| %>
<li>
<h4><%= pizza.name %></h4>
<p><%= pizza.description %></p>
</li>
<% end %>
</ul>
9 changes: 5 additions & 4 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Rails.application.routes.draw do
get '/pizzerias/new', to: 'pizzerias#new', as: 'new_pizzeria'
post '/pizzerias', to: 'pizzerias#create', as: 'pizzerias'
get '/pizzerias/:id', to: 'pizzerias#show', as: 'pizzeria'
get '/pizzerias', to: 'pizzerias#index'
resources :pizzerias, only: [:new, :create, :show, :index] do
resources :pizzas, only: [:new, :create, :show]
end

resources :pizzas, only: [:new, :create, :show]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't need this line.

Suggested change
resources :pizzas, only: [:new, :create, :show]

end
10 changes: 10 additions & 0 deletions db/migrate/20240821132910_create_pizzas.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreatePizzas < ActiveRecord::Migration[5.1]
def change
create_table :pizzas do |t|
t.string :name
t.string :description
t.references :pizzeria, foreign_key: true
t.timestamps
end
end
end
11 changes: 10 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20170814143728) do
ActiveRecord::Schema.define(version: 20240821132910) do

create_table "pizzas", force: :cascade do |t|
t.string "name"
t.string "description"
t.integer "pizzeria_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["pizzeria_id"], name: "index_pizzas_on_pizzeria_id"
end

create_table "pizzerias", force: :cascade do |t|
t.string "name"
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/pizzas.yml
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't commit files/changes that don't involve your changes.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

# This model initially had no columns defined. If you add columns to the
# model remove the '{}' from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one: {}
# column: value
#
two: {}
# column: value
7 changes: 7 additions & 0 deletions test/models/pizza_test.rb
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class PizzaTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end