【问题标题】:How do I set date format for Contentful field in Ruby?如何在 Ruby 中为 Contentful 字段设置日期格式?
【发布时间】:2020-01-16 23:00:01
【问题描述】:

以下是我编写的内容迁移,用于在 Contentful 中创建一个名为“Trip”的内容模型。我想做的是指定“开始日期”和“结束日期”字段的格式。 Contentful 为您提供了三个可以在 UI 中设置的格式选项:

  1. 仅限日期
  2. 没有时区的日期和时间
  3. 带有时区的日期和时间

没有在我的迁移文件中指定格式,我默认得到格式#3,我需要格式#1。有谁熟悉如何做到这一点?

谢谢!

class CreateTrip < RevertableMigration

  self.content_type_id = 'trip'

  def up
    with_space do |space|

      # Create content model
      content_type = space.content_types.create(
        name: 'Trip',
        id: content_type_id,
        description: 'Content model for trip cards'
      )

      # Set validation
      validation_for_country = Contentful::Management::Validation.new
      validation_for_country.in = ['Bolivia','Haiti','India','Nicaragua', 'Puerto Rico', 'South Africa']

      content_type.fields.create(id: 'image', name: 'Image', type: 'Link', link_type: 'Asset', required: true)
      content_type.fields.create(id: 'country', name: 'Country', type: 'Symbol', required: true,  validations: [validation_for_country])
      content_type.fields.create(id: 'trip_details', name: 'Trip Details', type: 'Symbol')
      content_type.fields.create(id: 'start_date', name: 'Start Date', type: 'Date', required: true)
      content_type.fields.create(id: 'end_date', name: 'End Date', type: 'Date', required: true)
      content_type.fields.create(id: 'trip_description', name: 'Trip Description', type: 'Text')
      content_type.fields.create(id: 'link_url', name: 'Link URL', type: 'Symbol', required: true)

      # Publish
      content_type.save
      content_type.publish

      # Editor interface config
      editor_interface = content_type.editor_interface.default
      controls = editor_interface.controls
      field = controls.detect { |e| e['fieldId'] == 'trip_details' }
      field['settings'] = { 'helpText' => 'City, month, participant type, etc.' }
      editor_interface.update(controls: controls)
      editor_interface.reload

      content_type.save
      content_type.publish
    end
  end
end

【问题讨论】:

    标签: ruby migration contentful


    【解决方案1】:

    当我通过 Contentful CLI 使用 contentful export 命令导出我的内容类型时,我可以在我的 JSON 中看到类似的内容:

            {
              "fieldId": "endDate",
              "settings": {
                "ampm": "24",
                "format": "timeZ",
                "helpText": "(Optional) The date and time when the event ends..."
              },
              "widgetId": "datePicker"
            },
            {
              "fieldId": "internalTitle",
              "widgetId": "singleLine"
            },
            {
              "fieldId": "startDate",
              "settings": {
                "ampm": "24",
                "format": "timeZ",
                "helpText": "The date/time when this schedule starts..."
              },
              "widgetId": "datePicker"
            }
    

    现在,我不使用 Ruby 迁移工具,但这让我相信您可以设置 field['widgetId'] = 'datePicker'

    field['settings'] = {
      'format' => 'dateonly',
      'helpText' => ...
    }
    

    如果有帮助,请告诉我!

    【讨论】:

    • 谢谢,戈登!我会试一试并报告它的成功。
    • 所以我遇到了另一种情况,我必须以这种方式格式化日期并尝试了您上面提到的方法。有效!我认为唯一没有必要的是指定field['widgetId'] = 'datePicker',因为在创建字段时已经声明了这一点。我所希望的是,通过完全按照您所说的去做,我可以为与此特定内容模型关联的所有日期字段指定“仅日期”。不幸的是,似乎我必须为每个字段单独指定它。无论如何,感谢您的帮助!
    • 太棒了!请将答案标记为正确,以便其他人更容易找到。
    猜你喜欢
    • 1970-01-01
    • 2012-05-24
    • 2020-06-17
    • 2011-02-27
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 2017-12-12
    相关资源
    最近更新 更多