我仍在工作我的食谱应用程序/网站。我已经过去了一些我曾经遇到的主要问题。我现在深入到应用程序。在用户#显示一个收藏的食谱列表显示
现在我目前已经有了构建和工作的优化操作。我可以随意喜欢和不喜欢食谱。不过,我现在正在尝试创建和编制索引,如果您愿意的话,可以查看用户收藏的所有不同食谱。我希望这份收藏的食谱列表显示在users#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 %>
用户#显示页:
<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
我意识到代码可能相当粗糙看,但这是我建立我自己的第一个应用程序没有教程。您可以提供的任何帮助或见解我都会很感激。这个任务似乎很简单,但我已经把自己变成了这个疯狂。
如果您需要更多的信息或代码,请告诉我,我会继续。
你们摇滚,再次感谢。
这工作到完美,谢谢你保存我的理智。 – jammer
我惹上你了!再次感谢! – jammer
非常欢迎:-) –