Catch Unknown action in Rails 3 for custom 404

前端 未结 4 1813
挽巷
挽巷 2021-01-31 12:14

I want to catch unknown action error in Rails 3, that shows \"Unknown Action\" error on development and the 404.html on production. I tried putting this rescue_from

4条回答
  •  再見小時候
    2021-01-31 12:40

    If you really want 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).

提交回复
热议问题