不推荐使用“:nothing”选项,将在Rails 5.1中删除

这个代码在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.

我该如何解决?

Oleander asked 2019-08-13T09:18:01Z
1个解决方案
145 votes

根据导轨源,这是在导轨5中通过head :ok时在引擎盖下完成的。

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

因此,用body: nil替换head :ok应该可以解决问题。

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

或者你可以使用head :ok

class PagesController < ApplicationController
  def action
    head :ok
  end
end
Oleander answered 2019-08-13T09:18:31Z
translate from https://stackoverflow.com:/questions/34688726/the-nothing-option-is-deprecated-and-will-be-removed-in-rails-5-1