因此,当一个人生成脚手架时,控制器会自动创建这样的块(?)
respond_to do |format|
format.html
format.xml { render :xml => @c }
end
Run Code Online (Sandbox Code Playgroud)
这究竟做,怎么来它format.html和format.xml?每个人做什么?
它定义了当前操作将响应各种格式(操作的内容可以以多种方式呈现,而不仅仅是普通的旧HTML).
/my/path/to/action.html,它将呈现HTML(来自模板);/my/path/to/action.xml,它将使用呈现XML { render :xml => @c }.Rails将通过调用变量to_xml上的方法生成XML @c;/my/path/to/action.json它,它将抛出404错误.