Ruby on rails rails 4充当可注释的回复链接

Ruby on rails rails 4充当可注释的回复链接,ruby-on-rails,ruby,Ruby On Rails,Ruby,我使用acts_as_commentable来嵌套post-comments-reply。有没有人能给我举个很好的例子,特别是在评论控制器和后期显示视图方面。谢谢你的帮助 这里是model/comment.rb class Comment < ActiveRecord::Base acts_as_nested_set :scope => [:commentable_id, :commentable_type] belongs_to :commentable, :polymo

我使用acts_as_commentable来嵌套post-comments-reply。有没有人能给我举个很好的例子,特别是在评论控制器和后期显示视图方面。谢谢你的帮助

这里是model/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
这是你的表格

= 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…'
根据方法build_from(obj,user_id,comment),在您的comment_controller中创建:


仅此而已,希望对您有所帮助

无论是谁否决了这个问题,请解释原因。这个新用户应该是有疑问的。你能发布一个到目前为止你的代码的例子吗。
= 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…'
@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