Geo Geo - 1 year ago 124
JSON Question

How could I render to a string a JSON representation of a JBuilder view?

I'm using JBuilder as to return some JSON. I have a

that generates the data, and I need to render it to a string. However, I'm not sure how to do this, since:
don't seem to go through JBuilder.

How could I render the JBuilder view as a string?

Answer Source

I am rendering a collection of users as a json string in the controller like so:

def index
  @users = User.all
  @users_json = render_to_string( template: 'users.json.jbuilder', locals: { users: @users})

json.array!(users) do |json, user|
  json.(user, :id, :name)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download