jbuilder icon indicating copy to clipboard operation
jbuilder copied to clipboard

Does not work with Rails 7 api_only = true

Open noctivityinc opened this issue 3 years ago • 7 comments

Clean install of a new Rails 7 api_only app:

config.api_only = true

Added the jbuilder gem and generated a scaffold that has this line:

render json: @practice_tests, status: :ok

But the jbuilder files are not rendered - simply ignored. I believe it has to do with the views not being loaded in Rails 7 api only mode. Any thoughts on how to get this to work?

noctivityinc avatar Feb 24 '22 01:02 noctivityinc

I'm using Rails 7 in api_only mode as well and everything works fine.

As far as I see it, the problem here is render json: which basically just calls @practice_tests.as_json and doesn't render an actual view. It's the same as if calling

render json: {foo: 'bar'}

jBuilder views behave just like normal ones, so e.g. for an index action, it will search for a index.json.jbuilder template which you don't have to specify yourself. In this case

def index
  @practice_tests = PracticeTest.all
end

is sufficient.

I hope this helps a little.

stex avatar Mar 01 '22 18:03 stex

Got it. Thank you!

On Mar 1, 2022 at 1:07:23 PM, Stefan Exner @.***> wrote:

I'm using Rails 7 in api_only mode as well and everything works fine.

As far as I see it, the problem here is render json: which basically just calls @practice_tests.as_json and doesn't render an actual view. It's the same as if calling

render json: {foo: 'bar'}

jBuilder views behave just like normal ones, so e.g. for an index action, it will search for a index.json.jbuilder template which you don't have to specify yourself. In this case

def index @practice_tests = PracticeTest.allend

is sufficient.

I hope this helps a little.

— Reply to this email directly, view it on GitHub https://github.com/rails/jbuilder/issues/532#issuecomment-1055715205, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAATIARGJS7SLHPHE7CM27DU5ZMFXANCNFSM5PGA55TQ . You are receiving this because you authored the thread.Message ID: @.***>

noctivityinc avatar Mar 01 '22 22:03 noctivityinc

Rails 6 has the same issue.

What configuration does Rails need to enable native JBuilder for JSON?

juanca avatar Jun 01 '22 02:06 juanca

Rails 6 has the same issue.

What configuration does Rails need to enable native JBuilder for JSON?

I just finished an upgrade to 6.1.6.1 (from 5.2.8) and am trying to fix all of the numerous:

DEPRECATION WARNING: Rendering actions with '.' in the name is deprecated: <model_name>/index.json.jbuilder

Issues being thrown by rspec and I cannot seem to find an answer about this that works.

kkerley avatar Aug 02 '22 22:08 kkerley

Rails 6 has the same issue.

What configuration does Rails need to enable native JBuilder for JSON?

Sorry for the second reply, but for what it's worth, I got the DEPRECATION warning taken care of by changing my calls to

render '<model>/<action>.json.jbuilder

to:

render '<model>/<action>', formats: :json, handlers: :jbuilder

And you can even include , status: 200 or another HTML response code if desired. It's not automatic support, but it at least works and is no longer being handled via a deprecated approach.

kkerley avatar Aug 03 '22 01:08 kkerley

Rails 5 has the same issue -- but I am not sure how far back this gem is suppose to support.

juanca avatar Sep 21 '22 21:09 juanca

Rails 7 (without api_only = true) does not have the same issue. Works well out of the box when requesting an resource's .json

  • ruby 3.2.2
  • rails 7.1.1

juanca avatar Nov 12 '23 02:11 juanca