Welcome / Ruby on Rails Homepage - Best Practice

My home page (or welcome page) will consist of data from two models (allows you to call them authors and posts). I am new to rails and not sure if this is the best way to achieve this.

Should I create a new controller called welcome, which collects data from authors and posts, and then displays them in a welcome index view? Or should I get a welcome look as part of a publication model that also receives data from authors? Or any other way to do this?

I understand how to do all this technically, but I just don’t know which best practice method is used with the rails framework.

+77
ruby ruby-on-rails
Dec 08 '08 at 14:40
source share
7 answers

The question is, is your homepage a landing page or will it be a group of pages? If this is just the landing page, you don’t expect your users to hang out there for long, except elsewhere. If this is a page group or similar to an existing group, you can add the most action to it.

What I did for my current project is to create a controller called Static , because I need 3 static pages. The main page is one of them, because there is nothing that could be seen or done, except to go to another place.

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

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

In my case it will be:

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

If you want, you can only create a controller for this homepage. I would call it core or something that you can recall that relates to the home page. From there you can get your data and your models and put them off until the release.

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

Assuming your model relationships are defined correctly, the template matching it will be very simple.

Good luck, hope this helps.

+51
Dec 08 '08 at 15:02
source share

There seems to be no single best practice.

(1) It seems that the standard config/routes.rb offers to process the root page (or home page / welcome page) using welcome#index . If you should be guided by this, then to generate the corresponding welcome#index controller / action, you can use the following command:

 rails generate controller Welcome index 

Then in config/routes.rb you can delete the GET route ( get "welcome/index" ) automatically added by the generator and put the root root route root 'welcome#index' (or root :to => 'welcome#index' in Rails < 4 ) at the top of the file, because it is likely to be your most popular route and should be found first.

Also remember to remove public/index.html in Rails < 4 .

(2) The official Ruby on Rails routing guide uses the PagesController . In fact, it offers pages#main , although it is more logical for me to switch to pages#home (because "home page" is a universal term / concept). In addition, this controller can handle other page-oriented actions, such as pages#about , pages#contact , pages#terms , pages#privacy , etc.

(3) The Ruby on Rails manual comes with static_pages#home and static_pages#help , etc. Although I don't like the idea of ​​designating this controller as “static”. These pages are likely to still have some dynamic aspects, especially the homepage!

(4) Although it’s not discussed how to handle the home page, RailsCast # 117 on semi-static pages offers another set of display-only resources.

I feel preference 1 and / or 2. In the scenario "and" you can use welcome # index and pages # about etc., while in the scenario "or" you can use pages # home, pages # about, etc. If I had to choose, I would choose option 2 just because you have less code. And by the way, 2 and 3 are pretty much the same except for the word "static."

+117
Jun 12 '12 at 19:52
source share

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

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

That is, a specific combination of controllers / views can work with as many models as you need to create this particular page.

The purpose of the controller is to prepare the data set that you need to display, regardless of which models are used to store this data.

The purpose of the presentation is then to display this data in the most appropriate way.

In other words, controller / view combinations are never “under” a particular model. They use models, but are not hierarchical under them. In fact, they are peers for all the models that they use.

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

ruby script / create a forest model controller.

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

Hope this helps.

- John

+28
Dec 08 '08 at 18:23
source share

Best practice would be your first suggestion. Create a “welcome” controller and call up entries from any models you want. You have the root point of the route to this controller. Very clean and correct.

+11
Dec 08 '08 at 18:03
source share

Note that in Rails3, the right way to handle this is to add the following line at the end of the route.rb file:

 root :to => "welcome#index" 

and delete public / index.html.erb.

Also note that the welcome index # corresponds to the index action in the WelcomeController, and the code from the Wicked Flea answer will look like this:

 class WelcomeController < ApplicationController def index @posts = Posts.find(:all, :limit => 10, :order => 'date_posted', :include => :user) end end 
+9
Jun 26 '11 at 19:32
source share

This answer applies to Rails 3.2.1.

First, configure the controller for the pages, named, for example, static :

 $ rails generate controller static 

In the app/controllers/static_controller.rb :

 class StaticController < ApplicationController def index end end 

Create a new View app/views/index.html.erb file

And finally configure your config/routes.rb :

 MyApp::Application.routes.draw do match 'home', :to => "static#index" root :to => "static#index" end 

This will cause both /home and / go over to what you put in the newly created View file.

+9
Oct. 20 '12 at 2:30
source share

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

Not only this, I would seriously think about creating a new model, and not based on ActiveRecord, which collects information from your Author and Post models (or how their real names are) for presentation in your presentation. An alternative is to collect data in the controller, which will almost certainly be messy - this was every time I tried it, and I tried a lot. It seems that a separate model was much more accurate.

If the processing is relatively simple, why not try to create the data in the controller first, and then wrap the output in Struct, and then replace Struct with the real class and move the construct there, refactoring completely. It should not increase the overall time too much (most of the code can be reused), and you will get a good idea of ​​what works best for you.

+6
Dec 08 '08 at 15:02
source share



All Articles