LoginSignup
3
6

More than 5 years have passed since last update.

Rails5なAPIで単純に404を返す方法

Last updated at Posted at 2017-01-22

状況

API作成時に単純に404を返そうとしたら、deprecatedなwarningが発生
Rails5で怒られない書き方を調べたのでメモ

環境

Ruby 2.4.0
Rails 5.0.1

結論

head :not_found

を利用し、warningが出なくなった

流れ

最初に調べた方法で、以下のように記述

render :nothing => true, :status => 404

すると、テストは通るものの以下の警告が発生

DEPRECATION WARNING: `:nothing` option is deprecated and will be removed in Rails 5.1. Use `head` method to respond with empty response body. (called from show at controller.rb:xx)

内容的には5.1で削除される予定となっているので、テスト自体は通る
しかし、いずれ使えるなくなるとわかっているので、修正せねば
で、調べて行き着いたところが↓

head :not_found

あとがき

iOSで次のバージョンで消えるよ!という警告を無視して痛い目に合いまくって来たので、事前の警告はしっかり対応していかねばと思った。

3
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
6