119

rails 5中的此代码

class PagesController < ApplicationController
  def action
    render nothing: true
  end
end

导致以下弃用警告

DEPRECATION WARNING: :nothing` option is deprecated and will be removed in Rails 5.1. Use `head` method to respond with empty response body.

我该如何解决?

4

1 回答 1

191

根据rails source,这是在通过nothing: truerails 5 时在引擎盖下完成的。

if options.delete(:nothing)
  ActiveSupport::Deprecation.warn("`:nothing` option is deprecated and will be removed in Rails 5.1. Use `head` method to respond with empty response body.")
  options[:body] = nil
end

因此,只需替换nothing: truebody: nil即可解决问题。

class PagesController < ApplicationController
  def action
    render body: nil
  end
end

或者你可以使用 head :ok

class PagesController < ApplicationController
  def action
    head :ok
  end
end
于 2016-01-09T01:31:31.670 回答