我正在使用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. render ... :location => @user将设置HTTP location header以通知客户端新创建的资源的位置(即其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.

Ruby-on-rails相关问答推荐

引用连接 Rails 表中枚举的查询

Rails HMAC - 使用应用程序机密作为加密密钥

NoMethodError:Puma::Events:Class 的未定义方法字符串

错误原始错误:未安装 ImageMagick/GraphicsMagick

有没有办法在 Rails 3.1 中检测用户代理

在 Rails 的 ActiveRecord 中,touch 有什么用?

Ruby on Rails 取消首字母大写

Rails 4,使用 ActiveRecord 的原始查询

添加自定义字段/列以使用 Rails 4 进行设计

如何在 rspec 中运行单个测试?

如何从 ActiveRecord 中的每个组中获取最新记录?

Rails - 生产模式下的错误

form_tag 是否与 Simple_form 一起使用?

不允许请求来源:使用 Rails5 和 ActionCable 时的 http://localhost:3001

带有 master.key 的 Rails 5.2 - Heroku 部署

如何从 rails 控制器操作发出 404 响应?

如何使用 Rails 3 获取请求的目标控制器和操作?

我怎样才能看到水豚在失败的黄瓜步骤中发现了什么?

使用redirect_to后如何停止控制器执行? (使用导轨)

使用连接的 Ruby on Rails ActiveRecord 查询