I am trying to implement tree comments on the site using heme - acts-as-commentable-with-threading.

Comments are perfectly created and displayed on the site, if I go to the site as a user (implemented using the devise heme).

But when I try to browse pages anonymously, I naturally get an error that id cannot be called on an empty item.

Here is my recipes_controller.rb controller:

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 ... 

Here are the 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 ... 

In the view ( recipes / show.html.erb ) I insert the following render:

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

I think that perhaps you need to create something like an if ... else construct in the controller for those who simply browse the site, because by default at the moment the show method is set to current_chef, which is why the error.

    1 answer 1

    Do if else or unless in the recipes / show.html.erb view, or rather in the partial comments / template, something like

     <% if current_user_id? %> заданное значение current_chef <% else %> Войдите чтобы ответить <% end %> 

    Maybe this will help