rails / jbuilder

Jbuilder: generate JSON objects with a Builder-style DSL
MIT License
4.34k stars 440 forks source link

Output simple field values from blocks #464

Closed toncid closed 5 years ago

toncid commented 5 years ago

Is it possible to set a simple field value that is result of a do-end block:

json.some_field do
  if some_condition?
    42
  else
    13
  end
end

This would result with some_field as either 42 or 13 ({"someField": 42}), but in reality it is fully omitted from the output JSON.

Richard-Degenne commented 5 years ago
json.some_field begin
  if some_condition?
    42
  else
    13
  end
end

Instead of giving your block to Jbuilder, evaluate it, and give Jbuilder the result.

toncid commented 5 years ago

Thanks!