我正在使用 Ruby on Rails 4 和 RABL gem 。我想通过从 index.json.rabl
渲染 show.json.rabl
View 来重用模板,并保持 jsonapi.org 中规定的约定。 (来源:RABL documentation)。
也就是说,我有以下文件:
# index.json.rabl
collection @articles => :articles
attributes :id, :title, :...
# show.json.rabl
collection [@article] => :articles
attributes :id, :title, :...
由于对于每个 article
实例,index.json.rabl
呈现与 show.json.rabl
相同的属性,我希望重用后者作为部分模板,或者(也许)扩展第一个。
我想输出的是:
# index JSON output
{"articles":[{"id":1,"title":"Sample 1","...":...},{"id":2,"title":"Sample 2","...":...},{"id":3,"title":"Sample 3","...":...}]}
# show JSON output
{"articles":[{"id":1,"title":"Sample 1","...":...}]}
最佳答案
这是一个巧妙的方法:
事物/base.rabl:
attributes :id, :title
child(:another_child, :object_root => false) { attributes :id, :title }
事物/show.rabl:
object @thing
extends 'things/base'
事物/index.rabl:
collection @things
extends 'things/base'
关于ruby-on-rails - 如何通过保持约定来重用 RABL 部分模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23839540/