Search code examples
ruby-on-railsjsonhelperjbuilder

Need to create a json string from Jbuilder in rails Helper


I need to create a complex json from my Rails helper

I tried to use Jbuilder for that. I tried a code from a xxxx.json.jbuilder and it is working great.

json.array! @demandes.includes(....) do |demande| 
  json.id demande.id
  json.title demande.title
  .....
end

from my_app_url/xxxx.json, the object is well generated.

But I want to generate the object from a Rails Helper and assigne it to a variable. It's possible? If yes, how to do it?

Thanks


Solution

  • Can you try with:

    ApplicationController.render(template: 'demandes/index', assigns: { demandes: @demandes })

    (Substitute with your template name)