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