html - 在 user#show 中显示最喜欢的食谱列表

标签 html css ruby-on-rails favorites

我仍在开发我的食谱应用程序/网站。我已经解决了我遇到的一些主要问题。我现在正在更深入地研究该应用程序。

现在我已经建立并运行了收藏操作。我可以随意喜欢和不喜欢的食谱。但是,如果您愿意,我现在正在尝试创建和索引用户喜欢的所有不同食谱。我希望这个最喜欢的食谱列表显示在 users#show 页面上,这样他们就可以登录并转到他们的个人资料来查看他们最喜欢的食谱。但是,我完全不知道该怎么做。我整天都在想办法弄明白,我以为我明白了,但没有。它显示所有食谱,而不仅仅是最喜欢的食谱。所以我终于崩溃了,决定把它带给天才们(你们!)

我将向您提供一大堆代码,不确定它们是否对您有帮助,但希望您能够快速破译它。

如果我还没有说清楚的话,我的目标是让最喜欢的食谱作为列表列在 User#show 页面上。

收藏夹 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

用户 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

食谱 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

用户模型:

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

配方模型:

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

收藏模型:

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

部分收藏夹:

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

用户#显示页面:

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

路线.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

我知道代码可能看起来相当粗糙,但这是我在没有教程的情况下自行构建的第一个应用程序。如果您能提供任何帮助或见解,我将不胜感激。这个任务看起来很简单,但我已经把自己逼疯了。

如果您需要我提供更多信息或代码,请告诉我,我会马上处理。

你们太棒了,再次感谢。

最佳答案

在你的用户 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

这个 @recipes = Recipe.joins(:favorites).where('favorites.user_id = ?', @user.id) 应该给你当前用户最喜欢的食谱你正在寻找。

关于html - 在 user#show 中显示最喜欢的食谱列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33157343/

相关文章:

javascript - 在 Javascript 类池之间切换

包含 .slim 文件的 jquery DOM

div容器内div的CSS水平对齐方式?

html - Bootstrap : Responsive row of equal-height images, 基于图像纵横比的宽度

javascript - 是否有一个 JQuery 插件可以让浏览器从当前位置平滑地向下滚动到 200 像素?

ruby-on-rails - AWS ECS 上 at_exit 中的 SignalException

html - 图像不适用于容器宽度的 50%

javascript - 有没有办法为 createElement 对象分配唯一 ID?

ruby-on-rails - 提交按钮文本来自哪里(Ruby on rails)?

ruby-on-rails - request.remote_ip 返回错误的 ip