Skip to content

Create and display pizzas #4

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 1 commit 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 @@ -31,6 +31,8 @@ gem 'jbuilder', '~> 2.5'
# Use ActiveModel has_secure_password
gem 'bcrypt', '~> 3.1.7'

gem 'bourbon'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

Expand Down
3 changes: 3 additions & 0 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 Down Expand Up @@ -186,6 +188,7 @@ PLATFORMS

DEPENDENCIES
bcrypt (~> 3.1.7)
bourbon
byebug
coffee-rails (~> 4.2)
jbuilder (~> 2.5)
Expand Down
16 changes: 10 additions & 6 deletions app/assets/stylesheets/application.scss
Copy link
Contributor

Choose a reason for hiding this comment

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

Please don't commit changes like this for the actual code challenge. Pay attention to what you're committing EVERY TIME.

Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,24 @@
@import "base/base";

.container {
width:1000px;
margin:auto;
margin-top:50px;
width: 1000px;
margin: auto;
margin-top: 50px;
}

.form-container {
width:500px;
width: 500px;
}

.pizza_logo {
width: 100px;
-webkit-animation: spin 5s infinite linear;
@-webkit-keyframes spin {
0% {-webkit-transform: rotate(0deg)}
100% {-webkit-transform: rotate(360deg)}
0% {
-webkit-transform: rotate(0deg);
}
100% {
-webkit-transform: rotate(360deg);
}
}
}
29 changes: 29 additions & 0 deletions app/controllers/pizzas_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class PizzasController < ApplicationController
def new
@pizzeria = Pizzeria.find(params[:pizzeria_id])
@pizza = Pizza.new
end

def create
@pizzeria = Pizzeria.find(params[:pizzeria_id])
@pizza = @pizzeria.pizzas.build(pizza_params)

if @pizza.save
redirect_to @pizzeria
render :new
end
end

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

def index
@pizzas = Pizza.all
end

private
def pizza_params
params.require(:pizza).permit(:name,:description)
end
end
5 changes: 5 additions & 0 deletions app/models/pizza.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Pizza < ApplicationRecord
belongs_to :pizzeria

validates :name, :description, 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, :address, presence:true
end
26 changes: 26 additions & 0 deletions app/views/pizzas/new.html.erb
Copy link
Contributor

Choose a reason for hiding this comment

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

Code looks good, spacing could use some work. Adding a line between divs might be nice.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<h1>Add a New Pizza to <%= @pizzeria.name %></h1>

<%= form_with model: [@pizzeria, @pizza], local: true do |f| %>

<% 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 %>
<div>
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div>
<%= f.label :description %>
<%= f.text_area :description %>
</div>
<div>
<%= f.submit 'Create Pizza' %>
</div>
<% end %>
4 changes: 4 additions & 0 deletions app/views/pizzas/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<h1><%="#{@pizza.name} from "%>
<%=link_to @pizza.pizzeria.name, pizzeria_path(@pizza.pizzeria)%>
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
<%=link_to @pizza.pizzeria.name, pizzeria_path(@pizza.pizzeria)%>
<%= link_to @pizza.pizzeria.name, pizzeria_path(@pizza.pizzeria) %>

</h1>
<p>Description: <%[email protected]%>
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
<p>Description: <%=@pizza.description%>
<p>Description: <%= @pizza.description %>

9 changes: 9 additions & 0 deletions app/views/pizzerias/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
<h1 style='margin-top:100px'>Pizzeria name: <%= @pizzeria.name %></h1>
<p>Address: <%= @pizzeria.address %></p>

<p><%= link_to "Tell us about a great pizza you had at #{@pizzeria.name}", new_pizzeria_pizza_path(@pizzeria)%></p>
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
<p><%= link_to "Tell us about a great pizza you had at #{@pizzeria.name}", new_pizzeria_pizza_path(@pizzeria)%></p>
<p><%= link_to "Tell us about a great pizza you had at #{@pizzeria.name}", new_pizzeria_pizza_path(@pizzeria) %></p>


<h2>Great eats from <%= @pizzeria.name %></h2>
<ol>
<% @pizzeria.pizzas.each do |pizza| %>
<li><%=link_to pizza.name, pizza_path(pizza) %></li>
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
<li><%=link_to pizza.name, pizza_path(pizza) %></li>
<li><%= link_to pizza.name, pizza_path(pizza) %></li>

<% end %>
</ol>
10 changes: 6 additions & 4 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
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 :pizzas, only: [:new, :create, :show]
resources :pizzerias

resources :pizzerias do
resources :pizzas, only: [:new, :create, :show]
end
Comment on lines +3 to +7
Copy link
Contributor

Choose a reason for hiding this comment

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

Whoops!

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

end
8 changes: 0 additions & 8 deletions db/migrate/20170808134858_create_locations.rb
Copy link
Contributor

Choose a reason for hiding this comment

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

You shouldn't be deleting migrations. Assume that the migrations that were given to you are already "in production" and can't be undone.

This file was deleted.

8 changes: 8 additions & 0 deletions db/migrate/20170808134858_create_pizzerias.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class CreatePizzerias < ActiveRecord::Migration[5.1]
def change
create_table :pizzerias do |t|
t.string :name
t.string :address
end
end
end
5 changes: 0 additions & 5 deletions db/migrate/20170814143728_rename_locaitons_to_pizzerias.rb

This file was deleted.

10 changes: 10 additions & 0 deletions db/migrate/20240821125409_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
end
end
end
9 changes: 8 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@
#
# It's strongly recommended that you check this file into your version control system.

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

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

create_table "pizzerias", force: :cascade do |t|
t.string "name"
Expand Down
54 changes: 32 additions & 22 deletions db/seeds.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rails db:seed command (or created alongside the database with db:setup).
#
# Examples:
#
# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
# Character.create(name: 'Luke', movie: movies.first)
#
# 1
# Clear existing data
Pizzeria.destroy_all

Pizzeria.create!([{
name: "Sottocasa NYC",
address: "298 Atlantic Ave, Brooklyn, NY 11201",
},
{
name: "PizzArte",
address: "69 W 55th St, New York, NY 10019",
},
{
name: "San Matteo NYC",
address: "1559 2nd Ave, New York, NY 10028"
}])
Pizza.destroy_all

# Create pizzerias
pizzerias = Pizzeria.create!([
{
name: "Sottocasa NYC",
address: "298 Atlantic Ave, Brooklyn, NY 11201",
},
{
name: "PizzArte",
address: "69 W 55th St, New York, NY 10019",
},
{
name: "San Matteo NYC",
address: "1559 2nd Ave, New York, NY 10028"
}
])

# Create pizzas and associate them with pizzerias
pizzas = [
{ name: "Margherita", description: "Classic pizza with tomato, mozzarella, and basil", pizzeria: pizzerias[0] },
{ name: "Pepperoni", description: "Pepperoni, mozzarella, and tomato sauce", pizzeria: pizzerias[0] },
{ name: "Quattro Formaggi", description: "Four cheese pizza with mozzarella, gorgonzola, fontina, and parmesan", pizzeria: pizzerias[1] },
{ name: "Diavola", description: "Spicy pizza with pepperoni, chili peppers, and mozzarella", pizzeria: pizzerias[1] },
{ name: "Marinara", description: "Tomato, garlic, oregano, and extra virgin olive oil", pizzeria: pizzerias[2] },
{ name: "Prosciutto e Funghi", description: "Ham, mushrooms, mozzarella, and tomato sauce", pizzeria: pizzerias[2] }
]

Pizza.create!(pizzas)

puts "Seeding completed!"