0

I am currently trying to add comments via the acts_as_commentable_with_threading gem for all activities via public_activity and I am having trouble capturing each activity to use the the method comment_threads on in order to get each activity's comments. I know as much logic as possible should be in the controllers or models but if I iterate through @activities in the view, how do I take each activity back to the controller to run .comment_threads on? I added helper method :comment_threads to the controller but that doesn't seem to work.

Note: also in general I'm having difficulty using the acts_as_commentable_with_threading with public activity for an activity feed so if anyone knows a better ways to have comments on activity feed items please let me know.

the error I'm getting

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 (activities and comments are on the dog show page)

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 partial for dog show page (this is where the comment_threads method is used which is causing the error and which I would somehow like to take the activity back to the controller to use the method there)

%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…"
Kirti Thorat
  • 52,578
  • 9
  • 101
  • 108
monty_lennie
  • 2,871
  • 2
  • 29
  • 49

1 Answers1

1

Got it working with following code. I let comments belong to an activity instead of the model an activity was following. Now I just have to get the AJAX working.

_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
monty_lennie
  • 2,871
  • 2
  • 29
  • 49
  • Thanks for this, what were your routes (specifically for Activities and Comments)? Did you use nested resources? – Amir Dec 15 '14 at 16:06