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

Does not work with Rails 7 api_only = true #532

Open
noctivityinc opened this issue Feb 24, 2022 · 7 comments
Open

Does not work with Rails 7 api_only = true #532

noctivityinc opened this issue Feb 24, 2022 · 7 comments

Comments

@noctivityinc
Copy link

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?

@stex
Copy link

stex commented Mar 1, 2022

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.

@noctivityinc
Copy link
Author

noctivityinc commented Mar 1, 2022 via email

@juanca
Copy link

juanca commented Jun 1, 2022

Rails 6 has the same issue.

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

@kkerley
Copy link

kkerley commented Aug 2, 2022

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
Copy link

kkerley commented Aug 3, 2022

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.

@juanca
Copy link

juanca commented Sep 21, 2022

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

@juanca
Copy link

juanca commented Nov 12, 2023

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

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

No branches or pull requests

4 participants