Your local pet adoption store has asked you to build their new website. First and foremost, the store needs a form for their staff to add puppies that are available for adoption. Good thing you love puppies and coding!
- Implement RESTful routing using Rails conventions
- Implement both POST and GET requests following RESTful patterns
- Connect a controller action with both a view and a model
- Practice using Rails form helpers with proper nested naming conventions
- Display a list of existing puppies on the homepage
The focus of this lab is to build a way for a user to go to a homepage, follow a link to a form where they can enter a puppy's information, and, upon submission, view the puppy's information on a show page. The homepage should also display a list of all existing puppies.
-
Run
bundle install -
Run
bin/rails server -
Set up your routes in
config/routes.rb:- Use RESTful routing conventions with
resources :puppies - This will create all the standard RESTful routes including:
- GET
/puppies→puppies#index - GET
/puppies/new→puppies#new - POST
/puppies→puppies#create - GET
/puppies/:id→puppies#show
- GET
- Use RESTful routing conventions with
-
Create controller actions in
app/controllers/puppies_controller.rb:- Add an
indexaction to display the homepage with a list of all puppies - Add a
newaction to display the form - Add a
createaction to process the form submission and redirect to show - Add a
showaction to display the puppy information
- Add an
-
Build the homepage in
app/views/puppies/index.html.erb. This page should welcome you to the Puppy Adoption Site and include a link to the new puppy form. Use Railslink_tohelper with thenew_puppy_pathroute helper to create a link with the text "Click Here To List A Puppy". The page should also display a list of all existing puppies with their name, breed, and age, along with links to view each puppy's details. If no puppies exist yet, display a message encouraging users to add the first one. -
Create the form in
app/views/puppies/new.html.erb. You can create this form using Rails form helpers. Use Railsform_withhelper with a model parameter to create your form. The form should automatically submit to the correct route and have fields for name, breed, and age. Remember: the "submit" button should be an<input>element with atypeof"submit"and text content of "submit". -
Display the results in
app/views/puppies/show.html.erb. This view should display the info for the puppy that was just created. Display the puppy information with labels like:- Puppy Name: [name]
- Puppy Breed: [breed]
- Puppy Age: [age]
- The database and model are already set up for you
- Use
form_withwith a model parameter, not a URL parameter - After creating a puppy, redirect to the show page, don't render a create view
- Follow RESTful conventions throughout
- Form fields should use nested naming (e.g.,
puppy[name],puppy[breed],puppy[age]) - The index page should display all existing puppies with links to their show pages
Run bundle exec rspec to test your implementation. Make sure all tests pass!
The tests will check that:
- Your routes are set up correctly using RESTful conventions
- Your form has all the required fields with proper nested naming
- Your form submits to the correct endpoint
- Your show page displays all the submitted data correctly
- Your controller follows proper redirect patterns
- Your index page displays existing puppies with proper information and links
