I\'m using JBuilder as to return some JSON. I have a index.json.jbuilder that generates the data, and I need to render it to a string. However, I\'m not sure ho
If the view users.json.jbuilder is at the default path relative to the controller and it cannot find the template, it may be due to a format discrepancy, as it may be trying to look for the html format file. There are two ways to fix this:
Have the client GET /users/index.json
or
Specify the formats option when calling render_to_string (also applies to render):
#controllers/users_controller.rb
def index
@users = User.all
@users_json = render_to_string( formats: 'json' ) # Yes formats is plural
end
This has been verified in Rails 4.1.