Reputation: 2363
I have a "dashboard" type page that show records from many different Models (a Parent Model with many associated Child Models) and uses logic that I currently keep in the Parent Controller. I am new to rails and wondering if it would be better to create a separate Dashboard controller or and keep the Parent Controller just for add/edit/index/destroy of the Parent records.
Is there a best practice for this type of page?
I am using Rails 4...if that matters.
Upvotes: 0
Views: 214
Reputation: 2246
You're right that this is more a RESTful dilemma and not related to any specific framework. It depends on the role of the Parent and Child Models and whether the dashboard 'exposes' any other additional resources. For the sake of simplicity I'm going the pretend the parent model is an Author and the child model is a Book.
If the dashboard only contains a collection of children related to a parent model you could consider using Nested Resources. E.g URLs similar to /authors/{author_id}/books
(and alias to /dashboard
if required):
# routes.rb
resources :authors do
resources :books
end
The logic then belongs in the BooksController
:
class BooksController < ApplicationController
def index
if params[:author_id]
@author = Author.find(params[:author_id])
@books = @author.books
return render '/books/author_index'
else
# Code for simple top level RESTful resource. E.g '/books'
# ...
@books = Book.all
return render 'index'
end
end
end
However if your dashboard is more broad and touches multiple domains, you could consider the dashboard as a resource in itself (e.g it's own DashboardController
) as described in this answer.
Upvotes: 1