9

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.

1

5 Answers 5

4

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.

4
  • Thanks Marian. Could you elaborate on why deviate from Rails using widget would be a good option?
    – AdamNYC
    Sep 24, 2011 at 1:29
  • I think what hes trying to say is to encapsulate the behavior of your application in the model which would be a bad idea. Business logic can go in the model however if it is unrelated to data storage and retrieval then you need to place it in the controller.
    – Devin M
    Sep 24, 2011 at 2:05
  • i strongly disagree. first of all, data persistence is a layer of your models, it's NOT what defines a model. you can have a lot of models without persistence. and you should ALWAYS encapsulate business logic into your model, just follow good OOP principles. just because rails gives us a good but opinionated way of doing things, it doesn't mean we have to forget all object orientation. the purpose of a WidgetsController would be to load and render a/several widget(s). it doesn't need to know what the widget is. querying 20 different tables in this controller would be awkward. Sep 24, 2011 at 5:59
  • that said, if you just put that 20 queries into a function in your Widget model, you have gained nothing. But you could build an appropriate class structure around it, thinking in objects and relations, not "what data do i need from what table". Sep 24, 2011 at 6:02
1

What you would want is to take a look at the administration frameworks and see if that solves your needs, I like Typus and RailsAdmin Otherwise you would want to take a look at the presenter pattern and how it applies to Rails and your application. A dashboard basically would only interface with your existing models and controller logic since you dont want to end up with a situation where you have two sets of logic for each model. While the blog I linked to is from 2007 you should be able to pull some useful information off of it and get the idea of what you need to do.

If you have any questions feel free to ask.

7
  • Thanks a lot, Devin. I only know about Presenter in the context of creating objects for multiple models at the same time. Could you explain its potential benefits in my situation (mainly display)?
    – AdamNYC
    Sep 24, 2011 at 1:26
  • Well you want to interface with a large portion of your application so this pattern is designed to keep code DRY and delegate responsibilities. Typically you keep the same CRUD actions and delegate them out, I would also make use of partials so you share the view code when you can.
    – Devin M
    Sep 24, 2011 at 2:10
  • Thanks Devin. I'll try your approach.
    – AdamNYC
    Sep 24, 2011 at 2:26
  • Hi Devin, I would like to invite you to join a follow up discussion on implementing presenter here: stackoverflow.com/questions/7537180/… . Thank you.
    – AdamNYC
    Sep 24, 2011 at 5:22
  • 3
    "Use a libray" is not a great answer in my opinion. Jun 21, 2014 at 16:22
0

Should I create a dashboard_controller with just index action?

I prefer to put pages that do not perform CRUD upon a model (such as dashboards) in the application_controller. No need to create a dashboard_controller.

3
  • 1
    This can cause a lot of clutter in application_controller. Especially if ajax gets involved, which is likely with a dashboard, or developing an atypical application.
    – providence
    Sep 23, 2011 at 22:51
  • @providence I completely agree. This answer was only to address OP's situation where he was considering "just an index action," where he would display "summary" info from "multiple models."
    – Ben
    Sep 24, 2011 at 5:53
  • I use a dashboard controller where we have multiple dashboards, so index action lists the available dashboards and show action is the dashboard itself.
    – cpuguy83
    Aug 30, 2012 at 20:36
0

Would that be a use case for the Presenter pattern?

I am about to implement a ddashboard and that's the best design I think that will lead to a maintainable code base.

1
  • By instinct I flinch in horror when I see recommendations to create controller actions that don't map to RESTful conventions (index, new, create, show, edit, update, destroy). Is there a way to build these kind of pages following the REST approach instead? Feb 10, 2017 at 6:50
0

My solution is to run

rails g controller Dashboards

then manually create a app/models/dashboard.rb model which contents can be as little as

class Dashboard end

Of course the model you can put all the logic you need fishing data from other models.

Back to the controller, you create just the index action. Lastly, you manually create the app/views/dashboards/index.html.erb file with whatever content you need. Add the route and you're good to go. If you pay attention to details you can change the url from /dashboards to /dashboard. That's it, i think it's very Rails-y.

2
  • this is in no way a solution to his problem, all you are describing is the rails scaffolding... Mar 13, 2014 at 23:33
  • 1
    Scaffolding in Rails means to generate all the MVC stack automatically, I don't see it in my answer. The solution is exactly how I implemented it for a similar requirement and it works indeed. Just because it's basic it does not mean it's wrong. Since the OP did non specify his level of Rails a basic solution is acceptable. I don't find the accepted answer of "use a big bloated framework and hope it works" any better. Mar 14, 2014 at 16:00

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.