「:nothing」オプションは非推奨となり、Rails 5.1 で削除されます。質問する

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

これを修正するにはどうすればいいでしょうか?

ベストアンサー1

によるとレールソースnothing: trueこれは、 Rails 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

代わりに、 head :ok

class PagesController < ApplicationController
  def action
    head :ok
  end
end

おすすめ記事