'Assigning jBuilder output to a variable

I have a Ruby on Rails App that uses jbuilder. I am using jbuilder to help render json responses. Is there a way assign the jbuilder partial to a variable without rendering the result? I have been trying to do something like below, but I am getting this undefined method 'key' for nil:NilClass error. It seems I am not correctly passing the users model to the user.json.jbuilder Can anyone offer any help?

context = ActionController::Base.new.view_context
user_json = JbuilderTemplate.new(context) do |json|
 json.partial! "/users_api/user.json.jbuilder", user: user
end.target!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source