【问题标题】:Acts as commentable with threading example作为可注释的线程示例
【发布时间】:2013-07-11 15:03:28
【问题描述】:

我是 Rails 新手,目前正在开发博客。我想知道是否有关于如何将 Acts 用作可注释线程的教程,因为从 GitHub 中使用的唯一说明是将 acts_as_commentable 添加到您的模型中。

  1. 我应该如何为新的 cmets 创建表单?
  2. 如何从模型中检索所有 cmets?
  3. 如何配置路由以显示 cmets?
  4. 如何修改我的控制器以使此功能可用?

class Comment < ActiveRecord::Base
acts_as_nested_set :scope => [:commentable_id, :commentable_type]
#attr_accessible :commentable, :body, :user_id
validates :body, :presence => true
validates :user, :presence => true

# NOTE: install the acts_as_votable plugin if you
# want user to vote on the quality of comments.
#acts_as_votable

belongs_to :commentable, :polymorphic => true

# NOTE: Comments belong to a user
belongs_to :user

# Helper class method that allows you to build a comment
# by passing a commentable object, a user_id, and comment text
# example in readme
def self.build_from(obj, user_id, comment)
new \
  :commentable => obj,
  :body        => comment,
  :user_id     => user_id
end

#helper method to check if a comment has children
def has_children?
   self.children.any?
end

# Helper class method to lookup all comments assigned
# to all commentable types for a given user.
scope :find_comments_by_user, lambda { |user|
where(:user_id => user.id).order('created_at DESC')
}

# Helper class method to look up all comments for
# commentable class name and commentable id.
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')
}

# Helper class method to look up a commentable object
# given the commentable class name and id
def self.find_commentable(commentable_str, commentable_id)
  commentable_str.constantize.find(commentable_id)
end
end

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3.2 acts-as-commentable


    【解决方案1】:

    你有没有通过github中的以下链接,

    https://github.com/jackdempsey/acts_as_commentable

    通过调用这个命令,它会自动创建带有迁移的表单。

    rails g 评论

    您只需要运行迁移。

    您使用的是哪个 Rails 版本?

    【讨论】:

    • 您好,感谢您的快速响应,我的 Rails 版本是 Rails 3.2.13,当我运行命令时,它只为评论创建了一个模型。我将在我编辑的问题中显示。
    猜你喜欢
    • 2018-04-08
    • 2014-12-05
    • 1970-01-01
    • 1970-01-01
    • 2010-12-19
    • 2021-01-24
    • 1970-01-01
    • 1970-01-01
    • 2016-05-11
    相关资源
    最近更新 更多