使用acts_as_commentable comment_threads方法在public_activity活动中嵌套注释

时间:2014-02-08 15:00:13

标签: ruby-on-rails ruby-on-rails-4 acts-as-commentable public-activity

我目前正尝试通过acts_as_commentable_with_threading gem通过public_activity为所有活动添加评论,但我无法捕获每个活动以使用方法comment_threads来获取每个活动活动的评论。我知道尽可能多的逻辑应该在控制器或模型中,但是如果我在视图中迭代@activities,我如何将每个活动带回控制器以运行.comment_threads?我将helper method :comment_threads添加到控制器,但这似乎不起作用。

注意:一般情况下,我很难将acts_as_commentable_with_threading公共活动用于活动Feed,因此如果有人知道更好的方式对活动Feed项目发表评论,请告诉我们。

我得到的错误

enter image description here

comments_controller.rb

class CommentsController < ApplicationController
  before_action :get_master

  def create
      @commentable = params[:comment][:commentable_type].constantize.find(params[:comment][:commentable_id])
      @comment = Comment.build_from( @commentable, @master.id, params[:comment][:body] )
    if @comment.save
      render :partial => "comments/comment", :locals => { comment: @comment }, :layout => false, :status => :created
    else
      render :js => "alert('error saving comment');"
    end
  end

  private

    def get_master 
      @master = Master.find(current_master.id) if master_signed_in?
    end

end

dogs_controller.rb(活动和评论在狗展页面上)

class DogsController < ApplicationController
  before_action :get_master

  helper_method :comment_threads

  def show
    @dog = @master.dogs.find(params[:id])
    @activities = PublicActivity::Activity.order("created_at desc").where(owner_id: @dog.id, owner_type: "Dog")
    @post = @dog.posts.build if signed_in?
    @photo = @dog.post_pics.build
    @new_comment = Comment.new
  end
end

_activity.html.haml部分用于狗展示页面(这是使用了comment_threads方法导致错误的地方,我会以某种方式将活动带回控制器以使用该方法)

%section
    = render 'post_form' 
- @activities.each do |activity| 
    .activity
        = render_activity activity 
    .comments
        %p Comments
        - @comments = activity.trackable_type.constantize.find(activity.trackable_id).comment_threads 
        = render :partial => 'comments/comment', collection: @comments, as: :comment 
        = simple_form_for Comment.new, :remote => true do |f|
            = f.input :body, :input_html => { :rows => "2" }, :label => false
            = f.input :commentable_id, :as => :hidden, :input_html => { :value => activity.trackable_id }
            = f.input :commentable_type, :as => :hidden, :input_html => { :value => activity.trackable_type }
            = f.button :submit, :class => "btn btn-primary", :disable_with => "Submitting…"

1 个答案:

答案 0 :(得分:1)

使用以下代码。我让评论属于活动而不是活动所遵循的模型。现在我只需要让AJAX工作。

_activity.html.haml

%section
    = render 'post_form' 
- @activities.each do |activity| 
    .activity
        = render_activity activity 
    .comments
        %p Comments
        - @comments = activity.comment_threads 
        = render :partial => 'comments/comment', collection: @comments, as: :comment 
        = simple_form_for Comment.new, :remote => true do |f|
            = f.input :body, :input_html => { :rows => "2" }, :label => false
            = f.input :commentable_id, :as => :hidden, :input_html => { :value => activity.id }
            = f.input :commentable_type, :as => :hidden, :input_html => { :value => activity }
            = f.button :submit, :class => "btn btn-primary", :disable_with => "Submitting…"

comments_controller.rb

class CommentsController < ApplicationController
  before_action :get_master

  def create
      @commentable = PublicActivity::Activity.find(params[:comment][:commentable_id])
      @comment = Comment.build_from( @commentable, @master.id, params[:comment][:body] )
    if @comment.save
      render :partial => "comments/comment", :locals => { comment: @comment }, :layout => false, :status => :created
    else
      render :js => "alert('error saving comment');"
    end
  end

  private

    def get_master 
      @master = Master.find(current_master.id) if master_signed_in?
    end