“: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. 

我该如何解决?

根据钢轨来源 ,这是在nothing: true传递的情况下完成的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: truebody: nil应该因此解决问题。

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

或者你可以使用 head :ok

 class PagesController < ApplicationController def action head :ok end end