简体   繁体   English

Rails Controller-“响应格式” html和json的目的是什么?

[英]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.

相关问题 这个Rails代码中的“respond_to”,“do”和“| format |”是什么? - What is “respond_to” and “do” and “|format|” in this Rails code? Rails 3-即使请求格式为text / html,控制器也使用javascript进行响应 - rails 3 - controller respond with javascript even through request format was text/html Rails response_to格式化HTML Json不适用于params - Rails respond_to format HTML Json Not workng with params 使用控制器操作响应JSON的Rails方式(在3.2.6中)是什么? - What is the Rails Way (in 3.2.6) to respond to JSON with controller actions? 如何使用 Rails 在 HTML 和 JSON 中响应? - How respond in HTML and JSON with Rails? Rails 4 - 仅响应 JSON 而不是 HTML - Rails 4 - Respond only to JSON and not HTML Rails:响应 JSON 和 HTML - Rails: respond_to JSON and HTML Rails 3 response_with用于在控制器位于不同模块中时创建返回的JSON或JSON格式的位置 - Rails 3 respond_with for create returning json or location for JSON format when controller is in different module Rails:如何让控制器生成器将format.json调用添加到所有动作response_to块? - Rails: Howto get the controller generator add format.json call to all action respond_to blocks? Rails控制器 - 处理404并响应动态HTML错误页面,无论请求格式如何 - Rails controller - handle 404 and respond with dynamic HTML error page regardless of request format
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM