From 122c5e16a2a810d35521ce7bd9c9726d431b30ed Mon Sep 17 00:00:00 2001 From: Jen Kelly Date: Tue, 19 Aug 2025 13:31:32 -0400 Subject: [PATCH 1/6] Migrate teams table, add routes, add seeds --- app/models/team.rb | 2 ++ config/routes.rb | 2 +- db/migrate/20250819171041_create_teams.rb | 15 ++++++++++++++ db/schema.rb | 25 +++++++++++------------ db/seeds.rb | 2 ++ 5 files changed, 32 insertions(+), 14 deletions(-) create mode 100644 app/models/team.rb create mode 100644 db/migrate/20250819171041_create_teams.rb create mode 100644 db/seeds.rb diff --git a/app/models/team.rb b/app/models/team.rb new file mode 100644 index 0000000..48a6c83 --- /dev/null +++ b/app/models/team.rb @@ -0,0 +1,2 @@ +class Team < ApplicationRecord +end diff --git a/config/routes.rb b/config/routes.rb index 0536571..e823e37 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,5 @@ Rails.application.routes.draw do - # TODO: Add your routes here + resources :team, only: %i[show new create] # You need: # - A GET route to '/newteam' that goes to the teams controller new action # - A POST route to '/team' that goes to the teams controller create action diff --git a/db/migrate/20250819171041_create_teams.rb b/db/migrate/20250819171041_create_teams.rb new file mode 100644 index 0000000..fb05523 --- /dev/null +++ b/db/migrate/20250819171041_create_teams.rb @@ -0,0 +1,15 @@ +class CreateTeams < ActiveRecord::Migration[6.1] + def change + create_table :teams do |t| + t.string :name + t.string :coach + t.string :pg + t.string :sg + t.string :pf + t.string :sf + t.string :c + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index ae89000..08e2960 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -1,14 +1,13 @@ -# 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. -# -# This file is the source Rails uses to define your schema when running `bin/rails -# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to -# be faster and is potentially less error prone than running all of your -# migrations from scratch. Old migrations may fail to apply correctly if those -# migrations use external dependencies or application code. -# -# It's strongly recommended that you check this file into your version control system. - -ActiveRecord::Schema.define(version: 0) do +ActiveRecord::Schema.define(version: 20_250_819_171_041) do + create_table 'teams', force: :cascade do |t| + t.string 'name' + t.string 'coach' + t.string 'pg' + t.string 'sg' + t.string 'pf' + t.string 'sf' + t.string 'c' + t.datetime 'created_at', precision: 6, null: false + t.datetime 'updated_at', precision: 6, null: false + end end diff --git a/db/seeds.rb b/db/seeds.rb new file mode 100644 index 0000000..7dc2f66 --- /dev/null +++ b/db/seeds.rb @@ -0,0 +1,2 @@ +Team.create(name: 'PHRG Ballers', coach: 'Stove McKeon', pg: 'Shantel Grey', sg: 'Kyle Housel', pf: 'Mike Clancy', + sf: 'Emily Kanarek', c: 'Nihar Patel') From 85ebdbc2ddfe0ef7366e04a6f0c40809446f6160 Mon Sep 17 00:00:00 2001 From: Jen Kelly Date: Tue, 19 Aug 2025 13:42:23 -0400 Subject: [PATCH 2/6] Write show, new, create actions --- app/controllers/teams_controller.rb | 25 ++++++++++++++++++++++--- config/routes.rb | 5 +---- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/app/controllers/teams_controller.rb b/app/controllers/teams_controller.rb index d8cd2dc..d5e086d 100644 --- a/app/controllers/teams_controller.rb +++ b/app/controllers/teams_controller.rb @@ -1,5 +1,24 @@ class TeamsController < ApplicationController - # TODO: Add your controller actions here - # You'll need a 'new' action to display the form - # You'll need a 'create' action to process the form submission + def show + @team = Team.find(params[:id]) + end + + def new + @team = Team.new(team_params) + end + + def create + @team = Team.new(team_params) + if @team.save + redirect_to @team, notice: 'Team created successfully!' + else + render :new + end + end + + private + + def team_params + params.require(:team).permit(:name, :coach, :pg, :sg, :pf, :sf, :c) + end end diff --git a/config/routes.rb b/config/routes.rb index e823e37..a1de9e2 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,3 @@ Rails.application.routes.draw do - resources :team, only: %i[show new create] - # You need: - # - A GET route to '/newteam' that goes to the teams controller new action - # - A POST route to '/team' that goes to the teams controller create action + resources :teams, only: %i[show new create] end From 5c365a325416b1942f002628a597cf0d93c9f563 Mon Sep 17 00:00:00 2001 From: Jen Kelly Date: Tue, 19 Aug 2025 13:45:36 -0400 Subject: [PATCH 3/6] Add validations --- app/models/team.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/models/team.rb b/app/models/team.rb index 48a6c83..1b671b6 100644 --- a/app/models/team.rb +++ b/app/models/team.rb @@ -1,2 +1,9 @@ class Team < ApplicationRecord + validates :name, presence: true, uniqueness: true + validates :coach, presence: true + validates :pg, presence: true + validates :sg, presence: true + validates :pf, presence: true + validates :sf, presence: true + validates :c, presence: true end From 1406d94d0a6b30708a1fc64be7b9faa4adad8cef Mon Sep 17 00:00:00 2001 From: Jen Kelly Date: Tue, 19 Aug 2025 15:10:21 -0400 Subject: [PATCH 4/6] Add sign up form --- app/controllers/teams_controller.rb | 8 +-- app/views/teams/new.html.erb | 82 +++++++++++++++++++++++++---- config/routes.rb | 2 +- 3 files changed, 76 insertions(+), 16 deletions(-) diff --git a/app/controllers/teams_controller.rb b/app/controllers/teams_controller.rb index d5e086d..439186d 100644 --- a/app/controllers/teams_controller.rb +++ b/app/controllers/teams_controller.rb @@ -1,16 +1,12 @@ class TeamsController < ApplicationController - def show - @team = Team.find(params[:id]) - end - def new - @team = Team.new(team_params) + @team = Team.new end def create @team = Team.new(team_params) if @team.save - redirect_to @team, notice: 'Team created successfully!' + redirect_to new_team_path, notice: 'Team created successfully!' else render :new end diff --git a/app/views/teams/new.html.erb b/app/views/teams/new.html.erb index 4be4bef..a86e8cd 100644 --- a/app/views/teams/new.html.erb +++ b/app/views/teams/new.html.erb @@ -5,16 +5,80 @@ Basketball Team Signup - + <%= form_with model: @team, local: true do |form| %> + <% if @team.errors.any? %> +
+

<%= pluralize(@team.errors.count, "error") %> prevented this team from being saved:

+
    + <% @team.errors.full_messages.each do |message| %> +
  • <%= message %>
  • + <% end %> +
+
+ <% end %> + +
+ <%= form.label :name %> + <%= form.text_field :name %> + <% if @team.errors[:name].any? %> + <%= @team.errors[:name].first %> + <% end %> +
+ +
+ <%= form.label :coach %> + <%= form.text_field :coach %> + <% if @team.errors[:coach].any? %> + <%= @team.errors[:coach].first %> + <% end %> +
+ +
+ <%= form.label :pg %> + <%= form.text_field :pg %> + <% if @team.errors[:pg].any? %> + <%= @team.errors[:pg].first %> + <% end %> +
+ +
+ <%= form.label :sg %> + <%= form.text_field :sg %> + <% if @team.errors[:sg].any? %> + <%= @team.errors[:sg].first %> + <% end %> +
+ +
+ <%= form.label :pf %> + <%= form.text_field :pf %> + <% if @team.errors[:pf].any? %> + <%= @team.errors[:pf].first %> + <% end %> +
+ +
+ <%= form.label :sf %> + <%= form.text_field :sf %> + <% if @team.errors[:sf].any? %> + <%= @team.errors[:sf].first %> + <% end %> +
+ +
+ <%= form.label :c %> + <%= form.text_field :c %> + <% if @team.errors[:c].any? %> + <%= @team.errors[:c].first %> + <% end %> +
+ +
+ <%= form.submit %> +
+ <% end %> diff --git a/config/routes.rb b/config/routes.rb index a1de9e2..b25c5aa 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,3 +1,3 @@ Rails.application.routes.draw do - resources :teams, only: %i[show new create] + resources :teams, only: %i[new create] end From fd5f959ee353b100fb26ff86610bdcbc965bef78 Mon Sep 17 00:00:00 2001 From: Jen Kelly Date: Tue, 19 Aug 2025 15:29:21 -0400 Subject: [PATCH 5/6] Add create page to display basketball team --- app/controllers/teams_controller.rb | 2 +- app/views/teams/create.html.erb | 23 +++++++++++------------ config/routes.rb | 3 ++- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/app/controllers/teams_controller.rb b/app/controllers/teams_controller.rb index 439186d..90d32b1 100644 --- a/app/controllers/teams_controller.rb +++ b/app/controllers/teams_controller.rb @@ -6,7 +6,7 @@ def new def create @team = Team.new(team_params) if @team.save - redirect_to new_team_path, notice: 'Team created successfully!' + render :create, notice: 'Team created successfully!' else render :new end diff --git a/app/views/teams/create.html.erb b/app/views/teams/create.html.erb index 3d56f29..fd4f31c 100644 --- a/app/views/teams/create.html.erb +++ b/app/views/teams/create.html.erb @@ -5,19 +5,18 @@ Basketball Team - diff --git a/config/routes.rb b/config/routes.rb index b25c5aa..65d4c01 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,3 +1,4 @@ Rails.application.routes.draw do - resources :teams, only: %i[new create] + get '/newteam', to: 'teams#new' + post '/teams', to: 'teams#create' end From 20385d7d9b7626276f5de5b38f2bfdaca8480712 Mon Sep 17 00:00:00 2001 From: Jen Kelly Date: Tue, 19 Aug 2025 16:00:32 -0400 Subject: [PATCH 6/6] Complete lab --- app/controllers/teams_controller.rb | 4 ++-- app/views/teams/create.html.erb | 18 +++++++++--------- app/views/teams/new.html.erb | 16 ++++++++-------- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/app/controllers/teams_controller.rb b/app/controllers/teams_controller.rb index 90d32b1..a15e946 100644 --- a/app/controllers/teams_controller.rb +++ b/app/controllers/teams_controller.rb @@ -6,9 +6,9 @@ def new def create @team = Team.new(team_params) if @team.save - render :create, notice: 'Team created successfully!' + render :new, notice: 'Team created successfully!' else - render :new + render :create end end diff --git a/app/views/teams/create.html.erb b/app/views/teams/create.html.erb index fd4f31c..5f381bc 100644 --- a/app/views/teams/create.html.erb +++ b/app/views/teams/create.html.erb @@ -8,15 +8,15 @@

Basketball Team

-
    -
  • Team Name: <%= @team.name %>
  • -
  • Coach: <%= @team.coach %>
  • -
  • Point Guard: <%= @team.pg %>
  • -
  • Shooting Guard: <%= @team.sg %>
  • -
  • Power Forward: <%= @team.pf %>
  • -
  • Small Forward: <%= @team.sf %>
  • -
  • Center: <%= @team.c %>
  • -
      + +

      Team Name: <%= @team.name %>

      +

      Coach: <%= @team.coach %>

      +

      Point Guard: <%= @team.pg %>

      +

      Shooting Guard: <%= @team.sg %>

      +

      Power Forward: <%= @team.pf %>

      +

      Small Forward: <%= @team.sf %>

      +

      Center: <%= @team.c %>

      + diff --git a/app/views/teams/new.html.erb b/app/views/teams/new.html.erb index a86e8cd..5746254 100644 --- a/app/views/teams/new.html.erb +++ b/app/views/teams/new.html.erb @@ -22,7 +22,7 @@
      <%= form.label :name %> - <%= form.text_field :name %> + <%= form.text_field :name, id: "name" %> <% if @team.errors[:name].any? %> <%= @team.errors[:name].first %> <% end %> @@ -30,7 +30,7 @@
      <%= form.label :coach %> - <%= form.text_field :coach %> + <%= form.text_field :coach, id: "coach" %> <% if @team.errors[:coach].any? %> <%= @team.errors[:coach].first %> <% end %> @@ -38,7 +38,7 @@
      <%= form.label :pg %> - <%= form.text_field :pg %> + <%= form.text_field :pg, id: "pg" %> <% if @team.errors[:pg].any? %> <%= @team.errors[:pg].first %> <% end %> @@ -46,7 +46,7 @@
      <%= form.label :sg %> - <%= form.text_field :sg %> + <%= form.text_field :sg, id: "sg" %> <% if @team.errors[:sg].any? %> <%= @team.errors[:sg].first %> <% end %> @@ -54,7 +54,7 @@
      <%= form.label :pf %> - <%= form.text_field :pf %> + <%= form.text_field :pf, id: "pf" %> <% if @team.errors[:pf].any? %> <%= @team.errors[:pf].first %> <% end %> @@ -62,7 +62,7 @@
      <%= form.label :sf %> - <%= form.text_field :sf %> + <%= form.text_field :sf, id: "sf" %> <% if @team.errors[:sf].any? %> <%= @team.errors[:sf].first %> <% end %> @@ -70,14 +70,14 @@
      <%= form.label :c %> - <%= form.text_field :c %> + <%= form.text_field :c, id: "c" %> <% if @team.errors[:c].any? %> <%= @team.errors[:c].first %> <% end %>
      - <%= form.submit %> + <%= form.submit "Submit" %>
      <% end %>