【发布时间】: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