Pranav RK
Pranav RK

Reputation: 61

How to redirect to Not Found page if an invalid URL is passed on Active Admin?

I'm using ActiveAdmin gem in my Rails application. I have an Item resource in the ActiveAdmin. I have allowed all the actions except new.

actions :all, except: [:new]

But in the URL if explicitly passed /admin/items/new, it throwing an ActiveRecord::Not Found error as it taking new as an id. In the description of the error, it is showing "Couldn't find Item with 'id'=new. I want to redirect to 404 or Not found page if an invalid url is passed. Can someone help me with this? Thanks is advance!

Upvotes: 1

Views: 619

Answers (2)

Subash khatri
Subash khatri

Reputation: 1

You can also do something like this:

def render_404
    respond_to do |format|
      format.html { render file: Rails.root.join('public', '404'), layout: false, status: 404 }
      format.json { render json: {}, status: 404 }
    end
  end

you can return like this too on any method you want to return 404 status.

render file: Rails.root.join('public', '404'), layout: false, status: 404

Upvotes: 0

Sjors Branderhorst
Sjors Branderhorst

Reputation: 2182

What you can do is open up the BaseController from active admin like this. You can put it in an initializer like: config/initializer/active_admin_overrides.rb

module ActiveAdmin
  class BaseController
    rescue_from ActiveRecord::RecordNotFound, :with => :resource_not_found
    def resource_not_found
      render :file => "#{Rails.root}/public/404.html", :status => 404
    end
  end
end

Upvotes: 3

Related Questions