All threads / How do I display a list of Favorited Posts from forum on the User Profile page?

Ask A Question

Notifications

You’re not receiving notifications from this thread.

How do I display a list of Favorited Posts from forum on the User Profile page?

Rob Sturcke asked in Rails

I'm creating a forum and am trying to display a list of posts that have been favorited by the current user in the users/show.html.erb view.

When I favorite a post however, then go to my user profile show page, I get the following error in my app/views/favorites/_favorite.html.erb:

NameError in Users#show

undefined local variable or method `post'

<% if favorite = current_user.favorite_for(post) %>

Am I missing something in my favorites_controller that is preventing it from saving, to then be rendered as a list? Or am I rendering it in the users/show.html.erb view improperly?

Here's my favorites_controller.rb:

class FavoritesController < ApplicationController
  before_action :require_sign_in

  def create
    post = Post.find(params[:post_id])
    favorite = current_user.favorites.build(post: post)

    if favorite.save
      flash[:notice] = "Saved as favorite!"
    else
      flash[:alert] = "Favorite failed to save."
    end
    redirect_to [post.topic, post]
  end

  def destroy
     post = Post.find(params[:post_id])
     favorite = current_user.favorites.find(params[:id])

     if favorite.destroy
       flash[:notice] = "Post unfavorited."
     else
       flash[:alert] = "Unfavoriting failed."
     end
       redirect_to [post.topic, post]
   end
end

Here's how I rendered it in my users/show.html.erb:

<h2>Favorites</h2>
   <%= render @user.favorites %>

   <h2>Posts</h2>
   <%= render @user.posts %>

Also tried this for users/show.html.erb:

<h2>Favorites</h2>
   <%= render partial: @user.favorites %>

Here's my favorites/_favorite.html.erb (line #1 raised issue):

<% if favorite = current_user.favorite_for(post) %>
 <%= link_to [post, favorite], class: 'btn btn-danger', method: :delete do %>
   <i class="icon ion-ios-heart"> </i>&nbsp; Unfavorite
 <% end %>
<% else %>
 <%= link_to [post, Favorite.new], class: 'btn btn-primary', method: :post do %>
   <i class="icon ion-ios-heart-outline"> </i>&nbsp; Favorite
 <% end %>
<% end %>

Edit:
Tried a migration to AddUserToFavorites but ran into a migration error upon rake db:migrate
rails g migration AddUserToFavorites user:references

If needed here's my code:
https://github.com/robSturcke/forum

Thank you for your help.

Hi,

It seems that you're calling for a local variable to be passed in _favorite.html.erb, however your call in users/show.html.erb is simply trying to render what it knows, regardless of locals being passed.

I think you need something like this:


<%= render partial: "favorites/favorite", locals: { post: @post } %>

Hey thanks for the help. I did get a bit further with your recommendation.

I did run into a new issue that I'm working on solving with the same task of displaying favorites despite the fix. I'm having trouble defining which post I would be sending to the users#show which resulted in nil while I'm calling the .id.

NoMethodError in Users#show

def favorite_for(post)
     **favorites.where(post_id: post.id).first
  end
(added ** next to the line)

Hi Rob,

I noticed that while looking at your code the first time. I knew there may be another issue with the favorites_for method.

What I like to do when debugging is open up rails console and find a user with and without any favorites (or whatever object you're working with). Then simply try to run user.favorites.where(post_id: post.id).first and see what it returns. You may very well be having a user that doesn't have any post favorites and you aren't accounting for that in the view.

Alternatively, I think line 1 in _favorite.html.erb may be the culprit of the error.

The if should be checking for not nil (appears you're setting a variable). Put the current_user.favorite_for(post) down where you have the variable. Then it should hit your else for the other case.

You can do something like <%= render 'favorites/favorites', favorites: @user.favorites %> to render partial favorites/_favorites.html.erb

Hey Erik,

Yeah it seems as though it's not accounting for the post favorites for the user after checking rails console so I think I found my main issue in the favorites_controller.

I need to redo my favorites_controller to sort of mimic the GoRails Liking Posts controller, I see that I'm not doing a few things with the create method @post.likes.where(user_id: current_user.id).first_or_create to identify it with the user, and using a private set_post to get the :post_id. (Around the 10:00 mark of the gorails tutorial).

Unfortunately my favoriting technique I used didn't give the proper current_user I guess.

I did also try moving down the current_user.favorite_for(post) but still ran into a error with favorite_for on line 11 favorites.where(post_id: post.id).first

Thanks for your help I really appreciate it!

Hey Dmitry,

I'll give it a shot after I configure the favorites controller, I'm sure it'll work.

Thanks for the help!

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 33,665+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.