views:

1791

answers:

4

My homepage (or welcome page) will consist of data from two models (lets call them authors and posts). I am new to rails and not sure what is the best way to accomplish this.

Should I create a new controller called welcome which gathers data from the authors and posts and then display them in the welcome index view? Or should I have a welcome view under the post model which also gets data from authors? Or any other way to accomplish this?

I understand how to do all this technically but just unsure what is the best practice method using the rails framework.

A: 

Create a new controller named as appropriately as you can. SummaryController? StartController? DailyFrontPageController? You'll have an idea.

Not only that, I'd seriously consider creating a new Model, not ActiveRecord-based, that collects the information from your Author and Post models (or whatever their real names are) for presentation in your view. The alternative is to assemble the data in the controller, which will almost certainly be messy - it was every time I tried it, and I tried it a lot. A separate model seems to end up a lot tidier.

If the processing is relatively straightforward, why not try building the data in the controller first, then wrap the output in a Struct, then replace the Struct with a real class and move the construction there, refactoring all the way. It shouldn't add too much to the total time (most of the code can be reused) and you'll get a good idea of what works best for you.

Mike Woodhouse
+8  A: 

The question is, is your home page just a landing page or will it be a group of pages? If it's just a landing page, you don't expect your users to hang around there for long except to go elsewhere. If it's a group of pages, or similar to an existing group, you can add an action to the controller it's most like.

What I've done for my current project is make a controller named Static, because I need 3 static pages. The home page is one of these, because there isn't anything to see or do except go elsewhere.

To map a default route, use the following in routes.rb:

# Place at the end of the routing!
map.root :controller => 'MyController', :action => :index

In my case this would be:

map.root :controller => 'static', :action => :index

If you wish to, you could create a controller just for this home page. I'd call it main, or something that you can remember which relates to the home page. From there you can get your data and your models and defer to the output view.

class MainController < ApplicationController
  def index
    @posts = Posts.find(:all, :limit => 10, :order => 'date_posted', :include => :user)
  end
end

Assuming you have your model relationships defined correctly, the template to match it will be very simple.

Good luck, hope this helps.

The Wicked Flea
+2  A: 

The best practice would be your first suggestion. Create a 'welcome' controller and call the records from whichever models you want. Have a root route point to that controller. Very clean and proper.

allesklar
+2  A: 

I asked myself something like this when I first started Rails. Here's what you need to know:

  • Models are not necessarily directly related to controllers and views.

That is, a particular controller/view combination can work with as many models as you need to generate that particular page.

The purpose of the controller is to prepare the dataset you need to display, irrespective of what models are used to store that data.

The purpose of the view is to then display that data in the most appropriate way.

In other words, controller/view combinations are never 'under' a particular model. They use models, but are not under them in any hierarchical relationship. In fact, they are peers to whatever models they use.

I think the confusion comes from the scaffold generator example found in AWDR and other introductory texts, like:

ruby script/generate scaffold model controller

I know that this implied relationship between model and controller/views confused me for a bit. But there is no strict relationship, really. If there were, then it would be very difficult to do anything complicated with the MVC approach. And clearly, that is not the case.

Hope this helps.

-- John

John