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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34688726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:20:47  来源:igfitidea点击:

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

ruby-on-railsruby-on-rails-5

提问by Linus Oleander

This code in rails 5

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

How do I fix this?

我该如何解决?

回答by Linus Oleander

According to the rails source, this is done under the hood when passing nothing: truein rails 5.

根据rails 来源,这是在通过nothing: truerails 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

Just replacing nothing: truewith body: nilshould therefore solve the problem.

因此,只需替换nothing: truewith 即可body: nil解决问题。

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

alternatively you can usehead :ok

或者你可以使用head :ok

class PagesController < ApplicationController
  def action
    head :ok
  end
end