簡體   English   中英

樹評論Ruby on Rails

[英]Tree comments Ruby on Rails

嘗試通過gem- acts-as-commentable-with-threading在站點上實現樹狀注釋。

注釋非常好,當我在用戶訪問下訪問站點時,該注釋會顯示在站點上(通過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。

您需要在視圖中處理特殊情況(可能是評論模板)以進行匿名訪問。 原因是current_chef為零。 因此,在視圖和控制器中使用它的地方,請正確處理。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM