“:无"选项已弃用,将在 Rails 5.1 中删除 [英] The ":nothing" option is deprecated and will be removed in Rails 5.1

查看:36
本文介绍了“:无"选项已弃用,将在 Rails 5.1 中删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

rails 5 中的这段代码

This code in 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.

我该如何解决这个问题?

How do I fix this?

推荐答案

根据 rails 源,这是在 Rails 5 中传递 nothing: true 时在后台完成的.

According to the rails source, this is done under the hood when passing nothing: true in 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 即可解决问题.

Just replacing nothing: true with body: nil should therefore solve the problem.

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

你也可以使用头部:ok

class PagesController < ApplicationController
  def action
    head :ok
  end
end

这篇关于“:无"选项已弃用,将在 Rails 5.1 中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆