Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Creating a Rails Website - Implementing multiple models on one page.

I've got more comfortable with Rails and want to design something more than a blog site.

What I want to do is build something that has a dashboard style 'home page' - i.e. a number of dynamic sections based on different models.

To create this page, would I create a whole controller, model and view?

A quick example that everyone should recognize is the Facebook profile page (not what I'm trying to create!), which has images, posts, about, etc all in once place.

Another (fantastic) example is the treehouse application home-page, which shows reward videos, points, your latest track etc.

From what I know of rails, the data and logic for these sections would all come from different models, so maybe I just need one controller called home.rb that draws upon my multiple models? A root-path request would be routed to the home.rb controller.

Then I would create a view called home.html.erb and present the different arrays and variables provided by the controller?

Any advice / resources would be very much appreciated!

Thomas

2 Answers

Brandon Barrette
Brandon Barrette
20,485 Points

If you are creating a dashboard, I recommend creating a dashboard controller and a dashboard view. You don't need a model, since as you said, you are pulling a bunch of other models together to be displayed (pictures, statuses, etc).

Then in your dashboard controller, you can just initiate a bunch of instance variables to be be displayed in the view. For example:

#dashboard controller
def index
   @pictures = current_user.pictures
   @statuses = current_user.statuses
end

Then in your view, you can loop over them all to be displayed in on one page:

#dashboard view
<%= @pictures.each do |pic| %>
 #however you want to display them
<% end %>

Note that you could also just add a new method to your users_controller and call it dashboard. I have a dashboard controller because I have more than just a dashboard, I also have data, and leaderboards, and all that in my app that I put under that (since they all come from different models).

Hope that helps!

Is your site live? It would be great to take a look!

Nice! Thanks for clarifying :)