[英]Tree comments Ruby on Rails
Trying to implement a tree-like comments on the site via a gem - acts-as-commentable-with-threading . 尝试通过gem- acts-as-commentable-with-threading在站点上实现树状注释。
Comments are excellent and are displayed on the site when I visit a site under the user (implemented via the gem devise ). 注释非常好,当我在用户访问下访问站点时,该注释会显示在站点上(通过gem devise实现 )。
But when trying to view pages anonymously, naturally, I receive an error that id is not may be due to the elements onto a blank. 但是,当尝试匿名查看页面时,很自然地,我收到一个错误,ID可能不是由于元素上的空白所致。
This is my controller recipes_controller.rb : 这是我的控制器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 : 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 : recipe.rb :
class Recipe < ActiveRecord::Base
acts_as_commentable
...
In views ( recipes/show.html.erb ) I put this render : 在视图( 配方/show.html.erb )中,我把这个渲染:
<%= render partial: "comments/template", locals: {commentable: @recipe, new_comment: @comment} %>
I think that you may need in the controller to create something like a design if ... else for those who just browse the site, because the default at this point in the show method is set current_chef, because of what and error. 我认为您可能需要在控制器中创建类似于设计的东西 , 如果...否则,对于那些刚浏览站点的人,因为由于错误和原因,show方法中此时的默认设置为current_chef。
You need to handle the special case in view(probably comment template) for anonymous visit. 您需要在视图中处理特殊情况(可能是评论模板)以进行匿名访问。 Cause then current_chef
would be nil. 原因是current_chef
为零。 So where you're using it in view and controller, handle that properly. 因此,在视图和控制器中使用它的地方,请正确处理。
A tip: You don't need to assign current_chef
to any instance variable actually. 提示:您实际上不需要将current_chef
分配给任何实例变量。 It's already a helper method. 它已经是一个辅助方法。 You can call it directly from view. 您可以直接从视图中调用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.