1

私は Rails を初めて使用し、現在ブログを作成しています。acts_as_commentableGitHub から使用する唯一の指示はモデルに追加することなので、Acts as commentable with threading を使用する方法に関するチュートリアルがあるかどうかを知りたいです。

  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
4

1 に答える 1

0

github の次のリンクにアクセスしましたか。

https://github.com/jackdempsey/acts_as_commentable

このコマンドを呼び出すと、移行を伴うフォームが自動的に作成されます。

レールgコメント

移行を実行するだけです。

使用しているRailsのバージョンは?

于 2013-07-11T15:08:39.493 に答える