我想解決的問題
我正在使用 Nuxt 和 Rails 構建服務,但由于 Nuxt 中使用的格式有限,Rails 的時間戳出現錯誤。有什么辦法可以改變 Rails 中 created_at 和 updated_at 的格式?具體來說,我希望它看起來像 YYYY-MM-DD hh:mm。我正在洗掉 created_at 和 updated_at 作為緊急措施。
代碼
控制器
class Api::V1::SchedulesController < ApplicationController
def index
schedules = Schedule.all
render json:schedules, status: :ok
end
uj5u.com熱心網友回復:
使用jbuilder
在控制器中:
def index
@schedules = Schedule.all
end
鑒于:
# app/views/api/v1/schedules/index.json.jbuilder
json.array! @schedules do |schedule|
json.id schedule.id
json.created_at schedule.strftime('%Y-%m-%d %H:%M')
json.updated_at schedule.strftime('%Y-%m-%d %H:%M')
# define other fields here...
# ....
# ....
end
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/430943.html
標籤:轨道上的红宝石
