- 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.
- 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
ApplicationControllercalledmost_recent_bookthat loads the most recent book using:most_recent_book_idstored 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
ApplicationControllercalledcurrent_book_summarythat loads the generates a string that interpolates in thecurrent_book_countand thecurrent_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>- 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.