Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rescue_from ::AbstractController::ActionNotFound not working

I have the following code:

unless Rails.application.config.consider_all_requests_local
  rescue_from Exception, with: :render_exception
  rescue_from ActiveRecord::RecordNotFound, with: :render_exception
  rescue_from ActionController::UnknownController, with: :render_exception
  rescue_from ::AbstractController::ActionNotFound, with: :render_exception
  rescue_from ActiveRecord::ActiveRecordError, with: :render_exception
  rescue_from NoMethodError, with: :render_exception
end

They all work flawless, except ::AbstractController::ActionNotFound

I've also tried

AbstractController::ActionNotFound
ActionController::UnknownAction

error:

   AbstractController::ActionNotFound (The action 'show' could not be found for ProductsController):
like image 454
vladCovaliov Avatar asked Nov 17 '12 17:11

vladCovaliov


2 Answers

This similar question suggests that you can no longer catch an ActionNotFound exception. Check the link for workarounds. This suggestion to use a Rack middleware to catch 404s looks the cleanest to me.

like image 196
Buck Doyle Avatar answered Nov 15 '22 20:11

Buck Doyle


To rescue AbstractController::ActionNotFound in a controller, you can try something like this:

class UsersController < ApplicationController

  private

  def process(action, *args)
    super
  rescue AbstractController::ActionNotFound
    respond_to do |format|
      format.html { render :404, status: :not_found }
      format.all { render nothing: true, status: :not_found }
    end
  end


  public

  # actions must not be private

end

This overrides the process method of AbstractController::Base that raises AbstractController::ActionNotFound (see source).

like image 37
user1003545 Avatar answered Nov 15 '22 19:11

user1003545