什么`:location => ...`和`head:ok`意思是在'respond_to'格式声明中? [英] What does `:location => ...` and `head :ok` mean in the 'respond_to' format statement?

查看:192
本文介绍了什么`:location => ...`和`head:ok`意思是在'respond_to'格式声明中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ruby on Rails 3,我想知道:location => ... head:ok 语句在下面的代码中有意义,它们是如何工作的以及如何使用这些语句。

  respond_to do | format | 
format.xml {render:xml => @user,:status => :created,:location => @user}
结束

respond_to do | format |
format.xml {head:ok}
end


解决方案


  1. render ...:location => @user 会设置 HTTP位置标题来通知客户新创建的资源的位置(即其URL)

  2. head:ok sets render 返回一个空的响应(所以只是标题,没有正文),状态200. head:ok 的缩写:true,status::ok

    以下列出了所有可用于设置适当状态码的:status 选项。



I am using Ruby on Rails 3 and I would like to know what the :location => ... and head :ok statements mean in following code, how they work and how I can\should use those.

respond_to do |format|
    format.xml  { render :xml => @user, :status => :created, :location => @user }
end

respond_to do |format|
    format.xml  { head :ok }
end

解决方案

  1. render ... :location => @user will set the HTTP location header to inform the client of the location of the newly created resource (that is, its URL)

  2. head :ok sets render to return an empty response (so just the header, no body) with status 200. head :ok is shorthand for render nothing: true, status: :ok.
    Here's a list of all the :status options you can use for setting the appropriate status code.

这篇关于什么`:location => ...`和`head:ok`意思是在'respond_to'格式声明中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆