【问题标题】:Updating Contentful entry with a link/reference field使用链接/参考字段更新内容条目
【发布时间】:2018-02-01 15:12:45
【问题描述】:

Contentful 文档非常少。我想更新一个条目上的一个字段,该字段是指向另一个条目的链接(“参考”是他们在 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。这返回:

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

我还尝试将params[:article] 替换为@client.entry(params[:article]),认为他们可能想要整个对象,但它返回相同的错误,只是它将相同的参数视为空字符串。

我也根据@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

【问题讨论】:

    标签: ruby contentful


    【解决方案1】:

    您需要发送的是参考文章的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/

    【讨论】:

    • 这样就成功生成了一个link hash,这次通过了case,所以没有报错。但它仍然没有在 CMS 中创建链接。该字段仍为空。
    • 您能否在support.contentful.com/hc/en-us 提出支持请求。在那里,我们可以更好地帮助您解决问题。
    • 这对我也不起作用。没有错误,并且链接没有显示在 cms 中
    猜你喜欢
    • 2017-11-07
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 2017-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多