为索引和显示共享 jsonbuilder 文件
sharing jsonbuilder files for index and show
假设我有一个名为 Customer 的模型。我的 index.json.jbuilder
看起来像这样
例如
json.array!(@customers) do |customer|
json.extract! customer, :id, :name, :phone_number, :email
# some more nested fields of customer
end
我的show.json.jbuilder
也有类似的重复代码
json.extract! customer, :id, :name, :phone_number, :email
# some more nested fields of customer
有没有更好的方法,比如说把show.json.jbuilder包含在index.json.jbuilder里面。我不想在两个地方重复呈现 json 的逻辑。
按照 jbuilder documentation.
中的描述使用部分 (partial!
)
假设我有一个名为 Customer 的模型。我的 index.json.jbuilder
看起来像这样
例如
json.array!(@customers) do |customer|
json.extract! customer, :id, :name, :phone_number, :email
# some more nested fields of customer
end
我的show.json.jbuilder
也有类似的重复代码
json.extract! customer, :id, :name, :phone_number, :email
# some more nested fields of customer
有没有更好的方法,比如说把show.json.jbuilder包含在index.json.jbuilder里面。我不想在两个地方重复呈现 json 的逻辑。
按照 jbuilder documentation.
中的描述使用部分 (partial!
)