MVC

(Model-view-controller)





OverView

  • MVC (Model-View-Controller)
  • Software Architectural pattern for implementing user interfaces
  • Divides the work of an application into three separate but closely cooperative subsystems.
  • This seperation helps you manage complex applications
  • Divided into 3 components

  1. Model:  responsible for maintaining data
  2. View: responsible for displaying the data to the user
  3. Controller: controls the interactions between the M and V


Overview


Model

  • Retrieves data  from a database
  • Stores data to a database
  • Responds to instructions from the controller to update itself 
  • Possibly updates itself from view as well ( But not recommended)
  • The bulk of your application’s business logic will be concentrated in the models

View

  • Views represent the user interface of your application
  • Views are often HTML files with embedded code which perform tasks related solely to the presentation of the data
  • Handles the job of providing data to the web browser or other tool that is used to make requests from your application

Controller

  • The controller is the decision maker and the glue between the model and the view
  • Responsible for 
  1. processing the incoming requests from the web browser 
  2. interrogating the models for data
  3. passing that data on to the views for presentation

 

RUBY ON RAILS



Introduction

  • Creator of ROR: David Heinemeier Hansson(DHH)
  • Open-source
  • Web application development framework written in the Ruby language
  • Full-stack framework for developing  web applications according to the MVC pattern
  • Productivity: Write less code and get it done
  • Where development is fun

ROR Philosophy

  • Don't Repeat Yourself(DRY)
    • DRY : "Every piece of knowledge must have a single and unambiguous representation within a system." 
    • By not writing the same information over and over again, our code is more maintainable, extensible, and less buggy.
  • Convention Over Configuration
    • Rails has opinions about the best way to do many things in a web application, and defaults to this set of conventions, rather than require that you specify every minute details through endless configuration files.

Architecture



  • Rails applications are implemented using the Model-View-Controller (MVC) architectural pattern
    • Model – ActiveRecord
    • View – ActionView
    • Controller - ActionController

Directory Layout

  • Rails applications have a common directory structure 


/app -  the MVC core
   /controllers
   /helpers - provides extra functionality for views
   /models
   /views/nameofcontroller - templates for controller actions
  /assets
  /mailers – mailer classes to send mails 


Rails Environments


  • Rails runs in different modes, depending on the parameters given to the server on startup.  
  • Each mode defaults to it’s own database schema
    • Development (verbose logging and error messages)
    • Test
    • Production

Creating Rails Application

  1. rails new app_name –database=mysql
  2. cd app_name
  3. rake db:create
  4. rails s

Models

  • ORM (Object Relational Mapping) : ActiveRecord
  • Generator 
    • Rails g model Product name:string quantity:integer
  • Callbacks
  • Validations
  • Finders
  • Querying
  • Asscociations

VIEW


  • erb , haml
  • partials
  • View helpers
  • Form helpers
  • Route helpers

CONTROLLER

  • Generater
    • rails g controller books
  • Filters
    • before_filter
    • after_filter
    • arround_filter
  • Render Views
  • respond_to

ROUtes

- One can check all routes using "rake routes"
  • Mapping urls to controller and actions
    •  get '/patients/:id', to: 'patients#show', as: 'patient'
  • Resources
    •  resources :photos
  • Namespace
    • namespace :admin do
        resources :posts, :comments
      end

ROUTES

  • Nesting routes
    • resources :magazines do
        resources :ads
      end
      
      
  • Limiting REST routes (7 default routes index, new, create, edit, update, destroy, show)
    •  resources :comments, only: [:index, :new, :create]

MVC in Detail


Migration


  • Create table, change column, add and remove columns etc
  • Generator
    • rails g migration add_column_name_to_table_name column_name:datatype
  • Running migration
    • rake db:migrate
    • rake db:migrate:down VERSION=timestamp_version
    • rake db:migrate:up VERSION=timestamp_version
    • rake db:migrate:status
    • rake db:rollback

References

  • http://www.railstutorial.org/book/beginning
  • http://en.wikipedia.org/wiki/Model-view-controller
  • http://guides.rubyonrails.org/




Questions ?

Contact


  • Skype id: /dattdongare
  • Email id: datt@cart91.com
  • Phone: 8446353453



    Thank you!

    Rails MVC

    By Datt Dongare

    Rails MVC

    What is MVC? MVC framework - Rails

    • 774