如何遍历“ application / ld + json”中的记录

问题描述 投票:0回答:1

我正在尝试创建一个[ItemList]作为schema.org结构化数据标记的一部分。对于itemList,我无法遍历@posts以使用:plain:cdata以编程方式创建列表。

- content_for(:header) do
  %script{:type => "application/ld+json"}
    :plain
      {
      "@context": "https://schema.org",
      "@graph":
      [
        {
          "@context": "http://schema.org",
          "@type": "ItemList",
          "itemListElement":
          [
            - @posts.each do  |post|
              {
                "@type": "ListItem",
                "position": 1,
                "item":
                  {
                  "@id": "https://example.com/post",
                  "name": "#{post.title}"
                  }
              }
          ]
        }
      ]
    }

是否有人成功完成此操作,或者可以建议仍然填充itemList的替代方法?

ruby-on-rails ruby haml json-ld
1个回答
1
投票

[在视图外部,为您的JSON数据创建常规的Ruby哈希。

然后,您的视图将看起来像

%script{ :type => "application/ld+json" }
  != my_ruby_hash.to_json
© www.soinside.com 2019 - 2024. All rights reserved.