2015-10-15 51 views
1

我仍在工作我的食譜應用程序/網站。我已經過去了一些我曾經遇到的主要問題。我現在深入到應用程序。在用戶#顯示一個收藏的食譜列表顯示

現在我目前已經有了構建和工作的優化操作。我可以隨意喜歡和不喜歡食譜。不過,我現在正在嘗試創建和編制索引,如果您願意的話,可以查看用戶收藏的所有不同食譜。我希望這份收藏的食譜列表顯示在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>&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 %> 

的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 

我意識到代碼可能相當粗糙看,但這是我建立我自己的第一個應用程序沒有教程。您可以提供的任何幫助或見解我都會很感激。這個任務似乎很簡單,但我已經把自己變成了這個瘋狂。

如果您需要更多的信息或代碼,請告訴我,我會繼續。

你們搖滾,再次感謝。

回答

1

在用戶控制器:

# 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)應該給你的是你在找什麼爲當前用戶收藏的recepies。

+1

這工作到完美,謝謝你保存我的理智。 – jammer

+1

我惹上你了!再次感謝! – jammer

+0

非常歡迎:-) –