`:location =>是什么...`和`head:ok`表示'respond_to'格式声明?

时间:2011-03-06 22:35:52

标签: ruby-on-rails ruby ruby-on-rails-3 format respond-to

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

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

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

1 个答案:

答案 0 :(得分:69)

  1. render ... :location => @user会设置HTTP location header以通知客户端新创建的资源的位置(即其网址)

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