0

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

注意:一般来说,我在将acts_as_commentable_with_threading公共活动用于活动提要时遇到困难,因此如果有人知道更好的方法来对活动提要项目发表评论,请告诉我。

我得到的错误

在此处输入图像描述

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…"
4

1 回答 1

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…"

评论控制器.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
于 2014-02-08T17:12:45.493 回答