使用 link/reference 字段更新内容条目

Updating Contentful entry with a link/reference field

Contentful 文档非常少。我想将 Link 条目上的字段更新为另一个条目("Reference" 是他们在 CMS UI 中的称呼)。

有谁知道在使用 Contentful Management API 时他们想要什么格式?

type  ||= @mgmt.content_types.find(
  ENV['CONTENTFUL_SPACE_ID'], 'comments'
)

entry = type.entries.create(
  title: params[:title],
  article: params[:article]
)

其中 :article 是字符串形式的条目 ID。这个returns:

undefined method `id' for "7L4IpEtsdffds8EsmoWMGIgy":String
Extracted source (around line #74):

#72         case field.type
#73         when ContentType::LINK then
*74           { sys: { type: field.type, linkType: field.link_type, id: attribute.id } } if attribute
#75         when ContentType::ARRAY then
#76           parse_fields_array(attribute)
#77         when ContentType::LOCATION then

我也尝试用 @client.entry(params[:article]) 替换 params[:article] 认为他们可能想要整个 object 但它 returns 相同的错误只是它看到相同的参数作为空字符串。

我也试过@DavidLitvak 的建议:

link = Contentful::Management::Link.new({
  sys: {
    type: 'Link',
    linkType: 'Entry',
    id: params[:article]
  }
})

entry = type.entries.create(
  title: params[:title],
  article: link
)

虽然这不会引发错误,但在填充标题字段时文章字段显示没有条目。

另请注意,我使用的是 Ruby gem: contentful-management.

您需要发送的是参考文章的Link

您可以这样做:

entry = type.entries.create(
  title: params[:title],
  article: Contentful::Management::Link.new({
    sys: {
      id: params[:article],
      type: 'Link',
      linkType: 'Entry'
    }
  })
)

您可以在此处找到有关链接工作原理的更多信息:https://www.contentful.com/developers/docs/concepts/links/