如何使用rails format.json [英] how to use rails format.json
本文介绍了如何使用rails format.json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
def index
@cellphones = Cellphone.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @cellphones }
end
-
请简单回答...
SIMPLE ANSWERS PLEASE...
-
format.json 的用途是什么?
我必须为其创建视图吗?
do I have to create a view for it?
它与 javascrip 有什么关系?
它与 ajax 有什么关系?
请举一个简单的示例来说明如何使用它...
please give a simple example of how to use it...
推荐答案
- 用于返回JSON.
- 否,它将
@cellphones
呈现为JSON.您可以 创建视图,例如,使用JSON构建器自定义输出,而不是Rails提供的现成功能. - JavaScript通常用于发出JSON Ajax请求,例如客户端框架.
- Ajax调用通常需要JSON作为结果.他们还可能要求HTML直接呈现.
- 是一个如何使用它的示例;向
/cellphones
发出Ajax请求.
- It's used to return JSON.
- No, it renders
@cellphones
as JSON. You may create a view, though, for example, using a JSON builder to customize the output beyond what Rails provides out of the box. - JavaScript is often used to make JSON Ajax requests, like for client-side frameworks.
- Ajax calls often want JSON as a result. They may also request HTML to render directly.
- That is an example of how to use it; make an Ajax request to
/cellphones
.
这篇关于如何使用rails format.json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
-
查看全文