Liked Products Page
Premium Content - Free Preview
Now that the Like model, relationship and methods are set up, let's create a page for users to view all their liked products. Afterwards, we'll create a button for liking products.
Create a page called my_page that users can visit at /my_page.
Guideline: Create a
my_page controller action in UsersController and a
my_page file in views/users. Create the necessary route to point to it.
Create the controller action:
class UsersController < ApplicationController def my_page end end
Create the file
views/users/my_page.html.erb and add some text to it, such as:
Add the following route to
get 'my_page', to: 'users#my_page'
You should now be able to navigate to /my_page.
Filling in my_page
Manual Sample Data
If you haven't already done so, create a few likes in the command line so you can see how my_page displays them before you even create the like button. (Make sure to create the likes for the user account you're logged in on.)
Displaying Liked Products
Add code to your my_page file to display the products for the current user. You may find the Devise
current_user helper method to be useful. Can you display all the liked products with one line of code?
Since you already have a _product partial, it's very easy to display all the products in a collection: