Comments (7)
I guess the root of my issue is that you cannot use partial
unless you're inside of node
or code
. Why is that?
Workaround #1:
The following did not work.
# index.json.rabl
object false
child(@people => :entries) do
...
end
node(:pagination) do
partial "shared/pagination", :object => @people
end
and the shared/pagination
partial looks like this:
# shared/pagination.json.rabl
{:count => @people.count}
{"pagination":[{}],"entries":[{"entry":{...}}]}
Ok, so now onto workaround #2.
# index.json.rabl
object false
child(@people => :entries) do
...
end
partial "shared/pagination", :object => @people
# shared/pagination.json.rabl
node(:pagination) do
{:count => @people.count}
end
The resulting JSON does not have a pagination node at all:
{"entries":[{"entry":{...}, ...}]}
The only way I have gotten this to work is by including the partial inside of a node
.
# index.json.rabl
object false
child(@people => :entries) do
...
end
node(:pagination) do
partial "shared/pagination", :object => @people
end
# shared/pagination.json.rabl
node(:pagination) do
{:count => @people.count}
end
But, as you would expect, then I have 2 pagination nodes in the resulting JSON:
{"pagination":[{"pagination":{"count":1}}],"entries":[{"entry":{...},...}]}
So my question is, how can I use partials so that I will only have a single pagination node?
Thanks for your help!
from rabl.
I readily admit the "partial" business is confusing. It evolved out of my own need to render a hash inside of a "node" block. It only works in a node block because that's all I set it up to do at the time.
I feel like given
# shared/pagination.json.rabl
node(:pagination) do
{:count => @people.count}
end
you should be able to achieve what you want with extends:
# index.json.rabl
object false
extends "shared/pagination"
child(@people => :entries) do
...
end
because extends does what you want, it "mixes in" the other template into the parent template and adds all the nodes and attributes. you can "extend" from multiple documents if needed as well. I would look at it very simply:
If you are inside a node and need a hash representation of a record, use partial to get that
In other cases or if you need to "mixin" behavior to a document, use extends to do that
Hope that helps a bit, this should all be clarified on the wiki, I haven't gotten around to that yet.
from rabl.
Thanks for all of your help nesquena.
When I extend shared/pagination, I'm still not seeing my pagination node in the response however.
# index.json.rabl
object false
extends 'shared/pagination'
child(@people => :entries) do
...
end
# shared/pagination.json.rabl
node(:pagination) do
{:this => "that"}
end
The response I see does not have the pagination node in it. Just the entries.
{"entries":[{"entry":{...},...}]}
from rabl.
I think I may have figured it out, if you look into the entries themselves, I suspect you will see the pagination node as part of each entry item. extends is for adding to the representation of an item, so I can see why this is not the behavior you are looking for either. You want to append additional items to the root (in the object false) case that are not part of the item represenation.
I think this is a legitimate limitation in the sharing system of RABL. In that to append to the root node, you need to build the nodes inside the template and it becomes hard to share code in this case. I can't think of a solution off hand for reusing the root nodes from another template except the sloppy:
# index.json.rabl
object false
child(@people => :entries) do
...
end
node(:pagination) do
partial("shared/pagination", :object => @people)[:pagination]
# simply traverses into the pagination hash to skip the duplicate node
end
I can think of a few ways to fix this (allow partials at root, allow you to send :root => false to partial as an option, etc) and I will leave this ticket open as a reminder to fix this when I can.
from rabl.
I had a similar problem and ended up solving it using the ability of node to not take any arguments (causing anything defined in the block to be merged with the parent). My problem was slightly different. I have two models (I've substituted users and projects here, where users have many projects), and I want to embed the JSON of each users' project in the user's JSON:
-
app/views/users/show.json.rabl:
object @user attributes :email, :name child :projects do node do |project| partial('project/_show', :object => project) end end
-
app/views/projects/show.json.rabl:
object false node(:project) { partial('project/_show', :object => @project) }
-
app/views/projects/_show.json.rabl:
attributes :name, :due_date, :notes
The general gist of this approach is to wrap the partial call in a node call with no parameter (other than the block) This will cause the partial to render directly into the scope containing the node. I think that should do what you want.
from rabl.
I have the same issue as @gammons . I need to call a shared/pagination partial from several different index views to keep things dry.
from rabl.
having the same issue, then I've found this: http://metabates.com/2012/02/22/adding-pagination-to-an-api/. He suggests putting meta data in the headers instead. I think it's a cleaner solution for pagination.
from rabl.
Related Issues (20)
- Drop riot? Unmaintained, broken on Ruby 2.5. HOT 2
- How to use rabl with swagger? HOT 2
- Tagging a new version, or use master branch? HOT 1
- Unable to properly render json for an simple array of objects HOT 1
- How do I render free form metadata HOT 1
- Migrating to Rails 5 - Rabl returning Active Record object
- Rspec renders rabl as string instead of json
- Not able to get the rendered result in Rails console HOT 1
- Rabl output values as keys
- Rabl partial lookup based on requested api namespace
- upgrading from 0.14.0 to 0.14.1 is a breaking change HOT 8
- Child removes node from response when the provided object is an empty array HOT 2
- Rabl not renders collection properly HOT 4
- Is nested map valid in RABL?
- N+1 issues HOT 1
- some problems to install rable on windows HOT 2
- Avoid playing with `Oj` shared `default_options` HOT 2
- rabl tests failing with rr 3.x and Ruby 3 HOT 12
- Rails 7 compatibility HOT 1
- access controller instance variables in representers
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rabl.