在 user#show 中显示最喜欢的食谱列表
Displaying a list of favorited recipes in user#show
我还在写我的食谱app/site。我已经解决了我遇到的一些主要问题。我现在正在深入研究该应用程序。
现在我已经建立并运行了最喜欢的操作。我可以随意喜欢和不喜欢的食谱。但是,如果您愿意,我现在正在尝试创建和索引用户喜欢的所有不同食谱。我希望这个最喜欢的食谱列表显示在 users#show 页面上,这样他们就可以 log-in 并转到他们的个人资料来查看他们最喜欢的食谱。但是,我完全不知道该怎么做。我整天都在想办法弄明白,我以为我明白了,但没有。它显示所有食谱,而不仅仅是最喜欢的食谱。所以我终于崩溃了,决定把它带给天才们(你们!)
我将为您提供一大堆代码,不确定是否对您有帮助,但希望您能够快速破译它。
如果我还没有说清楚的话,我的目标是让最喜欢的食谱作为列表列在用户#show 页面上。
收藏夹控制器:
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
用户模型:
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> Unfavorite
<% end %>
<% else %>
<%= link_to [@recipe, Favorite.new], class: 'btn btn-primary', method: :recipe do %>
<i class="glyphicon glyphicon-star"> </i> Favorite
<% end %>
<% end %>
</div>
<% end %>
用户#show 页面:
<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
我知道代码可能看起来相当粗糙,但这是我在没有教程的情况下自行构建的第一个应用程序。如果您能提供任何帮助或见解,我将不胜感激。这个任务看起来很简单,但我已经把自己逼疯了。
如果您需要我提供更多信息或代码,请告诉我,我会马上处理。
你们很棒,再次感谢。
在您的用户控制器中:
# 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)
应该给你当前用户喜欢的食谱,这就是你要找的。
我还在写我的食谱app/site。我已经解决了我遇到的一些主要问题。我现在正在深入研究该应用程序。
现在我已经建立并运行了最喜欢的操作。我可以随意喜欢和不喜欢的食谱。但是,如果您愿意,我现在正在尝试创建和索引用户喜欢的所有不同食谱。我希望这个最喜欢的食谱列表显示在 users#show 页面上,这样他们就可以 log-in 并转到他们的个人资料来查看他们最喜欢的食谱。但是,我完全不知道该怎么做。我整天都在想办法弄明白,我以为我明白了,但没有。它显示所有食谱,而不仅仅是最喜欢的食谱。所以我终于崩溃了,决定把它带给天才们(你们!)
我将为您提供一大堆代码,不确定是否对您有帮助,但希望您能够快速破译它。
如果我还没有说清楚的话,我的目标是让最喜欢的食谱作为列表列在用户#show 页面上。
收藏夹控制器:
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
用户模型:
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> Unfavorite
<% end %>
<% else %>
<%= link_to [@recipe, Favorite.new], class: 'btn btn-primary', method: :recipe do %>
<i class="glyphicon glyphicon-star"> </i> Favorite
<% end %>
<% end %>
</div>
<% end %>
用户#show 页面:
<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
我知道代码可能看起来相当粗糙,但这是我在没有教程的情况下自行构建的第一个应用程序。如果您能提供任何帮助或见解,我将不胜感激。这个任务看起来很简单,但我已经把自己逼疯了。
如果您需要我提供更多信息或代码,请告诉我,我会马上处理。
你们很棒,再次感谢。
在您的用户控制器中:
# 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)
应该给你当前用户喜欢的食谱,这就是你要找的。