【问题标题】:Include comments with post in rails在 Rails 中包含带有帖子的评论
【发布时间】:2019-06-13 12:49:27
【问题描述】:

我在几个页面上构建了一个带有 vue 组件的 rails 应用程序。我将数据传递给 vue 组件,如下所示:

<v-my-component :posts="<%= @post.to_json %>"></v-my-component>

这很好用,我的帖子作为 json 作为道具传递给我的组件。但是,如果我还想包含 post cmets,我该怎么做?我想要类似的东西:

#posts/show
<v-my-component :posts="<%= @post.wtih(:comment).to_json %>"></v-my-component>

我的 json 应该类似于:

post
    title:"",
    body:"",
    ...,
    comments:[
        {
            id:1,
            comment_body:"",
            ...
        },
        ...
    ]

有谁知道如何做到这一点?提前致谢

【问题讨论】:

    标签: ruby-on-rails json vue.js


    【解决方案1】:

    您需要将选项传递给to_json

    <v-my-component :posts="<%= @post.to_json(include: :comments) %>"></v-my-component>
    

    【讨论】:

      猜你喜欢
      • 2023-04-10
      • 1970-01-01
      • 2014-06-11
      • 1970-01-01
      • 1970-01-01
      • 2023-03-04
      • 2014-10-13
      • 1970-01-01
      • 2021-10-02
      相关资源
      最近更新 更多