From 9625610191107ccf54cee5c066a713d3891b3ae1 Mon Sep 17 00:00:00 2001 From: Shantel Date: Wed, 20 Aug 2025 14:43:35 -0400 Subject: [PATCH 1/3] Forms Basic Lab 2 --- Gemfile | 1 + Gemfile.lock | 5 +++++ app/controllers/puppies_controller.rb | 23 +++++++++++++++++++++++ app/models/puppy.rb | 8 ++++++++ app/views/puppies/index.html.erb | 6 ++++++ app/views/puppies/new.html.erb | 19 +++++++++++++++++++ app/views/puppies/show.html.erb | 7 +++++++ config/routes.rb | 4 ++++ 8 files changed, 73 insertions(+) diff --git a/Gemfile b/Gemfile index 1a1b15d..293548c 100644 --- a/Gemfile +++ b/Gemfile @@ -7,6 +7,7 @@ gem 'fiddle' gem 'logger' gem 'mutex_m' gem 'ostruct' +gem 'pry' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails', branch: 'main' gem 'rails', '~> 6.1.3', '>= 6.1.3.1' diff --git a/Gemfile.lock b/Gemfile.lock index caea9fb..b96a561 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -76,6 +76,7 @@ GEM rack-test (>= 0.6.3) regexp_parser (>= 1.5, < 3.0) xpath (~> 3.2) + coderay (1.1.3) concurrent-ruby (1.3.5) crass (1.0.6) date (3.4.1) @@ -131,6 +132,9 @@ GEM ast (~> 2.4.1) racc prism (1.4.0) + pry (0.15.2) + coderay (~> 1.1) + method_source (~> 1.0) public_suffix (6.0.2) puma (5.6.9) nio4r (~> 2.0) @@ -253,6 +257,7 @@ DEPENDENCIES mutex_m nio4r (~> 2.7) ostruct + pry puma (~> 5.0) rack-cors rails (~> 6.1.3, >= 6.1.3.1) diff --git a/app/controllers/puppies_controller.rb b/app/controllers/puppies_controller.rb index d682f57..8ee27dd 100644 --- a/app/controllers/puppies_controller.rb +++ b/app/controllers/puppies_controller.rb @@ -6,4 +6,27 @@ class PuppiesController < ApplicationController # You'll need a 'show' action to display the puppy information # # Remember: After creating a puppy, you should redirect to the show page + + def index + @puppies = Puppy.all + end + + def new + @puppy = Puppy.new + end + + def show + @puppy = Puppy.find(params[:id]) + end + + def create + @puppy = Puppy.create(puppy_params) + render :show + end + + private + + def puppy_params + params.require(:puppy).permit(:name, :breed, :age) + end end diff --git a/app/models/puppy.rb b/app/models/puppy.rb index 1d94ec1..2dccd8c 100644 --- a/app/models/puppy.rb +++ b/app/models/puppy.rb @@ -1,2 +1,10 @@ +# 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 < ApplicationRecord + def to_s + name + ' ' + breed + ' ' + age + end end diff --git a/app/views/puppies/index.html.erb b/app/views/puppies/index.html.erb index 237462e..bd6ef2a 100644 --- a/app/views/puppies/index.html.erb +++ b/app/views/puppies/index.html.erb @@ -1,3 +1,9 @@ +

Puppy Adoption Site

+ +
<%= link_to "Click Here To List A Puppy", '/new' %>
+ + + diff --git a/app/views/puppies/new.html.erb b/app/views/puppies/new.html.erb index 502a53d..2ef4b01 100644 --- a/app/views/puppies/new.html.erb +++ b/app/views/puppies/new.html.erb @@ -17,15 +17,3 @@ <%= form.submit "submit" %> <% end %> - diff --git a/app/views/puppies/show.html.erb b/app/views/puppies/show.html.erb index 1433024..9a1540a 100644 --- a/app/views/puppies/show.html.erb +++ b/app/views/puppies/show.html.erb @@ -4,15 +4,3 @@

Puppy Breed: <%= @puppy[:breed] %>

Puppy Age: <%= @puppy[:age]%>

- - diff --git a/config/routes.rb b/config/routes.rb index 7d794bd..263818e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,8 +3,4 @@ get '/puppies/new', to: 'puppies#new' post '/puppies', to: 'puppies#create' get '/puppies/:id', to: 'puppies#show' - # TODO: Add your routes here - # You need to use RESTful routing conventions - # Hint: Use the resources method to create all the standard RESTful routes - # This will create routes for index, show, new, create, edit, update, and destroy end From 12a0d6f89784e8c0f8c9e78d0617eeea7fbe85db Mon Sep 17 00:00:00 2001 From: Shantel Date: Wed, 20 Aug 2025 15:49:27 -0400 Subject: [PATCH 3/3] Just Use Resources --- app/controllers/puppies_controller.rb | 1 + config/routes.rb | 4 +--- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/app/controllers/puppies_controller.rb b/app/controllers/puppies_controller.rb index a779e92..6042fc2 100644 --- a/app/controllers/puppies_controller.rb +++ b/app/controllers/puppies_controller.rb @@ -1,3 +1,4 @@ +class PuppiesController < ApplicationController def index @puppies = Puppy.all end diff --git a/config/routes.rb b/config/routes.rb index 263818e..727fe27 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,4 @@ Rails.application.routes.draw do + resources :puppies, only: %i[show new create] get '/', to: 'puppies#index' - get '/puppies/new', to: 'puppies#new' - post '/puppies', to: 'puppies#create' - get '/puppies/:id', to: 'puppies#show' end