我正在尝试创建一个[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的替代方法?
[在视图外部,为您的JSON数据创建常规的Ruby哈希。
然后,您的视图将看起来像
%script{ :type => "application/ld+json" }
!= my_ruby_hash.to_json