Pagination for panel in active admin
Asked Answered
Y

1

12

I am using active admin for admin panel. On User show page I need to show his friends. I have two models User and Friend.

I want add pagination in the "friendship with panel" ie user.friends block.

How can I add pagination on one panel? Here is the code that I am using.

show do
  attributes_table  do
    row("Photo") { |user| image_tag(user.facebook_photo_url) }
    rows :name, :sex,:city
  end

  panel 'Friendship with' do
    table_for user.friends do
      column "" do |friend|
        (link_to image_tag(friend.facebook_photo_url('small')), admin_friend_path(friend)) + "       ".html_safe +  (link_to friend.name, admin_user_path(friend))
      end

    end
  end  

  active_admin_comments
end

The Friend model is actually Facebook friends so I can't use self-referential joins on User model (so don't say use one model instead of two) and I have some other panels on the same page. I need to make sure every panel has their own pagination param name so that they don't conflict with each other.

Yasmineyasu answered 24/12, 2012 at 6:37 Comment(0)
B
30

Some time ago I wrote something like this to add pagination to multiple tables on one page. You can have multiple tables, by changing paginated_collection parameters.

I hope this code can help.

users = User.by_customer(customer.customer_id) #by_customer is scope
panel 'Users' do
  paginated_collection(users.page(params[:users_page]).per(15), param_name: 'users_page') do
    table_for(collection) do |cr|
      column(I18n.t("cr.start")) { |cr| I18n.l cr.start, format: :raw }
        #other columns...
     end
   end
 end
Bricky answered 27/12, 2012 at 14:32 Comment(3)
not working for me, says undefined method .per for activerecord relation objectGodfree
@Godfree - it's probably because you're using will_paginate in your project? If so, have a look at this comment: github.com/activeadmin/activeadmin/issues/…Heard
this is such a beautiful solution. great great great !Kingcraft

© 2022 - 2024 — McMap. All rights reserved.