views:

170

answers:

0

I'd like to be able to DRY-ly and intelligently set up my Rails controllers so that I can effortlessly use paginated tables of data on various pages. Let me clarify. I already use will_paginate + a custom plugin that I wrote which acts as a table-view creator. This combination works perfectly for "index" actions which simply show one table of data. I can paginate through the data with AJAX and all is well. However, I would like to have certain pages which show multiple tables of data which should be able to be AJAX-paginated through as well. For instance a home page which shows a table of friends, a list of recent activity, etc. I would hope to house the table-creation code for each table within the corresponding controller for that model. So the FriendsController might look something like this:

class FriendsController < ApplicationController
  ...
  def create_list
    # This will can be called by the HomeController (or view) to show the list table.
    # ...Return a table with headers, data, and pagination links
  end

  def list
    # This is the action called by AJAX to update the table.
    respond_to |format|
      format.js { create_list }
    end
  end
end

Of course, I'm not set on the format of this approach. I could make create_list a class method, but it would still involve one controller (or view) calling a method on another controller. I could put the create_list into a separate module and include that in both FriendsController and HomeController, but that seems like it would be a pain to maintain, since the code to display the friends table is now it 2 different places.

What is the best practice for a situation like this where controllers must communicate and share code?