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.

AdamNYC
  • 19,887
  • 29
  • 98
  • 154

5 Answers5

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.

Marian Theisen
  • 6,100
  • 29
  • 39
  • Thanks Marian. Could you elaborate on why deviate from Rails using widget would be a good option? – AdamNYC Sep 24 '11 at 01: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 '11 at 02: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. – Marian Theisen Sep 24 '11 at 05: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". – Marian Theisen Sep 24 '11 at 06: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.

Devin M
  • 9,636
  • 2
  • 33
  • 46
  • 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 '11 at 01: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 '11 at 02:10
  • Thanks Devin. I'll try your approach. – AdamNYC Sep 24 '11 at 02:26
  • Hi Devin, I would like to invite you to join a follow up discussion on implementing presenter here: http://stackoverflow.com/questions/7537180/how-to-pass-argument-to-delegate-method-in-rails . Thank you. – AdamNYC Sep 24 '11 at 05:22
  • But in the case where the a single page aka Dashboard page needs to be displayed just after login in of the user, which action or which controller should be defined? – yatish mehta Dec 12 '12 at 07:21
  • @yatishmehta I would recommend creating a new controller for the dashboard and using the presenter pattern to keep things clean. Also, you may want to add detail and ask a full question so other people can answer too. Good luck! – Devin M Dec 12 '12 at 18:10
  • 3
    "Use a libray" is not a great answer in my opinion. – Marek Maurizio Jun 21 '14 at 16:22
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.

ontk
  • 942
  • 1
  • 10
  • 24
  • 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? – Kelsey Hannan Feb 10 '17 at 06: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.

Marek Maurizio
  • 1,073
  • 2
  • 11
  • 22
  • this is in no way a solution to his problem, all you are describing is the rails scaffolding... – Robbie Guilfoyle Mar 13 '14 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. – Marek Maurizio Mar 14 '14 at 16:00
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.

Ben
  • 6,857
  • 26
  • 23
  • 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 '11 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 '11 at 05: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 '12 at 20:36