0

I have a client with a Rails 6 site running ActiveAdmin, and the client would like to return the user back to the page he or she was on in the index after selecting edit and updating a record.

For example, if a user is on the page /admin/employees?page=2 and clicks the "Edit" link for an employee, after updating the employee record, they want to be returned back to /admin/employees?page=2

I've got how to return it back to the index page instead of the show page, I just can't get it to go back to page it was on.

Can anyone help?

Carl
  • 1,246
  • 3
  • 21
  • 39

1 Answers1

1

Assuming you're using sessions, then following the advice here:

Link back to page visited before form

should help. To apply it to active_admin, in your app/admin/employees.rb, first of all save the last URL every time you hit the edit page, and then redirect to it after a successful update:

controller do
  def edit
    session[:my_previous_url] = URI(request.referer || '').path
    super
  end

  def update
    @employee.update(permitted_params[:employee])
    # check if the previous URL was correctly saved in session, if so use that, if not use employees index
    redirect_target = session[:my_previous_url].blank? ? employees_path : session[:my_previous_url]
    if @employee.save
      redirect_to redirect_target
    else
      render :edit
    end
  end
end
Mark
  • 6,112
  • 4
  • 21
  • 46