jbuilder
jbuilder copied to clipboard
Jbuilder renders blank body with rails 5.0
I have latest jbuilder (2.6.0) and latest rails (5.0.0). API-controller hello:
class HelloController < ActionController::API
def hello
@hello = {a: 1, b: 2}
end
end
routes:
get '/hello', to: 'hello#hello', format: 'json'
When there is no folder views/hello
it renders 204 No Content
.
When I add this folder and hello.json.jbuilder
inside like this:
json.hello @hello
it renders 200 OK
with empty body.
With jbuilder (2.4.1) in same situation it renders as expected:
{
"hello": {
"a": 1,
"b": 2
}
}
I think it might be related to https://github.com/rails/rails/pull/19377 which is the default behavior of Rails 5 to return with 204 no content
if no response is specified.
There is no problem with 204 with missing template (espesially it logs No template found for HelloController#hello, rendering head :no_content
in this case), there is problem with blank body when there is a template.
same problem in 2.6, log said Completed 204 No Content, any update?
I seem to be getting the same, or very similar issue. My controller is sending back a response with the expected status code of 200
(:ok
), but my body is empty. The jbuilder template files are not being hit at all, but if I use the json: @instance
to by pass it I get a valid serialized response.
Only changing the version of jbuilder does not seem to impact anything. This was a rails 4 to rails 5 project non api project. And it worked pre-upgrade to rails5/jbuilder
If I force it with render 'model/action.json.jbuilder'
it still does nothing, but if I point it to a different template path that doesn't exist. It will fail saying not found.
I got the same problem with: Rails 5.0.0 Api mode
when i include some gem (eg. devise, wechat),the controller will return 200 and no content, it even not hit the jbuilder template.
And I checked the gem(wechat) which will cause the problem, if i comment these lines out in the gem https://github.com/Eric-Guo/wechat/blob/master/lib/action_controller/wechat_responder.rb.
if defined? Base
class << Base
include WechatResponder
end
end
if defined? API
class << API
include WechatResponder
end
end
jbuilder will work again
I got the same issue too. But I also found that, if there is a gem makes ActionController::Base to include it's own modules upon initialization (e.g. bugsnag), then jBuilder's hack won't be run, and it will send blank body.
My project currently uses a gem that does the including via ::ActionController::Base.send(:include,
method without issue. The issue in my case that rspec does not render the views in controller tests by default.
RSpec.configure do |config|
config.render_views = true
end
It can be solved by adding include ActionView::Rendering
as following:
class ApplicationController < ActionController::API
include ActionView::Rendering
end
Hi, I am new to rails and currently developing my first app, so I am not sure what went wrong. I am using rails 5.0.1 and following steps from https://richonrails.com/articles/getting-started-with-jbuilder.
Here`s what I found: if my app is created without --api tag, then jbuilder works fine. However, when I create an api-only app, I get 204 No content response, with or without "freefishz"'s step. Let me know if you need any information. Thanks
@BOOOMKKK Hi, do you have jbuilder in you gemfile?
@realwol Thanks, I added in and it works!
Updated to rails 5.2 and this bug is still reproduced. And @freefishz workaround still works.
I'm getting this issue in latest 5.1, but @freefishz solution doesn't work for me. I include that rendering, and still get an empty response body. I have to manually render json or use a serializer in place of jbuilder.
@jeremy with Rails 5.1, try this:
class ApplicationController < ActionController::API
include ActionView::Rendering
def render_to_body(options)
_render_to_body_with_renderer(options) || super
end
end
referenced from https://github.com/Eric-Guo/wechat/issues/165
Had the same issue with Rails 6. @freefishz's solution did not work, I had to add jbuilder
to the Gemfile.
I have no idea if that should've been obvious or anything but why isn't this included in the documentation? Virtually every gem documentation starts with this instruction. Omitting it here makes it seem this is already part of Rails and should work out of the box. I'm not sure if that is the case and this is a bug or that's not the case but either way, it would be great to include it in the readme.
@thisismydesign @realwol
As note if it's in the .gemspec of your project it will not work (204), but it will work if it's in the Gemfile.