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

Jbuilder renders blank body with rails 5.0 #346

Open
gsmetal opened this issue Aug 1, 2016 · 16 comments
Open

Jbuilder renders blank body with rails 5.0 #346

gsmetal opened this issue Aug 1, 2016 · 16 comments
Labels

Comments

@gsmetal
Copy link

gsmetal commented Aug 1, 2016

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
    }
}
@gsmetal gsmetal changed the title Jbuilders renders blank body with rails 5.0 Jbuilder renders blank body with rails 5.0 Aug 1, 2016
@prathamesh-sonpatki
Copy link
Member

I think it might be related to rails/rails#19377 which is the default behavior of Rails 5 to return with 204 no content if no response is specified.

@gsmetal
Copy link
Author

gsmetal commented Aug 1, 2016

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.

@realwol
Copy link

realwol commented Oct 9, 2016

same problem in 2.6, log said Completed 204 No Content, any update?

@sirwolfgang
Copy link

sirwolfgang commented Nov 17, 2016

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.

@charleyw
Copy link

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

@yorkxin
Copy link

yorkxin commented Dec 9, 2016

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.

@sirwolfgang
Copy link

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

@freefishz
Copy link

It can be solved by adding include ActionView::Rendering as following:

class ApplicationController < ActionController::API
  include ActionView::Rendering
end

@ghost
Copy link

ghost commented Mar 16, 2017

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

@realwol
Copy link

realwol commented Mar 16, 2017

@BOOOMKKK Hi, do you have jbuilder in you gemfile?

@ghost
Copy link

ghost commented Mar 16, 2017

@realwol Thanks, I added in and it works!

@gsmetal
Copy link
Author

gsmetal commented Apr 20, 2018

Updated to rails 5.2 and this bug is still reproduced. And @freefishz workaround still works.

@jwoertink
Copy link

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.

@freefishz
Copy link

freefishz commented Apr 25, 2018

@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

@thisismydesign
Copy link

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.

@ProtonPunk
Copy link

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests