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 a POST request to the controller to display data from a user in the view
- Implement both POST and GET requests
- Connect a controller action with both a view and a model
- Practice using Rails form helpers and routing
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.
-
Run
bundle install
-
Run
bin/rails server
-
Build out a puppy class in
app/models/puppy.rb
. Puppies 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. -
Set up your routes in
config/routes.rb
:- Create a route that responds to a GET request at
/
that goes topuppies#index
- Create a route that responds to a GET request at
/new
that goes topuppies#new
- Create a route that responds to a POST request at
/puppy
that goes topuppies#create
- Create a route that responds to a GET request at
-
Create controller actions in
app/controllers/puppies_controller.rb
:- Add an
index
action to display the homepage - Add a
new
action to display the form - Add a
create
action to process the form submission and pass data to the view
- 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_to
helper to create a link with the text "Click Here To List A Puppy" that goes to/new
. -
Create the form in
app/views/puppies/new.html.erb
. You can create this form using Rails form helpers. Remember, you'll need to set up the form to submit to the correct route. Use Railsform_with
helper to create your form. The form should submit to/puppy
using POST method and have fields for name, breed, and age. Remember: the "submit" button should be an<input>
element with atype
of"submit"
and text content of "submit". -
Display the results in
app/views/puppies/create.html.erb
. Make sure the form is being submitted properly. 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]
Run bundle exec rspec
to test your implementation. Make sure all tests pass!
The tests will check that:
- Your routes are set up correctly
- Your form has all the required fields
- Your form submits to the correct endpoint
- Your results page displays all the submitted data correctly
- Your Puppy class works as expected
Please open a GitHub issue or pull-request. Provide a detailed description that explains the issue you have found or the change you are proposing. Then "@" mention your instructor on the issue or pull-request, and send them a link via Connect.
View Sinatra Basic Forms Lab on Learn.co and start learning to code for free.
PHRG Sinatra Basic Forms Lab