Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save case-eee/de7ee518ba2051338986838f66c31798 to your computer and use it in GitHub Desktop.
Save case-eee/de7ee518ba2051338986838f66c31798 to your computer and use it in GitHub Desktop.
Sessions, Cookies, and Flashes Homework

Homework

  1. Using a Flash

Implement a flash[:notice] in the BooksController #create action for when you successfully create a book.

Extension: Modify your #create action to conditionally create a book depending on whether or not a name is provided. Then create a flash[:error] that holds @book.errors.full_messages.join(", "). Use a dynamic content generator to display the flash notice. Take a look at 20:33 in the Sessions, Cookies, and Flashes video for a refresher on how to do this.

  1. Storing Most Recent Book in the Session
  • Store the id of the last book added to BookShelf in the session with a key of :most_recent_book_id.
  • Add a method to your ApplicationController called most_recent_book that loads the most recent book using :most_recent_book_id stored in the session. (Hint: Make sure it doesn't break if there are no books in the database!)
  • Make that new method available to your views by making it a helper method.
  • Add this snippet to application.html.erb:
<p>
   <strong>Newest book:</strong> <%= most_recent_book.title %>
</p>

Optional Extensions:

  • Store the quantity of all books added during the user's current session with a key of [:current_book_count]
  • Store the potential revenue of all books added in the session with a key of [:current_potential_revenue]
  • Just like with all hashes if you try to access a key that has no value it will be nil, so make sure you set the initial session value to 0 so you can do calculations.
  • Add a method to your ApplicationController called current_book_summary that loads the generates a string that interpolates in the current_book_count and the current_book_potential_revenue)
  • Make that new method available to your views by making it a helper method.
  • Add this snippet to application.html.erb:
<p>
  <strong>Current Sessions Book Summary:</strong> <%= current_book_summary %>
</p>
  1. Authentication Preparation

Watch this video on authentication in preparation for tomorrow's class. Prepare questions for class tomorrow and be ready for a code-along in the morning.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment