Skip to content

Create Vendor_sweets #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
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), notice: "Sweet added to vendor!"
else
render 'vendors/show'
end
end

private

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

def index
@vendors = Vendor.all
end

def show
@vendor = Vendor.find(params[:id])
@vendor_sweet = VendorSweet.new
end

end
2 changes: 2 additions & 0 deletions app/models/sweet.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
class Sweet < ApplicationRecord
has_many :vendor_sweets
has_many :vendors, through: :vendor_sweets

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

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

validates :vendor_id, :sweet_id, :comment, presence: true


end
Comment on lines +1 to +8
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
class VendorSweet < ApplicationRecord
belongs_to :vendor
belongs_to :sweet
validates :vendor_id, :sweet_id, :comment, presence: true
end
class VendorSweet < ApplicationRecord
belongs_to :vendor
belongs_to :sweet
validates :vendor_id, :sweet_id, :comment, presence: true
end

7 changes: 7 additions & 0 deletions app/views/sweets/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<h1><%[email protected]%></h1>

<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>
20 changes: 20 additions & 0 deletions app/views/vendors/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<h1><%[email protected]%></h1>

<h2> Comments </h2>
<ul>
<% @vendor.vendor_sweets.each do |vendor_sweet| %>
<li><%= link_to vendor_sweet.sweet.name, sweet_path(vendor_sweet.sweet) %> - <%= vendor_sweet.comment %></li>
<% end %>
</ul>



<%= form_for @vendor_sweet do |f|%>
Comment on lines +8 to +12
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
</ul>
<%= form_for @vendor_sweet do |f|%>
</ul>
<%= form_for @vendor_sweet do |f|%>

<div class="field">
<%= f.hidden_field :vendor_id, value: @vendor.id%>
<%= f.label :sweet_id, "Select Sweet" %><br>
<%= f.collection_select :sweet_id, Sweet.all, :id, :name %><br>
<%= f.text_field :comment %>
<%= f.submit "Add Sweet"%>
</div>
<% end %>
11 changes: 8 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
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
resources :vendors
resources :vendor_sweets


resources :vendors do
Comment on lines +4 to +7
Copy link
Contributor

@stephenmckeon stephenmckeon Aug 22, 2024

Choose a reason for hiding this comment

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

Only define routes that you are using.

Suggested change
resources :vendor_sweets
resources :vendors do
resources :vendor_sweets
resources :vendors do

resources :vendor_sweets, only: [:create]
end

end
9 changes: 9 additions & 0 deletions db/migrate/20240819154344_create_vendor_sweets.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateVendorSweets < ActiveRecord::Migration[5.1]
def change
create_table :vendor_sweets do |t|
t.references :vendor, foreign_key: true
t.references :sweet, foreign_key: true
t.string :comment
end
end
end
35 changes: 35 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# 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: 20240819154344) 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.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
56 changes: 28 additions & 28 deletions db/seeds.rb
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
# 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)

# Clear existing data
VendorSweet.destroy_all
Vendor.destroy_all
Sweet.destroy_all

vendors = [
"Insomnia Cookies",
"Cookies Cream",
"Carvel",
"Gregory's Coffee",
"Duane Park Patisserie",
"Tribeca Treats",
# Create some sweets
sweets = [
Sweet.create(name: "Chocolate Bar"),
Sweet.create(name: "Candy Cane"),
Sweet.create(name: "Lollipop"),
Sweet.create(name: "Gummy Bears"),
Sweet.create(name: "Marshmallows")
]

sweets = [
"Chocolate Chip Cookie",
"Chocolate Chunk Cookie",
"M&Ms Cookie",
"White Chocolate Cookie",
"Brownie",
"Peanut Butter Icecream Cake",
# Create some vendors
vendors = [
Vendor.create(name: "Sweet Tooth"),
Vendor.create(name: "Candy Land"),
Vendor.create(name: "Sugar Rush"),
Vendor.create(name: "Choco Delight"),
Vendor.create(name: "Gummy Galore")
]

vendors.each do |vendor|
Vendor.create(name: vendor)
end
# Associate vendors with sweets and add a comment
VendorSweet.create(vendor: vendors[0], sweet: sweets[0], comment: "Best-selling chocolate bar!") # Sweet Tooth sells Chocolate Bar
VendorSweet.create(vendor: vendors[0], sweet: sweets[1], comment: "Perfect for the holidays!") # Sweet Tooth sells Candy Cane
VendorSweet.create(vendor: vendors[1], sweet: sweets[2], comment: "Kids love these lollipops!") # Candy Land sells Lollipop
VendorSweet.create(vendor: vendors[1], sweet: sweets[3], comment: "Gummy bears are a classic!") # Candy Land sells Gummy Bears
VendorSweet.create(vendor: vendors[2], sweet: sweets[4], comment: "Fluffy and sweet marshmallows!") # Sugar Rush sells Marshmallows
VendorSweet.create(vendor: vendors[2], sweet: sweets[0], comment: "Chocolate bars are a hit!") # Sugar Rush sells Chocolate Bar
VendorSweet.create(vendor: vendors[3], sweet: sweets[1], comment: "Candy canes all year round!") # Choco Delight sells Candy Cane
VendorSweet.create(vendor: vendors[3], sweet: sweets[2], comment: "Delightful lollipops!") # Choco Delight sells Lollipop
VendorSweet.create(vendor: vendors[4], sweet: sweets[3], comment: "Best gummy bears in town!") # Gummy Galore sells Gummy Bears
VendorSweet.create(vendor: vendors[4], sweet: sweets[4], comment: "Perfect for roasting!") # Gummy Galore sells Marshmallows

sweets.each do |sweet|
Sweet.create(name: sweet)
end
puts "Seeding completed!"