Reputation:
I am looking at a code that in one of its controllers, in one of its action methods first it calls a service to get a JSON, puts it in a json_data
varaible and as for the next and last command says render json: json_data
But I can't understand what happens after that? what is the next line of code that gets run?
Upvotes: 0
Views: 182
Reputation: 26193
render json: json_data
is in fact the final statement executed in the function. After it's executed, the function is automatically exited.
Whenever something is "rendered" in a Rails controller, whether it's an action, a template, or otherwise, the rendering is the final executed statement in the controller function.
In the case of the render json: json_data
, ActionController
will render out the passed argument, json_data
, by whatever method is defined. In this case, it's JSON, so Rails renders the contents of json_data
to the browser in JSON format, complete with respective headers. To contrast, if the statement was render text: json_data
, ActionController would send the contents of json_data
to the browser as text.
You may want to check out the canonical Rails guide's documentation on render
, which provides several examples of it can be invoked within the context of a controller action.
Upvotes: 4