125

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

Linus Oleander
  • 17,746
  • 15
  • 69
  • 102
  • 3
    probably because deprecation warning tells you exactly how to fix it. – sevenseacat Feb 02 '16 at 05:05
  • 29
    @sevenseacat No, it just refers to `head`, that's all. You still have to look up the API being used. Note that this is a Q&A-style post with the goal to quickly solve the above deprecation warning without having to read through the official API. The post is currently raking #1 on google on the above warning, which was my initial goal. – Linus Oleander Feb 02 '16 at 08:20

1 Answers1

198

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

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

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

alternatively you can use head :ok

class PagesController < ApplicationController
  def action
    head :ok
  end
end
Linus Oleander
  • 17,746
  • 15
  • 69
  • 102
  • 4
    `render body: nil` worked for me, `render head :ok` didn't (it gave some double render error). – Fellow Stranger Jan 26 '16 at 15:07
  • 14
    If you'd like to change the status codes, there are options other than just `:ok` http://guides.rubyonrails.org/layouts_and_rendering.html#the-status-option – TJ Biddle May 01 '16 at 04:14
  • 1
    You can also use `head 403` or any other status code. – Hendrik Feb 14 '17 at 11:47
  • 3
    another example would be `head :unauthorized` to return status code 401 – Jirapong Feb 22 '17 at 06:53
  • 1
    @Dan do you mind explaining why it's the preferred syntax? From a code reader standpoint, it makes much more sense for me the `render body: nil` than `head :ok`. – Erowlin Apr 21 '17 at 16:04
  • @Erowlin That's an interesting question. My guess is that I was referencing http://stackoverflow.com/a/18059789/866618, which in turn was referencing the Rails guide. The Rails guide link has changed since when I commented above and the WayBackMachine is offline so I can't tell you what it said back then. The Rails guide link referenced now isn't very definitive... – Dan Apr 21 '17 at 19:21
  • 24
    @FellowStranger, it's not `render head: :ok`, it's `head :ok`. No `render`. I struggled with that too. – ben Dec 15 '17 at 23:25
  • Confusing. `head` is not a verb unless you "head in a direction"; Adding a new resolving method call to controller actions complexifies behavior; At least using `render status: foo, body: bar` allows a non-Rails dev to have a chance of grokking the code – Luke Griffiths Oct 16 '20 at 20:29