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

Array attribute types incorrectly rendered with Jbuilder using key_format! option #265

Open
tsmith-dev opened this issue Jan 1, 2021 · 0 comments

Comments

@tsmith-dev
Copy link

tsmith-dev commented Jan 1, 2021

When using gon with Jbuilder and the key_format! :camelize :lower option, array database attributes are incorrectly parsed to json. Visiting the .json route displays the correctly parsed JSON from Jbuilder, but the JSON from Gon inside the <head> tag on the page is incorrect.

Example:

Attribute is stored in a database array attribute:

schema.rb

create_table "conditions", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
  t.string "determinant_name"
  t.string "operator"
  t.string "condition_value", default: [], array: true
end

Jbuilder template uses the key_format! option to convert snake_case to camelCase:

_form.json.jbuilder

json.key_format! camelize: :lower
json.rules @rules do |rule|
  json.(rule, :id)
  json.conditionsAttributes rule.conditions do |condition|
    json.(condition, :determinant_name, :condition_value, :operator)
  end
end

Expected result:

rules: [
  {
    id: "a915ba39-fa2e-4a47-ab04-20ecc330080e",
    conditionsAttributes: [
      {
        determinantName: "States",
        operator: "One of These",
        conditionValue: ["Alaska","California"]
      }   
    ]    
  }
]

Actual result:

rules: [
  {
    id: "a915ba39-fa2e-4a47-ab04-20ecc330080e",
    conditionsAttributes: [
      {
        determinantName: "States",
        operator: "One of These",
        conditionValue: ["0"]
      }   
    ]    
  }
]
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