2016-01-06 129 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实际分配给任何实例变量。这已经是一个辅助方法。你可以直接从视图中调用它。