我目前正在尝试通过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…"