From 908636824ac9c357deab05a1e5fff462b26209fb Mon Sep 17 00:00:00 2001 From: Matthew Thomas Date: Sun, 30 Aug 2026 10:00:41 -0400 Subject: [PATCH 1/2] Added dropdown for people to have an address --- app/controllers/people_controller.rb | 21 +++++++++++---- app/models/person.rb | 6 +++-- app/views/people/_form.html.erb | 26 +++++++++++++++++-- ...829201158_add_street_address_to_address.rb | 5 ++++ db/schema.rb | 3 ++- 5 files changed, 51 insertions(+), 10 deletions(-) create mode 100644 db/migrate/20260829201158_add_street_address_to_address.rb diff --git a/app/controllers/people_controller.rb b/app/controllers/people_controller.rb index f68035c..688ab40 100644 --- a/app/controllers/people_controller.rb +++ b/app/controllers/people_controller.rb @@ -12,11 +12,19 @@ def show # GET /people/new def new - @person = Person.new - end + @person = Person.new + @person.build_address +end + +def edit + @person.build_address if @person.address.nil? +end + + # GET /people/1/edit def edit + @person.build_address if @person.address.nil? end # POST /people or /people.json @@ -65,6 +73,9 @@ def set_person # Only allow a list of trusted parameters through. def person_params - params.expect(person: [ :first_name, :last_name, :date_of_birth, :social_security_number, :email, :relationship_id ]) - end -end + params.require(:person).permit( + :first_name, :last_name, :date_of_birth, :email, :relationship_id, + address_attributes: [ :city, :street_address, :state ] + ) + end +end \ No newline at end of file diff --git a/app/models/person.rb b/app/models/person.rb index e8c2489..206af1a 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -1,5 +1,7 @@ +# app/models/person.rb class Person < ApplicationRecord - belongs_to :relationship - + has_one :address, dependent: :destroy + accepts_nested_attributes_for :address + end diff --git a/app/views/people/_form.html.erb b/app/views/people/_form.html.erb index 0d19245..4a6404c 100644 --- a/app/views/people/_form.html.erb +++ b/app/views/people/_form.html.erb @@ -2,7 +2,6 @@ <% if person.errors.any? %>

<%= pluralize(person.errors.count, "error") %> prohibited this person from being saved:

-
- +
<%= form.label :social_security_number, style: "display: block" %> <%= form.text_field :social_security_number %> @@ -41,6 +40,29 @@ <%= form.text_field :relationship_id %>
+ <%= form.fields_for :address do |address_form| %> +
+ Address + +
+ <%= address_form.label :city, style: "display: block" %> + <%= address_form.text_field :city %> +
+ +
+ <%= address_form.label :street_address, "Street Address", style: "display: block" %> + <%= address_form.text_field :street_address %> +
+ +
+ <%= address_form.label :state, style: "display: block" %> + <%= address_form.text_field :state %> +
+ + +
+ <% end %> +
<%= form.submit %>
diff --git a/db/migrate/20260829201158_add_street_address_to_address.rb b/db/migrate/20260829201158_add_street_address_to_address.rb new file mode 100644 index 0000000..b932f4d --- /dev/null +++ b/db/migrate/20260829201158_add_street_address_to_address.rb @@ -0,0 +1,5 @@ +class AddStreetAddressToAddress < ActiveRecord::Migration[8.1] + def change + add_column :addresses, :street_address, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 36dd645..68570a3 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,12 +10,13 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.1].define(version: 2026_08_29_145325) do +ActiveRecord::Schema[8.1].define(version: 2026_08_29_201158) do create_table "addresses", force: :cascade do |t| t.string "city" t.datetime "created_at", null: false t.integer "person_id", null: false t.string "state" + t.string "street_address" t.datetime "updated_at", null: false t.index ["person_id"], name: "index_addresses_on_person_id" end From 1373a25aa1a699837b348baaf5ae79c149959c68 Mon Sep 17 00:00:00 2001 From: Matthew Thomas Date: Sun, 30 Aug 2026 10:16:16 -0400 Subject: [PATCH 2/2] Cops --- app/controllers/people_controller.rb | 2 +- app/models/person.rb | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/app/controllers/people_controller.rb b/app/controllers/people_controller.rb index 688ab40..bf5e741 100644 --- a/app/controllers/people_controller.rb +++ b/app/controllers/people_controller.rb @@ -78,4 +78,4 @@ def person_params address_attributes: [ :city, :street_address, :state ] ) end -end \ No newline at end of file +end diff --git a/app/models/person.rb b/app/models/person.rb index 206af1a..98c7c46 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -1,7 +1,5 @@ # app/models/person.rb class Person < ApplicationRecord - has_one :address, dependent: :destroy accepts_nested_attributes_for :address - -end +end \ No newline at end of file