From c4c3d5be0930352596e487f6fa5d864ef4086b5a Mon Sep 17 00:00:00 2001 From: Mike Date: Wed, 20 Aug 2025 10:24:09 -0400 Subject: [PATCH 1/2] Setup routes and welcome page --- app/models/puppy.rb | 7 +++++++ app/views/puppies/index.html.erb | 2 ++ config/routes.rb | 3 +++ 3 files changed, 12 insertions(+) diff --git a/app/models/puppy.rb b/app/models/puppy.rb index 77f6078..edf9e33 100644 --- a/app/models/puppy.rb +++ b/app/models/puppy.rb @@ -7,4 +7,11 @@ class Puppy # TODO: Add your code here # Hint: You'll need attr_accessor for name, breed, and age # You'll also need an initialize method that takes name, breed, and age as parameters + attr_accessor :name, :breed, :age + + def initialize(name:, breed:, age:) + @name = name + @breed = breed + @age = age + end end diff --git a/app/views/puppies/index.html.erb b/app/views/puppies/index.html.erb index 2be4660..5ad23cc 100644 --- a/app/views/puppies/index.html.erb +++ b/app/views/puppies/index.html.erb @@ -8,3 +8,5 @@ Hint: Use Rails link_to helper --> +

Welcome to the Puppy Adoption Site!

+<%= link_to 'Click Here To List A Puppy', new_path %> diff --git a/config/routes.rb b/config/routes.rb index 4c97056..808f3be 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,4 +4,7 @@ # - A GET route to '/' that goes to the puppies controller index action # - A GET route to '/new' that goes to the puppies controller new action # - A POST route to '/puppy' that goes to the puppies controller create action + root 'puppies#index' + get '/new', to: 'puppies#new' + post '/puppy', to: 'puppies#create' end From 89b6f141e4675953bf918a2ae7b6b9b95b29a147 Mon Sep 17 00:00:00 2001 From: Mike Date: Wed, 20 Aug 2025 10:37:49 -0400 Subject: [PATCH 2/2] fix routes and add views and controller --- app/controllers/puppies_controller.rb | 16 ++++++++++++++++ app/models/puppy.rb | 17 +---------------- app/views/puppies/create.html.erb | 10 +++++++--- app/views/puppies/new.html.erb | 10 ++++++++++ config/routes.rb | 2 +- db/migrate/20250820143236_create_puppies.rb | 11 +++++++++++ 6 files changed, 46 insertions(+), 20 deletions(-) create mode 100644 db/migrate/20250820143236_create_puppies.rb diff --git a/app/controllers/puppies_controller.rb b/app/controllers/puppies_controller.rb index ed1730d..d73fd66 100644 --- a/app/controllers/puppies_controller.rb +++ b/app/controllers/puppies_controller.rb @@ -3,4 +3,20 @@ class PuppiesController < ApplicationController # You'll need an 'index' action to display the homepage # You'll need a 'new' action to display the form # You'll need a 'create' action to process the form submission and display the puppy + # + def index; end + + def new + # @puppy = Puppy.new + end + + def create + @puppy = Puppy.create!(puppy_params) + end + + private + + def puppy_params + params.permit(:name, :age, :breed) + end end diff --git a/app/models/puppy.rb b/app/models/puppy.rb index edf9e33..1d94ec1 100644 --- a/app/models/puppy.rb +++ b/app/models/puppy.rb @@ -1,17 +1,2 @@ -# TODO: Build out your Puppy class here -# Your puppy should have name, breed, and age attributes -# You will need to be able to pass these three attributes to initialization -# as well as readers and writers for the attributes - -class Puppy - # TODO: Add your code here - # Hint: You'll need attr_accessor for name, breed, and age - # You'll also need an initialize method that takes name, breed, and age as parameters - attr_accessor :name, :breed, :age - - def initialize(name:, breed:, age:) - @name = name - @breed = breed - @age = age - end +class Puppy < ApplicationRecord end diff --git a/app/views/puppies/create.html.erb b/app/views/puppies/create.html.erb index 6f54b98..b25deb2 100644 --- a/app/views/puppies/create.html.erb +++ b/app/views/puppies/create.html.erb @@ -2,10 +2,14 @@ TODO: Display the puppy information here! Your view should display: - - Puppy Name: [name] - - Puppy Breed: [breed] - - Puppy Age: [age] + Puppy Name: [name] + Puppy Breed: [breed] + Puppy Age: [age] Use the data passed from your controller action. Hint: Access the puppy data through instance variables from the controller --> + +

Puppy Name: <%= @puppy.name %>

+

Puppy Breed: <%= @puppy.breed %>

+

Puppy Age: <%= @puppy.age %> months

diff --git a/app/views/puppies/new.html.erb b/app/views/puppies/new.html.erb index 45ca039..faa6195 100644 --- a/app/views/puppies/new.html.erb +++ b/app/views/puppies/new.html.erb @@ -9,3 +9,13 @@ Hint: Use form_with helper with url parameter --> +

Create a New Puppy

+<%= form_with model: @puppy do |f|%> + <%= f.label :name %> + <%= f.text_field :name %> + <%= f.label :breed %> + <%= f.text_field :breed %> + <%= f.label :age %> + <%= f.number_field :age %> + <%= f.submit 'Create Puppy', id: 'submit' %> +<% end %> diff --git a/config/routes.rb b/config/routes.rb index 808f3be..255174f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,5 +6,5 @@ # - A POST route to '/puppy' that goes to the puppies controller create action root 'puppies#index' get '/new', to: 'puppies#new' - post '/puppy', to: 'puppies#create' + post '/new', to: 'puppies#create' end diff --git a/db/migrate/20250820143236_create_puppies.rb b/db/migrate/20250820143236_create_puppies.rb new file mode 100644 index 0000000..7abfd5c --- /dev/null +++ b/db/migrate/20250820143236_create_puppies.rb @@ -0,0 +1,11 @@ +class CreatePuppies < ActiveRecord::Migration[6.1] + def change + create_table :puppies do |t| + t.string :name + t.string :breed + t.integer :age + + t.timestamps + end + end +end