Html 在user#show中显示喜爱的食谱列表

Html 在user#show中显示喜爱的食谱列表,html,css,ruby-on-rails,favorites,Html,Css,Ruby On Rails,Favorites,我还在我的烹饪书应用程序/网站上工作。我已经克服了我遇到的一些主要问题。我现在正在深入应用程序 现在,我已经建立了一个有利于我的行动,并开始运作。我可以随意选择最喜欢和最不喜欢的食谱。然而,我现在正试图创建和索引,如果你愿意的所有不同的食谱,一个用户喜欢。我希望在用户的“显示”页面上显示此喜爱的食谱列表,这样他们就可以登录并转到他们的个人资料查看他们喜爱的食谱。然而,我完全不知道该怎么做。我花了一整天的时间想弄明白,我想我已经弄明白了,但是没有。它显示了所有的食谱,而不仅仅是喜欢的食谱。所以我终

我还在我的烹饪书应用程序/网站上工作。我已经克服了我遇到的一些主要问题。我现在正在深入应用程序

现在,我已经建立了一个有利于我的行动,并开始运作。我可以随意选择最喜欢和最不喜欢的食谱。然而,我现在正试图创建和索引,如果你愿意的所有不同的食谱,一个用户喜欢。我希望在用户的“显示”页面上显示此喜爱的食谱列表,这样他们就可以登录并转到他们的个人资料查看他们喜爱的食谱。然而,我完全不知道该怎么做。我花了一整天的时间想弄明白,我想我已经弄明白了,但是没有。它显示了所有的食谱,而不仅仅是喜欢的食谱。所以我终于崩溃了,决定把它带给天才们(你们这些家伙!)

我将为您提供一整套代码,不确定这些代码是否有用,但希望您能够快速破译它们

同样,如果我还没有明确说明的话,我的目标是将最喜欢的食谱列在用户的“显示为列表”页面上

收藏夹控制器:

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
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
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
  # 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
我知道代码可能相当粗糙,但这是我第一个没有教程自己开发的应用程序。如果您能提供任何帮助或见解,我将不胜感激。这项任务似乎很简单,但在这件事上我已经把自己逼疯了

如果您需要我提供更多的信息或代码,请告诉我,我会立即提供


你们太棒了,再次感谢。

在用户控制器中:

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
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
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
  # 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

这个
@recipes=Recipe.joins(:favorites)。where('favorites.user_id=?',@user.id)
应该会给你当前用户喜欢的接收者,这就是你要找的。

这个工作做得非常好,谢谢你让我保持理智。我帮你搞定了!再次感谢!
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
  # 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