shou2017.com
JP

Rails: Displaying a List of Favorites for the Logged-in User

Mon Dec 10, 2018
Sat Aug 10, 2024

I’m using Devise for user authentication.

We can use Devise’s current_user and Ruby’s where method to find the user’s favorites.

class BookmarkController < ApplicationController
  def index
    @user = current_user
    @bookmarks = Bookmark.where(user_id: @user.id).all
  end
end

In the view:

<% @bookmarks.each  do |bookmark| %>
  <%= bookmark.name %>
<% end  %>
See Also