Rails controller design pattern for dashboard

AdamNYC picture AdamNYC · Sep 23, 2011 · Viewed 7.8k times · Source

I would like to have a Dashboard page that gather information from multiple models to a summary view (without its own model). How should I go about it the Rails way? Should I create a dashboard_controller with just index action?

Thank you.

Answer

Marian Theisen picture Marian Theisen · Sep 23, 2011

just think a little less Rails. i'm assuming your dashboard contains mainly widgets? i.e. some self contained elements, whatever? why not make those widgets your model (not AR-based). encapsulate all your logic and data retrieval in your widget model(s). so now if you want to go RESTful, you could for example make a WidgetsController with an index action as dashboard. in a second step you could use your show action to provide JSON data for each widget for AJAX parts of your dashboard. etc.

just remember REST is not necessarily == CRUD, and a Model doesn't need to be a ActiveRecord model. Rails forces you into the MVC pattern, which is a great thing, and makes even poorly written Rails apps better than a lot of PHP mayhem out there, but sometimes we tend to forget that there are a lot of possibilities outside of ActionController + ActiveRecord etc.