我得到了以下代码:
class TasksController < ApplicationController
before_action :authenticate_user!
def index
tasks = current_user.tasks.all.page(params[:page] || 1)
tasks.each do |task|
authorize!(:read, task)
task.merge(total_duration_of_time_entries => task.total_duration_of_time_entries)
end
render_data(tasks)
end
字符串
如何向任务数组中添加额外的字段?我正在尝试添加total_duration_of_time_entries
,这是我的模型中的一个方法。
2条答案
按热度按时间jei2mxaa1#
如果是“标准”rails序列化,则可以覆盖
Task
模型中的as_json
方法字符串
但是使用一些序列化器(例如blueprinter)或JSON模板生成器(例如jbuilder)
kd3sttzy2#
最后是这样做的:
字符串
希望它能在未来帮助到某人。