-
Notifications
You must be signed in to change notification settings - Fork 6
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
Lsimmons98
wants to merge
1
commit into
powercodeacademy:master
Choose a base branch
from
Lsimmons98:workingBranch
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
<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 %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only define routes that you are using.
Suggested change
|
||||||||||||||||
resources :vendor_sweets, only: [:create] | ||||||||||||||||
end | ||||||||||||||||
|
||||||||||||||||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.