SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Zealot HenriIV's Avatar
    Join Date
    Jun 2004
    Location
    France
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Controller homepage?

    Hi,

    Is it a best practice to add a HomePage controller as it only will call the last 5 or top 10 method from other controllers?

    Example:
    I have an hotel class with a "last 5 list" method to display... the last 5 hotels added
    ditto for the "top 10 special offer" tickets
    and the homepage just gather all these bargain sales

  2. #2
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Last 5 or Top 10 methods should be in your models, not controllers. But yeah, you can do that. Example
    Code:
    class HomepageController < ApplicationController
      def index
        #assuming you have a top 10 method in your hotel model object
        @hotels = Hotel.find_top_10
      end
    end
    Then code a loop in your view to display the top 10 list.

  3. #3
    SitePoint Zealot HenriIV's Avatar
    Join Date
    Jun 2004
    Location
    France
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks Vinnie.

    PHP Code:
    Last 5 or Top 10 methods should be in your modelsnot controllers
    That's what I thought but the scaffold script generate lists in the controller ?!

  4. #4
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by HenriIV
    That's what I thought but the scaffold script generate lists in the controller ?!
    Try not to take everything single thing in scaffold code as best practice - its just something quick and easy to get you up and running. You'll find the more experienced you get with Rails the less you use it (I don't really use it at all).

  5. #5
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by HenriIV
    That's what I thought but the scaffold script generate lists in the controller ?!
    Yes the list is accessed in your controller, but not generated in a method there. If you're going to create a custom set of data you should pull it from the relevant model, then access it in the controller like I showed above.

  6. #6
    SitePoint Zealot HenriIV's Avatar
    Join Date
    Jun 2004
    Location
    France
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, thanks, it's getting clearer now


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •