Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to use a layout with yield without parsing and encoding two times #172

Closed
alex88 opened this issue Mar 13, 2014 · 9 comments

Comments

Projects
None yet
7 participants
@alex88
Copy link

commented Mar 13, 2014

Actually to use a json template it's needed to JSON.parse(yield) which needs a re-parse of the encoded content and a re-encoding to the final json.

It would be nice to be able to skip this step to improve performances.

@rwz

This comment has been minimized.

Copy link
Collaborator

commented Apr 12, 2014

@alex88 you can check out the implementation from @hasimo in #185. It's not gonna make it in master, but you can try using it.

Alternative solutions would be to explicitly call layout as a partial from view itself, or wrap it as a view helper.

@rwz rwz closed this Apr 12, 2014

@alex88

This comment has been minimized.

Copy link
Author

commented Apr 13, 2014

Thanks @rwz I think I'll do that way!

@paulwalker

This comment has been minimized.

Copy link

commented May 29, 2014

So, as things stand now, there are no plans to allow for a standard efficient use of layouts with jbuilder?

@rwz

This comment has been minimized.

Copy link
Collaborator

commented May 30, 2014

@paulwalker you're right.

@zeeraw

This comment has been minimized.

Copy link

commented May 30, 2014

@rwz What a shame... I guess I'll have to keep that json.errors line in every view. Parsing and rebuilding the JSON string just wont cut it.

json.user { json.partial! "api/v1/user", record: @user }
json.errors { json.array!(errors) } if errors.any?
@jplethier

This comment has been minimized.

Copy link

commented Apr 27, 2016

I need to use a layout just like this, is there any news or plans to implement a way to not need to re-parse and re-encoding json again?

@amw

This comment has been minimized.

Copy link

commented May 27, 2016

I'm not sure why the opposition to include functionality like this. I think wrapping response data in a shared metadata structure is commonplace in API design. See data, errors or meta keys in JSON-API specification: http://jsonapi.org/format/.

Not a very pretty solution, but I managed to create my general response layout using json.erb and just pasting literal results of yield in a proper position. Much better than doing an extra round of JSON.parse & JSON.generate.

app/controllers/api/base_controller.rb:

class Api::BaseController < ActionController::Base
  layout "api.v1"
end

app/views/layouts/api.v1.json.erb:

{
<% if @api_errors.present? %>
  "errors": <%= raw JSON.dump @api_errors %>,
<% else %>
  "data": <%= yield %>,
<% end %>
  "meta": <%= raw JSON.dump @api_meta %>
}

Edited to add raw in ERB code.

@paulwalker

This comment has been minimized.

Copy link

commented May 28, 2016

It's pretty remarkable actually that Rails 5 (with all it's special emphasis on building JSON APIs) would launch without the ability to easily render the JSON-API standard, of which @wycats himself is a primary editor. All because dhh doesn't believe in metadata in api responses?

@Richard-Degenne

This comment has been minimized.

Copy link

commented Jun 21, 2019

Is there any news for this feature?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.