Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I use RABL and extends/partials to generate a consistent JSON object across models
    primarykey
    data
    text
    <p>Our business requirement is that all JSON responses look something like this:</p> <pre><code>{ "status": "ok" "page": "1", "perPage: "20", "totalPages": "3", "totalRecords": "54", "records": [ { "id": 1234, "name": "Lorem ipsum } ] } </code></pre> <p>In other words, the meat of the response is the set of "records", but it needs to be wrapped with the status, paging attributes, etc. I'd like to use RABL to generate this. Is there a way to use extends or a partial to build this structure across all my objects in a DRY way?</p> <p>I'd love to do something like this:</p> <p><strong>app/views/widgets/index.rabl</strong></p> <pre><code>collection @widgets, :object_root =&gt; false attributes :id, :name extends "layouts/base" , :locals =&gt; { results: @widgets } </code></pre> <p><strong>app/views/layouts/base.rabl</strong></p> <pre><code>node( :status ) { 'ok' } node( :pages ) { @results.total_pages } </code></pre> <p><strong>app/controllers/widgets_controller.rb</strong></p> <pre><code>def index @widgets = Widget.paginate( page: params[:page] ) end </code></pre> <p>This does not work as-is.</p> <p>To clarify, I want to set a local as @results which I can then use inside the wrapper.</p> <p>Then, if I want to JSON-ify another object I could do something like:</p> <p><strong>app/views/fruits/index.rabl</strong></p> <pre><code>collection @fruits, :object_root =&gt; false attributes :id, :flavor extends "layouts/base" , :locals =&gt; { results: @fruits } </code></pre> <p><strong>app/controllers/fruits_controller.rb</strong></p> <pre><code>def index @fruits = Fruit.paginate( page: params[:page] ) end </code></pre> <p>And, not need to modify layouts/base, obviously.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload