3

The Contentful documentation is pretty sparse. I want to update a field on an entry that is a Link to another entry ("Reference" is what they call it in the CMS UI).

Does anyone know what format they want this in when using the Contentful Management API?

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

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

Where :article is the entry ID as a string. This 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

I've also tried to replace params[:article] with @client.entry(params[:article]) thinking they may want the whole object but it returns the same error only it sees the same argument as an empty string.

I have also tried this upon @DavidLitvak's suggestion:

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

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

And although this does not throw an error, the article field shows up without an entry while the title field is populated.

Also note that I am using the Ruby gem: contentful-management.

JLF
  • 2,280
  • 4
  • 28
  • 45

1 Answers1

1

What you need to send is the Link for the referenced article.

You can do this like:

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

You can find more information on how Links work here: https://www.contentful.com/developers/docs/concepts/links/

David Litvak
  • 344
  • 1
  • 5
  • This successfully generates a link hash and passes the case this time, so no errors. But it still doesn't create a link in the CMS. The field is still empty. – JLF Aug 23 '17 at 22:57
  • Can you open a support request at https://support.contentful.com/hc/en-us. There we can better help you with your issue. – David Litvak Aug 25 '17 at 00:31
  • This does not work for me either. no error and the link does not show up in the cms – Dominik Goltermann Jun 04 '20 at 12:50