Exercise Tuesday

Goal: Build basic Sinatra web applications with full CRUD functionality. Use RESTful naming conventions for routes.

Warm Up


  • What is the difference between GET and POST requests?
  • What are the two types of embedded Ruby tags and how are they different?
  • What does the form action in a form determine in a Sinatra application?
  • How does Sinatra know which route to execute?
  • Describe how app.rb routes are used to manage data between models (classes and methods) and views (.erb files).

Code


Record Store Continued

So far our record store application isn't very useful. Once full CRUD functionality for Albums has been added, include additional properties beside name such as year, genre, artist and so on. Once this is complete, consider the following challenges:

  • Search for an Album by name. You will need an Album.search() method to do this. Write a test for Album.search() before you try to implement the rest of the functionality. Hint: You may want to review this lesson for ideas on how to write a .search() method: Mapping and the Enumerable Module. Bonus points: use a regular expression for your search implementation.
  • Sort Albums by name. You will need an Album.sort() method.
  • Add a sold() instance method to an Album. This will move the Album instance to a @@sold_albums class variable. Implement the backend functionality first (along with testing). Once that's complete, add functionality to Sinatra to "buy" an Album and view both available and sold Albums.

Riddles of the Sphinx

In Greek myth, the Sphinx guarded the gates of the city Thebes. Travelers could only pass the Sphinx if they correctly answered a riddle. Build an application where a user has to answer a series of the Sphinx's riddles in order to get into the city. Woe to the user that answers incorrectly! Your application should have a "success" page (for users that correctly answer all riddles) and a "failure" page if one of the riddles is answered incorrectly. You can find examples of the Sphinx’s riddles online or you can use your own. The focus of this project is to practice routing. Do not attempt the further explorations until you have a basic application with hard-coded riddles and complete routing first.

Further Exploration

  • Turn this into a CRUD application: users can create, update and delete their own riddles.
  • Once you have a set of riddles a user can answer, add a random option so users see different riddles each time they play. Hint: Implement a Riddle.random() class method. This method does not need to be tested.
  • Make the Sphinx more forgiving. Allow a user to have one or more chances to get a riddle wrong. If the riddle is answered incorrectly, then the Sphinx can give a clue.
  • Update your application so the Sphinx is instead guarding treasure and/or knowledge, such as endless gold or the secret of eternal life. Allow users to solve riddles to get different final outcomes (beyond just entering the city gates).

Peer Code Review


  • Are objects created from a custom class with custom methods?
  • Does Sinatra process GET and POST requests correctly?
  • Is the code clean and well-refactored?
  • Does the project have complete integration tests as well as unit tests?
  • Are all specs passing?

Lesson 24 of 37
Last updated July 28, 2022