avo-hq/avo

How do I redirect if record not found?

Closed this issue ยท 5 comments

Feature

When a user visits a wrong SHOW or EDIT record link I want to redirect them to the INDEX
I tried to do finetune the controller like

module Avo
  class ActivitiesController < Avo::AvoBaseController
    def show
      super
    rescue ActiveRecord::RecordNotFound # ๐Ÿ‘‰ I never reach this rescue block...
      redirect_to some_valid_path, notice: 'Record not found'
      nil
    end
  end
end

but I never reach the rescue...

do I need to redefine
https://docs.avohq.io/3.0/customization.html#custom-find-method-for-show-and-edit-pages

Current workarounds

?

Screenshots or screen recordings

Image

Hi @pjmuller

That error is raised on a before_action that's why you can't rescue it on the show method.

You can catch it on set_record method:

class Avo::ProjectsController < Avo::ResourcesController
  def set_record
    super
  rescue ActiveRecord::RecordNotFound
    @record_not_found = true
  end

  def show
    if @record_not_found
      redirect_to root_path
    else
      super
    end
  end
end

Closing as completed. Let me know if you still have any question.

@Paul-Bob works like a charm for SHOW, but not for EDIT, there I get this stacktrace

Image

the show was more important to me, so only if easy feel free to help me out on the EDIT ;)

Got it, some actions are running after set_record and before hitting edit are breaking because rely on an existent record.

You can redirect directly from the set_record instead if you want the same behavior on the missing record.

class Avo::ProjectsController < Avo::ResourcesController
  def set_record
    super
  rescue ActiveRecord::RecordNotFound
    redirect_to root_path
  end
end

@Paul-Bob works like a charm for both SHOW as EDIT, might make sense to document somewhere (though I guess your list of things to document is pretty long by now :) ?

Cool!

might make sense to document somewhere

Totally!

though I guess your list of things to document is pretty long by now :)

It's getting bigger :D

It makes sense to be documented here https://docs.avohq.io/3.0/controllers.html