简体   繁体   中英

Jbuilder Partial With Path

I am attempting to render a partial jbuilder file using the below

json.(@request, :id)

json.profile do
    json.partial! partial: 'users/user_reduced', user: @request.user
end

the partial is below (users/_user_reduced.json.jbuilder)

json.(user, :id, :name, :sex)

However, I get the error:

ActionView::Template::Error (undefined local variable or method `user' for #<#<Class:0x007fe4ebc91878>:0x007fe4ebe599f8>):

It's as if the local user variable is not being set. What is the syntax for setting a local variable to a jbuilder partial path?

I have tried to fix this for hours, and found, finally, that the syntax (thought listed on the jbuilder readme) above does not work. You must format your partials like the below:

json.partial! 'partials/partial_path', locals: {local_var: my_local_var}

So in my case, it would be:

json.(@request, :id)

json.profile do
    json.partial! partial: 'users/user_reduced', locals: {user: @request.user}
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM