1

I have a hackish sort of setup in which I am displaying some information on a list of companies and then clicking one of those company names displays a list of all leads generated for that company. I accomplished this by passing the company_id through the params. This works beautifully and I can search and filter to my heart's content until I click 'Clear Filters' and it strips everything, including the company_id. The result is an Oops page, because the system doesn't have a company_id to use to generate the list of leads.

I attempted something like this in the controller for that page

after filter: company_id, :only => :index

def franchise_id
  params.merge({:franchise_id => params[:franchise_id]})
end

but this only injects the ID back in after I filter something, and not after 'Clear Filters' was clicked.

Is there a way to override the 'Clear Filters' behavior for just this page and force it to save the company_id?

2 Answers2

3

Previous solutions does not work for me so if anyone is still struggling with clear_filters_btn :

Here how I make it works :

Add this to active_admin.js

$(function() {
  $(document).off('click', '.clear_filters_btn');
  $('.clear_filters_btn').attr('href', '?commit=clear_filters');
})
Community
  • 1
  • 1
Tracy LOISEL
  • 81
  • 1
  • 1
2

Active admin currently does this:

$ ->
  $('.clear_filters_btn').click ->
    window.location.search = ''

You could manually remove this click event to do whatever you want:

$ ->
  $('.clear_filters_btn').off('click').click ->
    # your stuff

Though you might want to create a ticket on Github so we can discuss the merits of changing the button's behavior.

UPDATE 1

I wrote up some code to selectively strip out Active Admin's params, leaving yours in place:

$ ->
  $('.clear_filters_btn').click ->
    location.search = location.search.split('&').filter(
      (s)-> not s.match(/^(q\[|page|commit|order)/)
    ).join '&'

Put that in active_admin.js.coffee and you'll be good.

UPDATE 2

If you want to apply JS to a specific page in Active Admin, you can check on page load the CSS classes applied to the HTML <body> tag to tell if it's the one you want. Notably, AA adds the current controller verb (index, show, etc.) and the actual name of the resource in question. To apply this code only to the index page for companies:

$ ->
  if $('body.index.admin_companies').length
    $('.clear_filters_btn').click ->
      location.search = location.search.split('&').filter(
        (s)-> not s.match(/^(q\[|page|commit|order)/)
      ).join '&'
seanlinsley
  • 3,165
  • 2
  • 25
  • 26
  • I hope you'll forgive my ignorance, but I'm still pretty new to a lot of development and I don't know much at all about Javascript. How would I go about overriding this in the context of the one admin page I am working on? – unstablerealtiy Dec 21 '13 at 01:39
  • @unstablerealtiy I updated my answer with an explicit solution that you should be able to copy & paste into your application. – seanlinsley Dec 22 '13 at 00:47
  • BTW I created a ticket for this if you want to weigh in on it: https://github.com/gregbell/active_admin/issues/2811 – seanlinsley Dec 22 '13 at 00:58