diff --git a/Gemfile b/Gemfile
index 8bd546f..cc1afa7 100644
--- a/Gemfile
+++ b/Gemfile
@@ -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
diff --git a/app/controllers/sweets_controller.rb b/app/controllers/sweets_controller.rb
index 393c93a..1f96dd2 100644
--- a/app/controllers/sweets_controller.rb
+++ b/app/controllers/sweets_controller.rb
@@ -2,4 +2,8 @@ class SweetsController < ApplicationController
def index
@sweets = Sweet.all
end
+
+ def show
+ @sweet = Sweet.find(params[:id])
+ end
end
diff --git a/app/controllers/vendor_sweets_controller.rb b/app/controllers/vendor_sweets_controller.rb
new file mode 100644
index 0000000..51a61f7
--- /dev/null
+++ b/app/controllers/vendor_sweets_controller.rb
@@ -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
diff --git a/app/controllers/vendors_controller.rb b/app/controllers/vendors_controller.rb
index a1fec1d..db7050f 100644
--- a/app/controllers/vendors_controller.rb
+++ b/app/controllers/vendors_controller.rb
@@ -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
diff --git a/app/models/sweet.rb b/app/models/sweet.rb
index bd8b931..ee2dbf2 100644
--- a/app/models/sweet.rb
+++ b/app/models/sweet.rb
@@ -1,3 +1,4 @@
class Sweet < ApplicationRecord
-
+ has_many :vendor_sweets
+ has_many :vendors, through: :vendor_sweets
end
diff --git a/app/models/vendor.rb b/app/models/vendor.rb
index ce252ae..fadbfd2 100644
--- a/app/models/vendor.rb
+++ b/app/models/vendor.rb
@@ -1,3 +1,4 @@
class Vendor < ApplicationRecord
-
+ has_many :vendor_sweets
+ has_many :sweets, through: :vendor_sweets
end
diff --git a/app/models/vendor_sweet.rb b/app/models/vendor_sweet.rb
new file mode 100644
index 0000000..6425585
--- /dev/null
+++ b/app/models/vendor_sweet.rb
@@ -0,0 +1,6 @@
+class VendorSweet < ApplicationRecord
+ belongs_to :vendor
+ belongs_to :sweet
+
+ validates :vendor, :sweet, :comment, presence: true
+end
diff --git a/app/views/sweets/index.html.erb b/app/views/sweets/index.html.erb
index 9473e5d..4305db4 100644
--- a/app/views/sweets/index.html.erb
+++ b/app/views/sweets/index.html.erb
@@ -2,6 +2,6 @@
<% @sweets.each do |sweet| %>
- - <%= sweet.name %>
+ - <%= link_to sweet.name, sweet_path(sweet) %>
<% end %>
diff --git a/app/views/sweets/show.html.erb b/app/views/sweets/show.html.erb
new file mode 100644
index 0000000..d4e9a97
--- /dev/null
+++ b/app/views/sweets/show.html.erb
@@ -0,0 +1,8 @@
+<%= @sweet.name %>
+
+Vendors
+
+ <% @sweet.vendors.each do |vendor| %>
+ - <%= link_to vendor.name, vendor_path(vendor) %>
+ <% end %>
+
diff --git a/app/views/vendors/index.html.erb b/app/views/vendors/index.html.erb
index bf9fee7..ca41769 100644
--- a/app/views/vendors/index.html.erb
+++ b/app/views/vendors/index.html.erb
@@ -2,6 +2,6 @@
<% @vendors.each do |vendor| %>
- - <%= vendor.name %>
+ - <%= link_to vendor.name, vendor_path(vendor) %>
<% end %>
diff --git a/app/views/vendors/show.html.erb b/app/views/vendors/show.html.erb
new file mode 100644
index 0000000..07eae1f
--- /dev/null
+++ b/app/views/vendors/show.html.erb
@@ -0,0 +1,37 @@
+<% if @vendor_sweet.errors.any? %>
+ <%= pluralize(@vendor_sweet.errors.count, "error") %> prohibited saving:
+
+
+ <% @vendor_sweet.errors.full_messages.each do |message| %>
+ - <%= message %>
+ <% end %>
+
+<% end %>
+
+<%= @vendor.name %>
+
+Sweets
+
+ <% @vendor.vendor_sweets.each do |vendor_sweet| %>
+ - <%= vendor_sweet.sweet.name %>: <%= vendor_sweet.comment %>
+ <% end %>
+
+
+Add Sweet
+<%= form_with model: @vendor_sweet, local: true do |f| %>
+ <%= f.hidden_field :vendor_id, value: @vendor.id %>
+
+
+ <%= f.label :sweet_id %>
+
+ <%= f.collection_select :sweet_id, Sweet.all, :id, :name, include_blank: "PICK ONE!!!!!!" %>
+
+
+
+ <%= f.label :comment %>
+
+ <%= f.text_field :comment %>
+
+
+ <%= f.submit %>
+<% end %>
diff --git a/config/routes.rb b/config/routes.rb
index 2c378a4..50b7690 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -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
diff --git a/db/migrate/20240820173206_create_vendor_sweets.rb b/db/migrate/20240820173206_create_vendor_sweets.rb
new file mode 100644
index 0000000..d7afcfb
--- /dev/null
+++ b/db/migrate/20240820173206_create_vendor_sweets.rb
@@ -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
diff --git a/db/migrate/20240820174754_add_comment_to_vendor_sweets.rb b/db/migrate/20240820174754_add_comment_to_vendor_sweets.rb
new file mode 100644
index 0000000..77b3b38
--- /dev/null
+++ b/db/migrate/20240820174754_add_comment_to_vendor_sweets.rb
@@ -0,0 +1,5 @@
+class AddCommentToVendorSweets < ActiveRecord::Migration[5.1]
+ def change
+ add_column :vendor_sweets, :comment, :string
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
new file mode 100644
index 0000000..a9548f8
--- /dev/null
+++ b/db/schema.rb
@@ -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
diff --git a/db/seeds.rb b/db/seeds.rb
index 45f2145..45c628c 100644
--- a/db/seeds.rb
+++ b/db/seeds.rb
@@ -8,6 +8,7 @@
Vendor.destroy_all
Sweet.destroy_all
+VendorSweet.destroy_all
vendors = [
"Insomnia Cookies",
@@ -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