Skip to content

Live Code #5

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 13 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ end
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.1.6'
# Use sqlite3 as the database for Active Record
gem 'sqlite3', '1.5'
gem 'sqlite3', '1.5 '
# Use Puma as the app server
gem 'puma'
# Use SCSS for stylesheets
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/sweets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ class SweetsController < ApplicationController
def index
@sweets = Sweet.all
end

def show
@sweet = Sweet.find(params[:id])
end
end
18 changes: 18 additions & 0 deletions app/controllers/vendor_sweets_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class VendorSweetsController < ApplicationController
def create
@vendor_sweet = VendorSweet.new(vendor_sweet_params)
@vendor = @vendor_sweet.vendor

if @vendor_sweet.save
redirect_to vendor_path(@vendor)
else
render "vendors/show"
end
end

private

def vendor_sweet_params
params.require(:vendor_sweet).permit(:vendor_id, :sweet_id, :comment)
end
end
5 changes: 4 additions & 1 deletion app/controllers/vendors_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
class VendorsController < ApplicationController

def index
@vendors = Vendor.all
end

def show
@vendor = Vendor.find(params[:id])
@vendor_sweet = VendorSweet.new
end
end
3 changes: 2 additions & 1 deletion app/models/sweet.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
class Sweet < ApplicationRecord

has_many :vendor_sweets
has_many :vendors, through: :vendor_sweets
end
3 changes: 2 additions & 1 deletion app/models/vendor.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
class Vendor < ApplicationRecord

has_many :vendor_sweets
has_many :sweets, through: :vendor_sweets
end
6 changes: 6 additions & 0 deletions app/models/vendor_sweet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class VendorSweet < ApplicationRecord
belongs_to :vendor
belongs_to :sweet

validates :vendor, :sweet, :comment, presence: true
end
2 changes: 1 addition & 1 deletion app/views/sweets/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

<ul>
<% @sweets.each do |sweet| %>
<li><%= sweet.name %></li>
<li><%= link_to sweet.name, sweet_path(sweet) %></li>
<% end %>
</ul>
8 changes: 8 additions & 0 deletions app/views/sweets/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<h1><%= @sweet.name %></h1>

<h2>Vendors</h2>
<ul>
<% @sweet.vendors.each do |vendor| %>
<li><%= link_to vendor.name, vendor_path(vendor) %></li>
<% end %>
</ul>
2 changes: 1 addition & 1 deletion app/views/vendors/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

<ul>
<% @vendors.each do |vendor| %>
<li><%= vendor.name %></li>
<li><%= link_to vendor.name, vendor_path(vendor) %></li>
<% end %>
</ul>
37 changes: 37 additions & 0 deletions app/views/vendors/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<% if @vendor_sweet.errors.any? %>
<h2><%= pluralize(@vendor_sweet.errors.count, "error") %> prohibited saving:</h2>

<ul>
<% @vendor_sweet.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
<% end %>

<h1><%= @vendor.name %></h1>

<h2>Sweets</h2>
<ul>
<% @vendor.vendor_sweets.each do |vendor_sweet| %>
<li><%= vendor_sweet.sweet.name %>: <%= vendor_sweet.comment %></li>
<% end %>
</ul>

<h2>Add Sweet</h2>
<%= form_with model: @vendor_sweet, local: true do |f| %>
<%= f.hidden_field :vendor_id, value: @vendor.id %>

<div>
<%= f.label :sweet_id %>
<br>
<%= f.collection_select :sweet_id, Sweet.all, :id, :name, include_blank: "PICK ONE!!!!!!" %>
</div>

<div>
<%= f.label :comment %>
<br>
<%= f.text_field :comment %>
</div>

<%= f.submit %>
<% end %>
7 changes: 3 additions & 4 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
get '/sweets', to: "sweets#index", as: "sweets"
get '/vendors', to: "vendors#index", as: "vendors"

resources :sweets, only: %i[index show]
resources :vendors, only: %i[index show]
resources :vendor_sweets, only: :create
end
10 changes: 10 additions & 0 deletions db/migrate/20240820173206_create_vendor_sweets.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateVendorSweets < ActiveRecord::Migration[5.1]
def change
create_table :vendor_sweets do |t|
t.references :vendor
t.references :sweet

t.timestamps
end
end
end
5 changes: 5 additions & 0 deletions db/migrate/20240820174754_add_comment_to_vendor_sweets.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddCommentToVendorSweets < ActiveRecord::Migration[5.1]
def change
add_column :vendor_sweets, :comment, :string
end
end
37 changes: 37 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20240820174754) do

create_table "sweets", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "vendor_sweets", force: :cascade do |t|
t.integer "vendor_id"
t.integer "sweet_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "comment"
t.index ["sweet_id"], name: "index_vendor_sweets_on_sweet_id"
t.index ["vendor_id"], name: "index_vendor_sweets_on_vendor_id"
end

create_table "vendors", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

end
11 changes: 7 additions & 4 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

Vendor.destroy_all
Sweet.destroy_all
VendorSweet.destroy_all

vendors = [
"Insomnia Cookies",
Expand All @@ -27,10 +28,12 @@
"Peanut Butter Icecream Cake",
]

vendors.each do |vendor|
Vendor.create(name: vendor)
end

sweets.each do |sweet|
Sweet.create(name: sweet)
end

sweets = Sweet.all

vendors.each_with_index do |vendor, index|
Vendor.create(name: vendor, sweets: [sweets[index]])
end