Homework for 8

Exercise: Rails Navigation with the Treasure Hunt App

We need to clone the Treasure-Hunt repository. Paste the following code into your terminal.

git clone https://github.com/nax3t/Treasure-Hunt

Now cd into the Treasure-Hunt folder and navigate through it in sublime to complete the assignment.

Grab your treasure map! Navigate through this app and answer the questions below:

  1. How many routes does this project have? (take a look at routes.rb and make a guess, rake routes will not work because you can't use bundle for this project, your computers don't have postgresql installed yet)
  2. How many models?
  3. How many controllers?
  4. Where does the CRUD logic take place?
  5. Which view allows us to create a new treasure?
  6. Which view allows us to comment on an existing treasure?
  7. Which view is our home page?
  8. Which view lists all the treasures?
  9. Which attributes belong to treasures? And comments?
  10. Which file displays the attributes for each model?

Submit your answers via a private post on Piazza

Projects

Web Blog

Finish working on the weblog from Rails Guides Getting Started by completing Section 6 to the end.

Lekker Plekke

Finish a basic version of Lekker Plekke by completing the Bonus from Exercise 7B.

To refresh your memory:

  1. Add create, new, edit, update and destroy methods.
  2. Add appropriate views for creating and editing places. (hint: forms may help)
  3. Add a link to the show page to edit and delete places using the correct action methods. Add a show link to each place on the index page.

Make sure the Place has at least a name and description attribute.

BONUS: Seeing is Believing

If you want some extra, optional fun, add an image attribute that takes in an image url, and then render that image on a place's show page! Lekker!

Refer to Example Lekker Plekke if you are interested in example behavior.

Displaying places is all fun and stuff but, let's be real, we all know it's all about the likes and comments.

  1. Create a new migration to add the attribute "likes" to your Place model. If you want to keep track of the number of likes a location has, what data type do we want to make it? We also need to set "likes" to a default value. If a place starts out with no likes, what is a default value that makes sense?
  2. Add a method called liked! in the Place model that will increment the number of likes a place has by 1.
    • Concept Challenge Question: What's the purpose of this method? Why not put this in the controller?
    • Concept Challenge Question #2: Why do we use the the exclamation point in our method name here?
  3. Add a method called like in the places controller that calls the method liked! on the current place instance.
  4. Let's create a custom route. We want to GET '/places/:id/like', which will hit the like method in our places controller. What's the syntax for creating this route? (Hint: This is tricky, and there's a couple of ways to approach this. Google will help you out! Rails Guides might also help you out!). Run rake routes to make sure you have the correct routes.
  5. On the show page for a place, add a link that says "like", and also display the number of likes the place has (For example: "Likes: 4" or "This place has 4 likes!"). Note that a link's default verb is "GET". Make sure you have the correct path (Hint: Find this path by looking at your routes!).

Just For Fun

Cool beans. Food for thought: every time someone likes a place the page refreshes just to update one number! That seems kinda inefficient huh, and remember we're trying to be as lazy as possible. What do you think would make more sense? We'll learn something later this month to fix this!