ActiveRecord Models

Challenge

Your task is to write a RESTful routes & controllers for our Pizza Shop and fill in each with actual code. Earlier this week we had a skeleton, and now it's time to fill it out.

There are two important details to keep in mind. Tomorrow morning, we'll be demonstrating how to use HTML forms to send data from user input to our controller actions, which will be the final piece of the puzzle for creating a full blown web application.

  • You don't have to create the controller actions for new and edit. Skip those until tomorrow (unless you want to jump ahead on your own).
  • Hardcode your params for now. You'll need to specifically in your create and update actions. We'll get those to be real data tomorrow.
#create
post "/pizzas" do
    params = {name: "Red Anchovy Delight", sauce: 'red', cheese:true, mushrooms:true, extra_toppings: "anchovies"}
    #...

In the last 10 minutes, we'll walk through a complete solution example so you can gauge how you did!

Exercise

####Requirements

  • Write a RESTful controller for our pizza resource with index, show, create, update, and delete routes. You should be using (but not limited to) the following ActiveRecord methods:

    • .find
    • .all
    • .new
    • .save
    • .update_attributes
    • .destroy
  • Fill in each controller action with the CRUD operation that is appropriate for that action

  • Don't forget about params

Starter Code

The same code as our lesson is included in the starter-code folder. The Sinatra setup is ready to go, you just need to fill in your controller.

Deliverable

Shoot to create a complete RESTful controller with appropriate ActiveRecord methods inside and compare your work with the solution code when complete.

You'll get a lot more practice with this so don't sweat it if you're struggling, but if you are:

  • Don't freeze up, try things out
  • Read all errors carefully
  • Identify your knowledge gaps
  • Formulate a question
  • ASK THAT QUESTION!

###Resources