views:

310

answers:

2

In the admin section of a website i am building i would like to put together a dashboard page, or 'quick look' type page where the most recent changes/additions/etc in several different areas can be viewed.

I was thinking the best way to do this would be to use partials and have each partial contain the markup for the data i need to display.

My question is, is this the best approach? and if so, how do i separate the logic and presentation of these partials? For instance how do i put the logic in the dashboard controller, but keep the presentation in the partial rhtml?

+2  A: 

Partials are definitely the way to go, especially when you can pass in arbitrary data into them to make them do stuff. To answer your logic separation issue specifically, you would use:

<%= render :partial => "name_of_partial", :locals => { :some_var => @data_from_model } %>

Then, inside your partial, you'd have access to @data_from_model via the some_var variable.

jcapote
+1  A: 

The controller should be responsible for retrieving the data you need to present (and, usually implicitly, passing it to the view). Build no HTML here.

The partials, of course, are where you figure out presentation - you can make it a list, graph, Google Visualization widget, a single number in 40-point font, whatever.

<%= render :partial => 'new_users_last7days', ;locals => { :new_user_count => @new_users.size } %>
bradheintz
I should have pointed out that if you have scoped data of like "new users in the last 7 days", that should be handled at the model level with a named scope.
bradheintz