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 ignore_nil, key_format, and deep_format_keys not working (Rails 7) #548

Open
shettytejas opened this issue May 5, 2023 · 0 comments

Comments

@shettytejas
Copy link

shettytejas commented May 5, 2023

File Structure:

config/initializers/jbuilder.rb

Jbuilder.ignore_nil
Jbuilder.key_format camelize: :lower
Jbuilder.deep_format_keys

config/routes.rb

  namespace 'api', defaults: { format: :json } do
    namespace 'v1' do
      resource 'post', only: [:create]
    end
  end

app/views/layouts/application.json.jbuilder

json.success @result.success?
json.merge! yield

app/views/api/v1/posts/create.json.jbuilder

json.data @model

Expected Result:

{
  success: true,
  data: {
    id: 1,
    subject: 'Test',
    content: 'Testing Content',
    createdAt: 'some timestamp'
  }
}

Actual Result:

{
  data: {
    id: 1,
    subject: 'Test',
    content: 'Testing Content',
    created_at: 'some timestamp',
    updated_at: nil
  }
}

I don't think I've set up the initializer wrong as inside the create.json.jbuilder, I used a debugger and checked that calling json, I can see that ignore_nil and other specified options have been set correctly. Yet the actual behaviour is not according to the expectations.

P.S: It's also not loading the success attribute from application.json.jbuilder that I had mentioned.

Any help regarding this would be nice 😄

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

1 participant