":nothing" 选项已弃用,将在 Rails 5.1 中删除

The ":nothing" option is deprecated and will be removed in Rails 5.1

此代码在rails5

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.

我该如何解决这个问题?

根据 the rails source,这是在 rails 中传递 nothing: true 时在幕后完成的 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: true 替换为 body: nil 即可解决问题。

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

alternatively you can use head :ok

class PagesController < ApplicationController
  def action
    head :ok
  end
end