I have this line in my jbuilder template using the new hash syntax. It seems this line is not executed at all.
json.partial! 'comments', post:, comments:
If I modify the line back to the old syntax or add braces or brackets, it works.
json.partial!('comments', post:, comments:)
json.partial! 'comments', { post:, comments: }
json.partial! 'comments', post: post, comments: comments
Can anyone explain what happens here? Is it something related to the template compilation? Thanks.
Test template for simplicity:
I don't know how to get to it any other way but you can inspect compiled
sourceat: https://github.com/rails/rails/blob/v7.0.4.3/actionview/lib/action_view/template.rb#L282to see what code actually runs when template renders:
formatted
If you don't have brackets around it,
json.target!becomes the value for thenameand your template returns whateverjson.attrreturns orjson.partial!in your case.It's also fixable if you put a semicolon at the end of this line:
https://github.com/rails/jbuilder/blob/v2.11.5/lib/jbuilder/jbuilder_template.rb#L277
A simplified version
So
method hash:is still an iffy syntax and much less obvious in a template. Just use parenthesis:https://bugs.ruby-lang.org/issues/18396