2016-03-20 35 views
0

轨道4 +葡萄宝石+格式的日期和日期JSON响应轨道4 +葡萄宝石+格式的日期和日期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宝石显示格式的日期,但无法产生输出。 尝试使用 -

DateFormat.change_to(日期, “ONLY_DATE”)

回答

2
require 'date_format' 

尝试包括它的lib/API/V1/item.rb的