[英]Rails Controller - What is the purpose of “respond to do format” html and json?
Ruby on Rails Question: Inside the controller you have seven REST actions. Ruby on Rails问题:在控制器内部,您有七个REST操作。 Almost all of them have respond to do format xml/html or json. 几乎所有人都响应xml / html或json格式。 I have no idea what this means. 我不知道这是什么意思。 Can you please explain it's purpose. 你能解释一下它的目的吗? For example: 例如:
def index
@tweets = Tweet.all
respond_to do |format|
format.html
format.json { render json: @tweets }
end
end
What is the purpose of the "respond to" part that contains the html and json? 包含html和json的“响应”部分的目的是什么? What do these formats do? 这些格式做什么? Also, what is the difference between xml and html? 另外,xml和html有什么区别? Sometimes I see xml and other times html. 有时我看到xml,其他时候看到html。
Thank you 谢谢
Its just a way of telling your controller how to respond to different request types . 这只是告诉您的控制器如何响应不同请求类型的一种方法 。 For example your client could want html or xml information from you: 例如,您的客户端可能需要您提供html或xml信息:
def index
@people = Person.find(:all)
respond_to do |format|
format.html
format.xml { render :xml => @people.to_xml }
end
end
What that says is, "if the client wants HTML in response to this action, just respond as we would have before, but if the client wants XML, return them the list of people in XML format." 这就是说,“如果客户想要HTML来响应此操作,请像以前一样响应,但是如果客户想要XML,则以XML格式返回他们的人员列表。” (Rails determines the desired response format from the HTTP Accept header submitted by the client.) (Rails从客户端提交的HTTP Accept标头中确定所需的响应格式。)
http://apidock.com/rails/ActionController/MimeResponds/InstanceMethods/respond_to http://apidock.com/rails/ActionController/MimeResponds/InstanceMethods/respond_to
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.