控制器动作方法中的“渲染”在哪里?

时间:2022-12-01 23:39:21

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

我正在查看一个代码,在其一个控制器中,在其一个操作方法中首先调用一个服务来获取一个JSON,将它放在一个json_data变量中,并且对于下一个和最后一个命令,表示渲染json:json_data

But I can't understand what happens after that? what is the next line of code that gets run?

但我无法理解之后会发生什么?运行的下一行代码是什么?

1 个解决方案

#1


3  

render json: json_data is in fact the final statement executed in the function. After it's executed, the function is automatically exited.

渲染json:json_data实际上是函数中执行的最终语句。执行后,该功能自动退出。

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.

每当在Rails控制器中“渲染”某些内容时,无论是动作,模板还是其他内容,渲染都是控制器函数中的最终执行语句。

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.

在渲染json:json_data的情况下,ActionController将通过定义的任何方法呈现传递的参数json_data。在这种情况下,它是JSON,因此Rails以JSON格式将json_data的内容呈现给浏览器,并带有相应的标题。相比之下,如果语句是渲染文本:json_data,ActionController会将json_data的内容作为文本发送到浏览器。

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.

您可能需要查看有关渲染的规范Rails指南的文档,该文档提供了几个可以在控制器操作的上下文中调用的示例。

#1


3  

render json: json_data is in fact the final statement executed in the function. After it's executed, the function is automatically exited.

渲染json:json_data实际上是函数中执行的最终语句。执行后,该功能自动退出。

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.

每当在Rails控制器中“渲染”某些内容时,无论是动作,模板还是其他内容,渲染都是控制器函数中的最终执行语句。

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.

在渲染json:json_data的情况下,ActionController将通过定义的任何方法呈现传递的参数json_data。在这种情况下,它是JSON,因此Rails以JSON格式将json_data的内容呈现给浏览器,并带有相应的标题。相比之下,如果语句是渲染文本:json_data,ActionController会将json_data的内容作为文本发送到浏览器。

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.

您可能需要查看有关渲染的规范Rails指南的文档,该文档提供了几个可以在控制器操作的上下文中调用的示例。