Rails和Mutli嵌套中的祖先宝石 [英] Ancestry gem in Rails and Mutli Nesting

查看:100
本文介绍了Rails和Mutli嵌套中的祖先宝石的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用rails中的祖先宝石来嵌套一些注释,而我想要的是让您能够获取所有注释,然后将它们全部嵌套。当我输入以下内容时,如何获得以下内容: @comments = post.comments.arrange_serializable 进入我的注释控制器索引操作并获得以下结果:

I am using the ancestry gem in rails to nest some comments, and what I wanted was for you to be able to get all comments and then have them all nested. How ever I get the following when I put: @comments = post.comments.arrange_serializable into my comments controller index action and get the following result:

{
   "comments":[
      {
         "id":3,
         "comment":"284723nbrkdgfiy2r84ygwbdjhfg8426trgfewuhjf",
         "author":"asdasdasdas",
         "post_id":268,
         "ancestry":null,
         "created_at":"2014-06-17T19:23:04.667Z",
         "updated_at":"2014-06-17T19:23:04.667Z",
         "children":[
            {
               "id":4,
               "comment":"284723nbrkdgfiy2r84ygwbdjhfg8426trgfewuhjf",
               "author":"asdasdasdas",
               "post_id":268,
               "ancestry":"3",
               "created_at":"2014-06-17T19:24:02.408Z",
               "updated_at":"2014-06-17T19:24:02.408Z",
               "children":[

               ]
            }
         ]
      },
      {
         "id":5,
         "comment":"97ryhewfkhbdasifyt834rygewbfj,dhsg834",
         "author":"asdasdasd",
         "post_id":268,
         "ancestry":"4",
         "created_at":"2014-06-17T20:30:04.887Z",
         "updated_at":"2014-06-17T20:38:16.060Z",
         "children":[

         ]
      }
   ]
}

很明显,带有 id:5 假设位于孩子的数组中,该数组位于注释 id:4 其中, IS 嵌套在注释中,并带有 id:3

It's very apparent that comment with id: 5 is suppose to be in the array of children which sits in comment id: 4 which IS nested under comment with id: 3.

有人可以告诉我为什么 arrange_serializable 不多嵌套注释吗?

Can some one one tell me why arrange_serializable does not "multi nest" comments? or if there is another function to do this with.

推荐答案

结构

您的 arrange_serializable 似乎有效-我认为问题在于您如何嵌套评论

Your arrange_serializable seems to be working - I think the problem is with how you're nesting the comments

我们发现(花了很长时间),如果要使用嵌套类别,则需要使用斜杠,如下所示:

We found out (took us ages) that if you want to use "nested" categories, you need to use a slash like this:

因此,如果您要深层嵌套,则需要确保包括到根对象的整个路径。通用逻辑建议从嵌套对象继承也将允许它们嵌套-不是。

So if you're trying to "deep nest", you need to ensure you're including the whole route to the root object. Common logic would suggest "inheriting" from nested objects would also allow them to be nested - not so.

-

修复

为您的 id 5 ,则应将祖先列设为该值:

For your id 5, you should make the ancestry column this value:

$ rails c
$ comment = Comment.find 5
$ comment.update(ancestry: "3/4")

-

部分

如果您想在视图中显示类别的嵌套数组,我们使用以下代码:

If you wanted to show a nested array of categories in your view, we use the following code:

#app/views/elements/_category.html.erb
<!-- Categories -->
<ol class="categories">
    <% collection.arrange.each do |category, sub_item| %>
        <li>
            <!-- Category -->
            <%= category.title %>

            <!-- Children -->
            <% if category.has_children? %>
                <%= render partial: "category", locals: { collection: category.children } %>
            <% end %>

        </li>
    <% end %>
</ol>


#app/views/application/index.html.erb
<%= render partial: "category", locals: { collection: Category.all } %>

这篇关于Rails和Mutli嵌套中的祖先宝石的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆