简体   繁体   English

在 Rails 中循环结构化数据

[英]Loop in structured data in rails

I am trying to use structured data in rails, but it is not working properly.我正在尝试在 rails 中使用结构化数据,但它无法正常工作。 Below is my code:下面是我的代码:

<script type="application/ld+json">
<%=
{
  "@context": "http://schema.org",
  "@type": "FAQPage",
  "mainEntity": @faq[:lists].each do |item|
    item[:list].map do |list|
      {
        "@type": "Question",
        "name": list[:heading],
        "acceptedAnswer": {
          "@type": "Answer",
          "text": list[:desc]
        }
      }
    end
  end
}.to_json.html_safe
%>
</script>

What I am doing?我在做什么?

If you want mainEntity to be a simple array (not an array of arrays), you can try this:如果您希望mainEntity是一个简单的数组(不是数组数组),您可以试试这个:

<script type="application/ld+json">
<%=
{
  "@context": "http://schema.org",
  "@type": "FAQPage",
  "mainEntity": @faq[:lists].map do |item|
    item[:list].map do |list|
      {
        "@type": "Question",
        "name": list[:heading],
        "acceptedAnswer": {
          "@type": "Answer",
          "text": list[:desc]
        }
      }
    end
  end.flatten # <===== array[array] --> array
}.to_json.html_safe
%>
</script>

See: https://apidock.com/ruby/Array/flatten参见: https://apidock.com/ruby/Array/flatten

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM