【问题标题】:Rails 4 + Grape Gem + Formatted date and date in JSON responseRails 4 + Grape Gem + JSON 响应中的格式化日期和日期
【发布时间】:2016-03-20 18:22:11
【问题描述】:

Rails 4 + Grape Gem + JSON 响应中的格式化日期和日期

在我的葡萄响应中 - 我试图在 JSON 响应中查找格式化日期,但无法找到格式化日期。

模型 - 项目

class Item < ActiveRecord::Base
  def self.list_view
    select("items.id as id, items.availability_date as date, items.name as name, items.description")
     .order("id ASC").as_json( except: [ :id, :created_at, :updated_at ])
  end
end

API 调用

lib/api/v1/item.rb

module API
  module V1
    class Items < Grape::API
      version 'v1'
      format :json
      resource :item_data do
        get do
          Item.list_view
        end
      end
    end
  end
end

请求

http://localhost:3000/api/v1/item_data

回应

[
  {
    "description": "Product One Description",
    "date": "2016-03-19",
    "name": "Product One"
  },
  {
    "description": "Product 2 Desc",
    "date": "2016-03-20",
    "name": "Product 2"
  }
]

这里我使用date_format gem 来显示格式化日期,但无法生成输出。 尝试使用 -

DateFormat.change_to(date, "ONLY_DATE")

【问题讨论】:

    标签: json date ruby-on-rails-4 grape-api


    【解决方案1】:
    require 'date_format'
    

    尝试在 lib/api/v1/item.rb 中包含它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-10
      • 1970-01-01
      相关资源
      最近更新 更多