Jbuilder Partial With Path

I am trying to make a partial jbuilder file using below

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

partial below (users / _user_reduced.json.jbuilder)

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

However, I get the error message:

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

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

+6
source share
1 answer

I tried to fix this for several hours and finally found that the syntax (the thought indicated in the jbuilder readme file) above does not work. You must format the partial parts as shown below:

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

So in my case it will be:

 json.(@request, :id) json.profile do json.partial! partial: 'users/user_reduced', locals: {user: @request.user} end 
+7
source

Source: https://habr.com/ru/post/985217/


All Articles