2013-07-11 52 views
1

我是新來的Rails,目前正在開發一個博客。我想知道是否有關於如何使用Acts作爲線程評論的教程,因爲從GitHub使用的唯一指令是將acts_as_commentable添加到您的模型中。行爲如線程示例

  1. 我該如何創建新評論表單?
  2. 如何從模型中檢索所有評論?
  3. 如何配置路線才能顯示評論?
  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 

回答

0

你有沒有經歷在GitHub上的鏈接,

https://github.com/jackdempsey/acts_as_commentable

通過調用這個命令時,它會自動創建與遷移的形式。

rails g評論

您只需要運行遷移。

您正在使用哪種Rails版本?

+0

嗨,感謝您的快速響應,我的Rails版本是Rails 3.2.13,當我運行命令時,它只創建了評論的模型。我將在我編輯的問題中顯示。 – torresomar