json - Rabl / Will Paginate setting name of root child -
i wonder if can help. i'm trying migrate our old rails model#to_json our api make versioning easier in longer term. i'm struggling @ first hurdle because of rabl , paginate.
we override willpaginate::collection#as_json
module willpaginate class collection alias :as_json_without_paginate :as_json def as_json(options = {}) # rabl seemingly passing json::ext::generator::state as_json unless options.is_a?(hash) options = {} end json = { :page => current_page :per_page => per_page, :total_entries => total_entries, :entries => as_json_without_paginate(options) } end end end
so @collection.to_json return like
{ "total_entries": 1, "page": 1, "entries": [ { "follow": { "id": 1, "name": "test" } } ], "per_page": 10 }
however when try following in rabl
node(:page) {|m| @follows.current_page } node(:per_page) {|m| @follows.per_page } node(:total_entries) {|m| @follows.total_entries} child(@follows => :entries) # extends 'follows/show' attributes :id, :name end
the name of child automatically set 'entry', want set 'follow' consistent our current api having no luck whatsoever.
{ "total_entries": 1, "page": 1, "entries": [ { "entry": { "id": 1, "name": "test" } } ], "per_page": 10 }
can give me pointers. i've been trying trace through source , seems name of root object name child implied.
i've encountered same exact bug, problem seems setting object_root false. here same solution proposed in response stackoverflow question: wrong child root name in rabl , can't set child root name
child( {@follow => :entries}, {:object_root => false} ) # extends 'follows/show' attributes :id, :name end
note both round parentheses "()" , braces "{}".
hope helps. worked in case.
Comments
Post a Comment