2016-01-06 85 views
1

試圖通過一個寶石在網站上實施樹狀評論 - 行爲作爲評論與線程樹評論Ruby on Rails

評論非常好,當我訪問用戶下的網站(通過gem devise實施)時,該網站會顯示在網站上。

但是,當試圖匿名查看網頁時,我收到一個錯誤,id不是可能是由於元素到一個空白。

這是我的控制器recipes_controller.rb

class RecipesController < ApplicationController 
    before_action :authenticate_chef!, except: [:index, :show] 
    def show 
     @recipe = Recipe.find(params[:id]) 
     @comments = @recipe.comment_threads.order('created_at desc') 
     @user_who_commented = current_chef 
     @new_comment = Comment.build_from(@recipe, @user_who_commented.id, "") 
    end 
... 

comments_controller.rb

class CommentsController < ApplicationController 
    before_action :authenticate_chef! 

    def create 
    commentable = commentable_type.constantize.find(commentable_id) 
    @user_who_commented = current_chef 
    @comment = Comment.build_from(commentable, @user_who_commented.id, body) 

    respond_to do |format| 
     if @comment.save 
     make_child_comment 
     format.html { redirect_to(:back, :notice => 'Comment was successfully added.') } 
     else 
     format.html { render :action => "new" } 
     end 
    end 
    end 
... 

recipe.rb

class Recipe < ActiveRecord::Base 
    acts_as_commentable 
... 

在視圖(食譜/ show.html.erb)我把這個渲染:

<%= render partial: "comments/template", locals: {commentable: @recipe, new_comment: @comment} %> 

我認爲你可能需要在控制器像一個設計如果創造的東西...其他爲那些只是瀏覽網站的人,因爲默認此時在show方法中設置current_chef,因爲什麼和錯誤。

回答

0

您需要處理匿名訪問中的特殊情況(可能爲評論模板)。原因然後current_chef將是零。因此,您在視圖和控制器中使用它的地方,請妥善處理。

建議:您不需要將current_chef實際分配給任何實例變量。這已經是一個輔助方法。你可以直接從視圖中調用它。