リア充爆発日記

You don't even know what ria-ju really is.

RecordNotFound時に404を返したいときー

ググればでてくるんだけど、ほとんどが

  rescue_from ActiveRecord::RecordNotFound, with: :url_not_found

  def url_not_found
    respond_with  status: 404
  end

みたいに書いてあるんだよね。

でも、それだとResponse Code自体は200だからこれだとあかんと思うんです。検索エンジンとかはページヒット!って思うと思われ。

で、response.statusに直接404を指定すると、完全無欠の404が返ってくる。

class V1::ApplicationController < ActionController::Base
  respond_to :json
  before_filter :set_format

  rescue_from ActiveRecord::RecordNotFound, with: :url_not_found

  private

  def set_format
    request.format = :json
  end

  def url_not_found
    response.status = 404
    respond_with message: "404 Not Found"
  end

end

以上、よろしくお願い致します。