Search code examples
htmlcssruby-on-railsfavorites

Displaying a list of favorited recipes in user#show


Im still working on my cookbook app/site. I've gotten past some of the main issues i've had. Im moving deeper into the app now.

Right now I've currently got the favoriting action built and working. I am able to favorite and unfavorite recipes at will. However I am now trying to create and index if you will of all the different recipes a user has favorited. I want this list of favorited recipes to be displayed on the users#show page, so they can just log-in and go to their profile to view their favorite recipes. However Im completely foggy on how to do so. I've all day trying to figure it out and I thought I had it, but no. Its displaying all the recipes, instead of just the favorited recipes. So I've finally broke down and decided to bring it to the geniuses (YOU GUYS!)

I'm going to provide you with a whole bunch of code, not sure if it all will be helpful or not but hopefully you will be able to quickly decipher it.

Again the goal if I haven't already made it clear is to get the Favorite Recipes to be listed on the User#show page as list.

Favorites Controller:

class FavoritesController < ApplicationController
  def create
    recipe = Recipe.find(params[:recipe_id])
    @recipe = Recipe.find(params[:recipe_id])
    favorite = current_user.favorites.build(recipe: recipe)
    authorize favorite
    if favorite.save
      flash[:notice] = "Your favorite was saved."
      redirect_to @recipe
    else
      flash[:error] = "There was an error saving your favorite."
      redirect_to @recipe
    end
  end

  def destroy
    recipe = Recipe.find(params[:recipe_id])
    @recipe = Recipe.find(params[:recipe_id])
    favorite = Favorite.find(params[:id])
     authorize favorite
    if favorite.destroy
      flash[:notice] = "Favorite successfully deleted!"
      redirect_to favorite.recipe
    else
      flash[:error] = "There was a error in deleting your favorite."
      redirect_to @recipe
    end

  end
end

Users Controller:

class UsersController < ApplicationController

  def show
    @user = User.find(params[:id])
    @recipes = Recipe.where(@favorited)
    @favorites = current_user.favorites
    @comments = current_user.comments
  end

  private

  def user_params
    params.require(:user).permit(:name, :email)
  end

end

Recipes Controller:

class RecipesController < ApplicationController
  def index
    if params[:category].present?
      @recipes = Recipe.where(category: params[:category])
    else
      @recipes = Recipe.all
    end
  end

  def show
     @recipe = Recipe.find(params[:id])
     @comments = @recipe.comments
     @comment = Comment.new
  end

  def new
    @recipe = Recipe.new
  end

  def edit
    @recipe = Recipe.find(params[:id])
    authorize @recipe
  end

  def update
    @recipe = Recipe.find(params[:id])
    authorize @recipe
    if @recipe.update_attributes(recipe_params)
      flash[:notice] = "Recipe was updated."
      redirect_to @recipe
    else
      flash[:error] = "There was an error saving the post. Please try again."
      render :edit
    end
  end


  def create
    @recipe = Recipe.new(recipe_params)
    authorize @recipe
    if @recipe.save
      flash[:notice] = "Recipe was saved."
      redirect_to @recipe
    else
      flash[:error] = "There was an error saving the post. Please try again."
      render :new
    end
  end

  private

  def recipe_params
    params.require(:recipe).permit(:title, :body, :category, :ingredient, :foodphoto, :recipecard)
  end


end

User Model:

class User < ActiveRecord::Base
  has_many :comments
  has_many :favorites, dependent: :destroy

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :confirmable

  def admin?
    role == 'admin'
  end

  def favorited(recipe)
    favorites.where(recipe_id: recipe.id).first
  end


end

Recipe Model:

class Recipe < ActiveRecord::Base
  has_many :comments
  has_many :favorites, dependent: :destroy
  mount_uploader :foodphoto, FoodPhotoUploader
  mount_uploader :recipecard, RecipeCardUploader
end

Favorites Model:

class Favorite < ActiveRecord::Base
  belongs_to :recipe
  belongs_to :user
end

Favorites Partial:

<% if policy(Favorite.new).create? %>
  <div class="favorite">
    <% if favorite = current_user.favorited(recipe) %>
      <%= link_to [recipe, favorite], class: 'btn btn-danger', method: :delete do %>
        <i class="glyphicon glyphicon-star-empty"> </i>&nbsp; Unfavorite
        <% end %>
    <% else %>
      <%= link_to [@recipe, Favorite.new], class: 'btn btn-primary', method: :recipe do %>
        <i class="glyphicon glyphicon-star"> </i>&nbsp; Favorite
      <% end %>
    <% end %>
  </div>
<% end %>

Users#show Page:

<h1>User#show</h1>
<h2>Favorite Recipes</h2>
<% @recipes.each do |recipe| %>
  <p><%= recipe.title %></p>
<% end %>

Routes.rb:

Rails.application.routes.draw do


  devise_for :users
  resources :users, only: :show
  get 'comments/index'

  get 'comments/create'

  get 'comments/show'

  get 'comments/edit'

  get 'comments/new'

  resources :recipes do
    resources :comments, only: [:create, :show, :index, :new, :destroy]
    resources :favorites, only: [:create, :destroy]
  end

  get 'drinks' => 'recipes#index', :category => "drinks"
  get 'entrees' => 'recipes#index', :category => "entrees"
  get 'sides' => 'recipes#index', :category => "sides"
  get 'desserts' => 'recipes#index', :category => "desserts"
  get 'appetizers' => 'recipes#index', :category => "appetizers"


  get 'about' => 'welcome#about'

  root to: 'welcome#index'

end

Im aware the code probably considerably rough looking, but this is my first app I'm building on my own without tutorials. Any help or insight you could provide I would be considerably grateful for. This task seems simple enough but I've driven myself into insanity on this one.

If you need any more information or code from me, please let me know and I'll jump on it.

You guys rock, thanks again.


Solution

  • In your users controller:

      # users_controller.rb
      def show
        @user = User.find(params[:id])
        @recipes = Recipe.joins(:favorites).where('favorites.user_id = ?', @user.id)
        # rest of your codes
      end
    

    This @recipes = Recipe.joins(:favorites).where('favorites.user_id = ?', @user.id) should give you the recepies favorited by the current user which is what you are looking for.