jbuilder icon indicating copy to clipboard operation
jbuilder copied to clipboard

nil! after cache! not working reliable

Open ThomasPr opened this issue 8 years ago • 2 comments

I came up with a really strange issue tonight: The following snippet produces an error on the THRID request. The first and second requests result in the expected json.

json.array! [1, 2, 3] do |index|
  json.cache! index do
    json.key1 "anything"
  end
  json.key2 do
    json.nil!
  end
end
ActionView::Template::Error (Failed to add "key2" property to null object):
    2:   json.cache! index do
    3:     json.key1 "anything"
    4:   end
    5:   json.key2 do
    6:     json.nil!
    7:   end
    8: end
  app/views/example/example.json.jbuilder:5:in `block in _app_views_example_example_json_jbuilder___2645842209584969737_70123296568040'
  app/views/example/example.json.jbuilder:1:in `_app_views_example_example_json_jbuilder___2645842209584969737_70123296568040'

I created a minimal example. Please execute curl -X "GET" "http://localhost:3000" three times to get the error.

activesupport (4.2.6) lib/active_support/proxy_object.rb:10:in `raise'
jbuilder (2.4.1) lib/jbuilder.rb:265:in `_merge_block'
jbuilder (2.4.1) lib/jbuilder.rb:38:in `set!'
jbuilder (2.4.1) lib/jbuilder/jbuilder_template.rb:74:in `set!'
jbuilder (2.4.1) lib/jbuilder.rb:66:in `method_missing'
app/views/example/example.json.jbuilder:5:in `block in _app_views_example_example_json_jbuilder___2645842209584969737_70123296568040'
jbuilder (2.4.1) lib/jbuilder.rb:298:in `block (2 levels) in _map_collection'
jbuilder (2.4.1) lib/jbuilder.rb:305:in `_scope'
jbuilder (2.4.1) lib/jbuilder.rb:298:in `block in _map_collection'
jbuilder (2.4.1) lib/jbuilder.rb:297:in `map'
jbuilder (2.4.1) lib/jbuilder.rb:297:in `_map_collection'
jbuilder (2.4.1) lib/jbuilder.rb:188:in `array!'
jbuilder (2.4.1) lib/jbuilder/jbuilder_template.rb:64:in `array!'
app/views/example/example.json.jbuilder:1:in `_app_views_example_example_json_jbuilder___2645842209584969737_70123296568040'
actionview (4.2.6) lib/action_view/template.rb:145:in `block in render'

ThomasPr avatar Mar 28 '16 20:03 ThomasPr

We're seeing this too on latest gem version 2.4.1

ngan avatar Apr 12 '16 23:04 ngan

The strange thing is if you move the json.key2 declaration above the json.cache! declaration, things work.

ngan avatar Apr 12 '16 23:04 ngan