Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Welcome/home page in Ruby on Rails - best practice

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.

like image 365
Espen Avatar asked Dec 08 '08 14:12

Espen


People also ask

What is the best practice for Ruby on Rails?

Ruby on Rails Best Practice: Keep Your Code DRY Ruby on Rails' object-oriented principles are built to help you avoid duplicating code throughout your web application. Whenever possible, a great Ruby on Rails tip is to re-use as much code as possible instead of repeating similar code in many places.

Is Ruby on Rails still relevant 2022?

Ruby's and Ruby on Rails' Overall Popularity Although way behind main contenders, such as PHP or Python, Ruby still makes the cut for the 20 most popular programming languages list in 2022. The 2022 edition of Stack Overflow Annual Developer Survey also places RoR in a similar spot.

What are RESTful routes in Rails?

In Rails, a RESTful route provides a mapping between HTTP verbs, controller actions, and (implicitly) CRUD operations in a database. A single entry in the routing file, such as. map.resources :photos. creates seven different routes in your application: HTTP verb.


1 Answers

There doesn't seem to be a single best practice.

(1) The standard config/routes.rb file seems to suggest that the root page (or home/welcome page) should be handled by welcome#index. If you were to be guided by that, 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 remove the GET route (get "welcome/index") automatically added by the generator, and place the root route root 'welcome#index' (or root :to => 'welcome#index' in Rails < 4) at the top of the file, because it will probably be your most popular route and should be matched first.

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

(2) The official Ruby on Rails routing guide uses PagesController. It actually suggests pages#main, though to me it makes more sense to go with pages#home (because "homepage" is the ubiquitous term/concept). Additionally, this controller can handle other page-oriented actions such as pages#about, pages#contact, pages#terms, pages#privacy, etc.

(3) The Ruby on Rails Tutorial, goes with static_pages#home and static_pages#help, etc., though I don't like the idea of denoting this controller with "static". These pages will still likely have some dynamic aspects to them, particularly the homepage!

(4) Though it does not discuss how to handle a homepage, RailsCast #117 on Semi-Static Pages suggests yet another set of approaches to show-only resources.

I feel preference toward 1 and/or 2. With the "and" scenario, you could use welcome#index and pages#about, etc., whereas with the "or" scenario, you could use pages#home, pages#about, etc. If forced to choose, I would go with option 2 just because you end up with less code. And btw, 2 and 3 are pretty much the same, apart from the word "static".

like image 74
user664833 Avatar answered Oct 02 '22 03:10

user664833