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

这段代码在 Rails 5中

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

results in the following deprecation warning

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

我该怎么补救?

30053 次浏览

根据 铁路来源,这是在引擎盖下完成时,通过 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

只要用 body: nil代替 nothing: true就可以解决这个问题。

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

也可以使用 head :ok

class PagesController < ApplicationController
def action
head :ok
end
end