0

目前我正在试图通过public_activity添加通过acts_as_commentable_with_threading gem所有活动的意见和我有麻烦捕捉每一个活动,以获得各使用方法comment_threads使用acts_as_commentable comment_threads方法活动的评论。我知道尽可能多的逻辑应该在控制器或模型中,但是如果我在视图中遍历@activities,我如何将每个活动返回给控制器以运行.comment_threads?我向控制器添加了helper method :comment_threads,但似乎不起作用。鉴于在public_activity活动嵌套评论

注意:同样在一般情况下,我很难将acts_as_commentable_with_threading与公共活动一起用于活动馈送,因此如果有人知道更好的方式对活动馈送项目发表评论,请让我知道。

错误我得到

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

该模型活动u的跟踪...?有没有任何模型称为PostPic ..? –

+0

@RahulSingh是的,有邮政,个人资料图片和后期图片模型。 –

+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 
+0

感谢您的支持,您的路线是什么(专门针对活动和评论)?你使用嵌套资源吗? – Amir