CSC/ECE 517 Fall 2011/ch4 4d mt

From Expertiza_Wiki
Revision as of 03:17, 15 October 2011 by Mndsouza (talk | contribs) (Created page with "''' The CookBook Application (Lecture 9)''' ==Introduction== Lecture 9 talks about creation of the Cookbook application. This is a basic web application which demonstrates the p...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

The CookBook Application (Lecture 9)

Introduction

Lecture 9 talks about creation of the Cookbook application. This is a basic web application which demonstrates the power and ease of use of Ruby on Rails for web development. This article first introduces Rails and its key features and then takes us through the steps for creation of the Cookbook application.


Ruby on Rails

Ruby on Rails, is an [open source web application framework http://en.wikipedia.org/wiki/Web_application_framework] for the[ Ruby programming language http://en.wikipedia.org/wiki/Ruby_(programming_language)]. It was first released to the public in July 2004 and is now become the standard Web-development framework for Ruby. The Rails framework is built on the following 2 key principles:

[ http://en.wikipedia.org/wiki/Convention_over_configuration Convention over Configuration]

Convention over Configuration states that the conventional part of the program will be specified by the framework itself. So the programmer needs to concentrate only on the core logic of the program and the unconventional aspects of the program .Rails implements this by providing features like Rails scaffold and Rails generate which help us generate the basic framework of our application with minimal coding.

[ http://en.wikipedia.org/wiki/Don't_repeat_yourself Don't repeat yourself]

Don't repeat yourself states that information should not be repeated. This applies to both data as well as code. Rails allows us to specify relational databases and queries in the form of object models using migrations and ActiveRecord. This helps us port applications across databases without any changes in code

[ http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model/View/Controller Frame work]

All Rails applications are implemented using the Model/View/Controller (MVC) architecture. MVC architecture is a software engineering concept that allows us to isolate the application logic from the user interface. This allows us implement these separate the development ,testing and maintainence of the application logic and the UI. Key components of MVC architecture include:

  1. Models : These are objects that map to real world entities. They contain the application logic and provide persistence storage by interfacing with the database. Models in Rails are implemented using ActivedRecord objects.
  2. Views: Views provide a human interface(GUI) for the model. Views in Rails are implemented using embedded ruby (.html.erb) files
  3. Controllers: Controllers process the user actions and help redirect to the appropriate model ,build a user response and update the View.

The typical user interaction on a Rails application happens as follows:

  1. The user responds to the UI (ex : clicks a button)
  2. The router routes the action to the appropriate controller and action in it
  3. The controller calls the model. The model may changes state depending on the call(Ex: The model may update its values based on user entered data
  4. A view queries the model and uses its data to build a response .
  5. The view updates the UI with the view and waits for further user interaction.

[ http://en.wikipedia.org/wiki/Create,_read,_update_and_delete CRUD]

CRUD stands for create, read, update and delete (CRUD) the four basic functions of persistent storage. CRUD also relates to the UI for most applications. Almost every model obect must have following features and a UI to acess them:

  1. Create or add new entries
  2. Read, retrieve, search, or view existing entries
  3. Update or edit existing entries
  4. Delete existing entries

Due to the fundamental nature of these features, Rails provides readymade implementation for them in the form of Scaffolding. Scaffolding creates migrations,model,controller and View by only specifying the attributes of the model. Once the CRUD functionality has been generated by Rails, it can be modified to suit requirements.

CRUD is also relevant at the user interface level of most applications. For example, in address book software, the basic storage unit is an individual contact entry. As a bare minimum, the software must allow the user to: Without at least these four operations, the software cannot be considered complete. Because these operations are so fundamental, they are often documented and described under one comprehensive heading, such as "contact management" or "contact maintenance" (or "document management" in general, depending on the basic storage unit for the particular application).

Routing in Rails

Rails provides recourseful routing. This allows us to quickly specify the common routes for a resourceful controller( ie one that implements the CRUD functionality). Since CRUD featuresare common across entities, a single line in routes.db ex: resources :photos can allow us to specify 7 routes for the application.

HTTP Verb Path action used for
/photos | index | display a list of all photos /photos/new | new | return an HTML form for creating a new photo /photos |create| create a new photo /photos/new| new| return an HTML form for creating a new photo| /photos |create |create a new photo| /photos/:id |show |display a specific photo| /photos/:id/edit |edit |return an HTML form for editing a photo /photos/:id| update |update a specific photo /photos/:id |destroy |delete a specific photo|}

Resource routing allows you to quickly declare all of the common routes for a given resourceful controller. Instead of declaring separate routes for your index, show, new, edit, create, update anddestroy actions, a resourceful route declares them in a single line of code.

resources :photos creates seven different routes in your application, all mapping to the Photos controller:


Object relational Mapping

Rails handles Object Relational mapping using Active Records. ActiveRecors allow a Railds program to deal with objects and itself handles the mapping of these objects to relational databases. In Active Record,

  1. Database tables correspond to Rails classes
  2. Database records (rows) correspond to Rails objects.

ActiveRecord also allow us to query the database in the object domain by using the Active Record Query INterface