In my rails 3.2 app, I'm using jbuilder to render responses from my JSON api.
I want to provide a common structure to all API responses, and a layout would be the likely solution to keep my views DRY.
ex: I'd like every response to be of the following form :
{
status: "ok|error|redirect",
data: { ... JSON specific to the current view ... },
errors: [ ... ],
notes: [ ... ]
}
(where the value for data is a json structure provided by the view, everything else is from the layout)
However: I can't get the jbuilder layout yielding to the view correctly.
# in layout
json.data yield
# in view
json.some "value"
results in:
{"data":"{\"some\":\"value\"}"} # arg! my json has become a string
Trying things another way:
# in layout
yield
# in view
json.data do |json|
json.some "value"
end
results in :
{}
Has anyone had success doing this with jbuilder, or another json templating gem/method?
This juilder github issue suggests it's possible, but indicates others are having similar issues.
I see rabl (https://github.com/nesquena/rabl/) is supposed to support layouts (https://github.com/nesquena/rabl/wiki/Using-Layouts), but I've decided not to use it for other reasons (rabl makes complex json structures a nightmare, particularly when trying to control object roots etc).
Late answer, but helped me get what I was looking for...
Success Result:
Error Result:
Code:
app/controllers/api/v1/base_controller.rb
app/controllers/api/v1/some_controller.rb
app/views/layouts/api/v1/application.json.jbuilder
app/views/api/v1/something/index.json.jbuilder
You can do this by this way
jbuilder is pretty simple technique for API views here you can add partials so if you want the same response for all the API create a decorator or create partial for the common response and call that response where ever you need that
Lets say if you want
create a partial for this /views/api/common/_some_partial
Its pretty much simple solution for your question.
Cheers
In case you don't want to include extra key you can do so
In /app/views/layouts/json_layout.json.jbuilder
I'll give you an alternative based on a solution we came up with:
then, in the view, you can call envelope and include your json code like:
Try
https://github.com/rails/jbuilder/issues/8#issuecomment-27586784