0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

#Rails error handling : routing not existence all path with request method GET / POST / PUT / PATCH / DELETE / OPTIONS and return 404 not found

Last updated at Posted at 2019-12-01

Warning

do not use easy

How

match '*path', to: 'errors#not_found', via: :all

or describe each request types

  get '*path', to: 'errors#not_found'
  post '*path', to: 'errors#not_found'
  put '*path', to: 'errors#not_found'
  patch '*path', to: 'errors#not_found'
  delete '*path', to: 'errors#not_found'
  match '*path', to: 'errors#not_found', via: :options

rspec test example

require 'rails_helper'

describe 'not found path', type: :request do
  describe 'get' do
    subject { get "/path/to/not/existence/" }

    before { subject }

    it { expect(response.status).to eq 404 }
  end

  describe 'post' do
    subject { post "/path/to/not/existence/" }

    before { subject }

    it { expect(response.status).to eq 404 }
  end

  describe 'put' do
    subject { put "/path/to/not/existence/" }

    before { subject }

    it { expect(response.status).to eq 404 }
  end

  describe 'patch' do
    subject { patch "/path/to/not/existence/" }

    before { subject }

    it { expect(response.status).to eq 404 }
  end

  describe 'delete' do
    subject { delete "/path/to/not/existence/" }

    before { subject }

    it { expect(response.status).to eq 404 }
  end

  context 'options' do
    subject { process :options, "/path/to/not/existence/" }

    before { subject }

    it { expect(response.status).to eq 404 }
  end
end

REf

Railsの ActionController::RoutingError は ApplicationController での rescue_from で捕まえられない - Qiita

Original by Github issue

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

Twitter

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?