1

問題の宝石は: https://github.com/rails/acts_as_tree

アプリでコメントをネストして出力するためのアプローチとして、acts_as_tree を使用しました。_comment.html.erb の次のコードは正常に動作します。

<div id="comment_<%= comment.id %>">
  <%= comment.body %> => <%= comment.children.length %> replies
  <%= render :partial => 'comments/comment', :collection => comment.children %>
</div>

私はcomment.children部分が機能していることを知っています。ただし、Ember を Rails アプリのフロント エンドとして使用しようとしていますが、これを Ember アプリケーションに浸透させる方法がわかりません。

私のコメントモデル(レール内)は次のとおりです。

class Comment < ActiveRecord::Base
    extend ActsAsTree::TreeView

    acts_as_tree :order => 'created_at'
    belongs_to :post
    belongs_to :user

    def commented_by
        self.user.username
    end
end

私のコメントシリアライザーは次のとおりです。

class CommentSerializer < ActiveModel::Serializer
    embed :ids, include: true

  attributes :id, :parent_id, :body, :created_at, :updated_at, :post_id, :user_id, :commented_by

  belongs_to :post
end

私の Ember コメント モデルは次のとおりです。

App.Comment = DS.Model.extend({
    parentID: DS.attr('number'),
    body: DS.attr('string'),

    createdAt: DS.attr('date'),
    updatedAt: DS.attr('date'),

    postID: DS.attr('number'),
    userID: DS.attr('number'),

    commentedBy: DS.attr('string'),

    post: DS.belongsTo('post', {async: true}),
    // parent: DS.belongsTo('comment', {async: true}),
    // children: DS.hasMany('comment', {async: true})
});

私の質問は少し広いと思いますが、自己参照関係を調べてきましたが、acts_as_tree 自体がどのように機能しているかよくわからないため、これらのソリューションの実装に成功していません。一度に多くのことをしようとしているのかもしれません。これが私が大いに助けてくれるものです:

Ember が読み取れるように、acts_as_tree 機能を正しくシリアル化するにはどうすればよいですか?

4

0 に答える 0