Passing locals to `json.array!` ?
jamesst20 opened this issue · comments
Hi,
I am trying to find what is the best way to pass locals
to json.array!
The reason behind this is that I would like to render a "lighter" version of my objects in the index
action whereas the show
action would render more attributes.
Exemple
json.array! cards, partial: "api/cards/card", as: :card, locals: { full: true }
Is there a way to achieve something like that?
Thank you!
@jamesst20 yes, in the current version of jbuilder (v2.11.5) the example you shared works with an explicit locals:
key. You can also pass locals as additional kwargs. Both of these are equivalent:
json.array! cards, partial: "api/cards/card", as: :card, locals: { full: true }
json.array! cards, partial: "api/cards/card", as: :card, full: true
This was my mistake.
json.array!
supports locals just fine. What didn't was this scenario
json.user(user, partial: "path/to/partial", as: :user, full: true) # full is not passed
json.user(user, partial: "path/to/partial", as: :user, locals: { full: true }) # full is not passed
However this can be replaced by
json.partial! "path/to/partial", user: user, full: true
json.partial! "path/to/partial", locals: { user: user, full: true }
Which works just fine. I will close the ticket.