1

I just have a simple index page which shows all the items of an ActiveRecord. What I'd like to have is that the table containing the items gets automatically refreshed every X seconds (i.e. loaded from the DB and rendered). I already redefined the index action as a partial rendering

[app/admin/item.rb]
ActiveAdmin.register Item do
   index do   
      render :partial => "items_list"
   end
end

And then I have

[app/views/admin/items/_items_list.html.erb]

(I don't mind using ERB or ARB to write the partial) The list table is rendered correctly when I first load the page.

I'm not sure which Javascript I should include in the page to refresh the list every X seconds. More specifically, which URL should be called by the Javascript command? Do I need to define any custom action in the controller?

Thank you for any advice.

Thomas

1 Answer 1

3

I finally managed it.

[app/admin/item.rb]
ActiveAdmin.register Item do
   ...
   index do
      # do nothing; table will be filled with a partial via Javascript
   end

   collection_action :items_list do 
      @items = Item.all
  render :partial => "items_list"
   end
end

[app/views/admin/_items_list.html.arb]
table_for items do 
    column "attr_1"
    column "attr_2"
    column "attr_3"
end

[app/assets/javascripts/items.js]
$(document).ready( function() {
   setInterval(function(){
    $('#index_table_items').load('items/items_list');},1000);
   })

finally append to app/assets/javascripts/active_admin.js the following

//= require inbox_files

This way I get the list table updated every second. Also the CSS doesn't get distorted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.