【发布时间】:2015-08-06 11:19:13
【问题描述】:
在使用 jbuilder 构建 JSON 响应时,我想为我的每个 Employee 对象添加一个属性“image_full_url”。
在我的 jbuilder 文件中,如下所示:
json.branch_companies @companies.select{ |branch| branch.head_company_id == head_company.id}.map{|branch| {
:branch => branch,
:employees => branch.employees.select(employee_attributes).each{ |emp| emp.image_full_url = "#{root_url[0..-2]}#{emp.photo_image.url}" },
:machine_categories => branch.machine_categories.pluck(:id, :name, :description)
}
}
在我的 Emplyoee 模型中,我有一个对应的 attr_accessor:
attr_accessor :image_full_url
向 Employee 对象添加属性在控制台上完美运行。
我有问题的部分在这里:
:employees => branch.employees.select(employee_attributes).each{ |emp| emp.image_full_url = "#{root_url[0..-2]}#{emp.photo_image.url}" }
我希望将“image_full_url”属性添加到 emp 对象中,但事实并非如此。 JSON 响应不包含“image_full_path”字段。我究竟做错了什么?有人可以帮忙吗?
【问题讨论】:
-
你得到什么 JSON 输出?
-
employees: [5] 0: { id: 3 name: "Vanessa" surname: "Woißett" phone_mobile: "+49 1721111111" phone_landline: "+49 711111113" fax: "+49 711111112" email: "vanessa@mail.com" department: "Disposition" company_id: 1 active: true }- 1: { id: 1 name: "Anton" surname: "Massner" phone_mobile: "+49 175655555" phone_landline: "+49 7116555551" fax: "+49 711655555552" email: "anton.massner@gmail.com" department: "Geschäftsführung" company_id: 1 active: true }
标签: ruby-on-rails ruby json jbuilder