2014-03-13 1 views
0

게시 - 의견 회신을 중첩하기 위해 acts_as_commentable을 사용하고 있습니다. 댓글 컨트롤러 및 게시보기에서 특히 좋은 예를 보여줄 수있는 사람이 있습니까? 당신의 도움에 감사드립니다.레일 4는 주석 처리 가능한 회신 링크 역할을합니다.

+0

을 helpfull 홉, 이유를 설명하십시오. 이 새로운 사용자에게는 의심의 이익이 주어져야합니다. –

+0

지금까지 코드 예제를 게시 할 수 있습니까? –

답변

0

이 모델/comment.rb

class Comment < ActiveRecord::Base 
    acts_as_nested_set :scope => [:commentable_id, :commentable_type] 

    belongs_to :commentable, :polymorphic => true 

    belongs_to :user 

    def self.build_from(obj, user_id, comment) 
    new \ 
     :commentable => obj, 
     :body  => comment, 
     :user_id  => user_id 
    end 

    def has_children? 
    self.children.any? 
    end 

    scope :find_comments_by_user, lambda { |user| 
    where(:user_id => user.id).order('created_at DESC') 
    } 

    scope :find_comments_for_commentable, lambda { |commentable_str, commentable_id| 
    where(:commentable_type => commentable_str.to_s, :commentable_id => commentable_id).order('created_at DESC') 
    } 

    def self.find_commentable(commentable_str, commentable_id) 
    commentable_str.constantize.find(commentable_id) 
    end 

    def owner?(user) 
    if user.present? 
     return self.user_id == user.id 
    else 
     return false 
    end 
    end 
end 

이 컨트롤러 쇼입니다

@comments = @project.comment_threads.order('created_at desc').page(params[:page]) 

@new_comment = Comment.build_from(@project, @user.id, '') 

이 (양식 방법 build_from에 따르면

= simple_form_for comment, remote: true do |f| 
    = f.input :body, input_html: { rows: '2' }, label: 'Content' 
    = f.input :commentable_id, as: :hidden, value: comment.commentable_id 
    = f.input :commentable_type, as: :hidden, value: comment.commentable_type 
    = f.button :submit, 'Done', class: 'btn btn-primary', disable_with: 'Submitting…' 

이다 OBJ 266 댓글)에서 귀하의 comment_controller # 생성 :

모두의
@user = current_user 
@comment_hash = params[:comment] 
@obj = @comment_hash[:commentable_type].constantize.find(@comment_hash[:commentable_id]) 
@comment = Comment.build_from(@obj, current_user.id, @comment_hash[:body]) 
@comment.save 

, 그것을 아래이 질문에 투표 누구든지

관련 문제