ruby-on-railsrubyactiveadmin

When an error occurs in the ActiveAdmin form the /new is removed from the URL


I'm creating an Admin page for my model i noticed that if there are errors in your new and edit forms the /new is removed from URL

for example if my url was /my_model/new

On error it is changed to /my_model

Is there any way to fix this because I'm writing custom javascript based upon the url if it's new do something if it's edit do something else

Please let me know if anyone knows a fix

Update:

i should've have provided more info what I'm doing and what's causing the issue so I have a model for which I'm creating an admin page using the ActiveAdmin gem

here is my model

class Task < ApplicationRecord
    validates :count, numericality: { greater_than_or_equal_to: 1, less_than_or_equal_to: 99 }, allow_nil: true
  end

and my active admin page looks something like this:

ActiveAdmin.register Task, as: "Tasks" do
  actions :all, except: [:destroy]
  menu label: "Tasks", parent: "Tasks Menu"

  permit_params :name, :count
  
  index do
    id_column
    column :name
    column :count
  end

  show do
    tabs do
      tab :basic do
        attributes_table do
          row :name
          row :count
        end
      end
    end
  end

  form do |f|
    f.semantic_errors *f.object.errors.keys

    f.inputs "Basic Details" do
      f.input :name     
      f.input :count

      f.actions
    end
  end
end

so when you open up a Item or try to create or update a Task the URL goes to either /admin/tasks/new or /admin/tasks/edit

so I need to make some changes using JQuery based upon the URL so I made a script for this which looks something like:

var window_location_elements = location.href.split("/");
var length = window_location_elements.length;


if(window_location_elements[length-1] === "edit" || window_location_elements[length-1] === "new"){
  // do something
}

So now the issue occurs when lets say i enter a value of count in my input which is not valid there is an error in my form which is shown perfectly but the weird thing that happens is as soon as there is an error the url switches from:

/admin/tasks/new to just /admin/tasks (same for edit)


Solution

  • This was a mistake on my end I was only thinking to just make my javascript work as I was only associating the new and edit form with the URL , but that's not ActiveAdmin handles form creation and updation, as in rails if a form is not valid we render the form again and send it as the response to an invalid form submission.

    Keeping this in mind and reading more about this I found out that ActiveAdmin updates the body element of the DOM to let the server know which form it is.

    Next the only thing needed was to update my javascript to handle this, here is the final javascript

    var bodyClassList = document.body.classList;
    
    if ( bodyClassList.contains("edit") || bodyClassList.contains("new") ||
          bodyClassList.contains("update") || bodyClassList.contains("create") ) {
       // do something
    }