SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    $books++ == true matsko's Avatar
    Join Date
    Sep 2004
    Location
    Toronto
    Posts
    795
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Model, View, Controller?

    I am trying to get acquainted with the ROR framework. The language is very useful, I enjoy coding it, but I cannot seem to understand the framework.

    What exactly is the Model, View, Controller system?
    I can't believe I ate the whole thing

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

  3. #3
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just start coding. These names really aren't important now. You'll learn what they mean by doing. This is more productive than reading abstract descriptions.

  4. #4
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think it is worth understanding what the three are for as you can put the same code in different places. You can load data into an object within a controller, model or view. Choosing the right one will make your code simpler and easier to debug. It is possible to use the three poorly and therefore learning by doing can lead you in the wrong direction if your doing is poor.

    The simplest component is the view. This is simply the component that controls how your information will displayed.

    The model defines your data objects. It controls the retrieval of data from your database, and conversion of that data into objects that Ruby can then manipulate.

    The controller manages the information flow from the model to the view. It defines which model objects are retrieved and how many, and then which view will be used to display the information.

    A controller also takes input from the browser and makes decisions based on that information. The input is usually via the params object that holds post and get data.

    So if you have an application that store information about people:

    The model will define the person object. If you need to add an extra characteristic to a person object, this is the place to do it. For example, you may store first_name and surname as separate fields in the database, but also need a full_name. Rather than storing an extra field, you create a method called full_name:

    Code:
    def full_name
      first_name + ' ' + surname
    end
    You need this to alter the person object (to produce person.full_name) and therefore you add this to the model.

    The controller defines which people objects will be retreived and which view they will be displayed on. So for example, if you want to list all people it is in the controller that you define:

    Code:
    @people = Person.find(:all)
    To create all the person objects in the database. It will then select the view to use to display this information:

    Code:
    render action => 'list'
    Though Rails assumes that if the controller method is called list, that the view to render will also be called list. So in fact all you need to do is:

    Code:
    def list
      @people = Person.find(:all)
    end
    The View then creates the display code. So typically the html code that defines where on the page information will be displayed. So if you need to display how many people you have in your database your code may include something like:

    Code:
    <p>There are <&#37;= @people.length -%> people in the database</p>
    Which takes the information passed to it from the controller and inserts it into the view html.


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
  •