<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Mndsouza</id>
	<title>Expertiza_Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Mndsouza"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Mndsouza"/>
	<updated>2026-10-04T17:22:26Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=54202</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4d mt</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=54202"/>
		<updated>2011-10-27T18:05:15Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: /* External Links */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''' The CookBook Application (Lecture 9)'''&lt;br /&gt;
=Introduction=&lt;br /&gt;
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.Rails includes a simple light weight application server [http://en.wikipedia.org/wiki/WEBrick Webrick] and a build system [http://en.wikipedia.org/wiki/Rake_(software) Rake] which makes development on rails easy.&lt;br /&gt;
&lt;br /&gt;
==Ruby on Rails==&lt;br /&gt;
Ruby on Rails, is an [http://en.wikipedia.org/wiki/Web_application_framework open source web application framework] for the [http://en.wikipedia.org/wiki/Ruby_(programming_language) 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:&lt;br /&gt;
===Convention over Configuration===&lt;br /&gt;
[http://en.wikipedia.org/wiki/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.&lt;br /&gt;
&lt;br /&gt;
===Don't repeat yourself=== &lt;br /&gt;
[http://en.wikipedia.org/wiki/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. Also in ActiveRecord there is no need to specify attribute names since it is picked up from the database tables itself.&lt;br /&gt;
&lt;br /&gt;
==Model/View/Controller Framework==&lt;br /&gt;
All Rails applications are implemented using the [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller 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.&lt;br /&gt;
Key components of MVC architecture include:&lt;br /&gt;
# 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.&lt;br /&gt;
#Views: Views provide a '''human interface(GUI)''' for the model. Views in Rails are implemented using embedded ruby (.html.erb) files&lt;br /&gt;
#Controllers: Controllers process the '''user actions''' and help redirect to the appropriate model ,build a user response and update the View. &lt;br /&gt;
The typical user interaction on a Rails application happens as follows:&lt;br /&gt;
#The user responds to the UI (ex : clicks a button)&lt;br /&gt;
#The router routes the action to the appropriate controller and action in it&lt;br /&gt;
#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&lt;br /&gt;
#A view queries the model and uses its data to build a response .&lt;br /&gt;
#The view updates the UI with the view and waits for further user interaction.&lt;br /&gt;
&lt;br /&gt;
==CRUD==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Create,_read,_update_and_delete 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:&lt;br /&gt;
#Create or add new entries&lt;br /&gt;
#Read, retrieve, search, or view existing entries&lt;br /&gt;
#Update or edit existing entries&lt;br /&gt;
#Delete existing entries&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
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 &amp;quot;contact management&amp;quot; or &amp;quot;contact maintenance&amp;quot; (or &amp;quot;document management&amp;quot; in general, depending on the basic storage unit for the particular application).&lt;br /&gt;
&lt;br /&gt;
==Routing in Rails==&lt;br /&gt;
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: &lt;br /&gt;
resources :photos &lt;br /&gt;
can allow us to specify 7 routes for the application.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! HTTP Verb&lt;br /&gt;
! Path &lt;br /&gt;
! action &lt;br /&gt;
! used for&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods GET]   || /photos || index || display a list of all photos&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods GET] || /photos/new || new  || return an HTML form for creating a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods POST]   ||	/photos	||create||	create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods GET]	||/photos/new ||	new ||	return an HTML form for creating a new photo &lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods POST]	|| /photos	|| create	|| create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods GET]	|| /photos/:id	|| show	|| display a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods GET]	|| /photos/:id/edit	|| edit	|| return an HTML form for editing a photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods PUT]	|| /photos/:id ||	update	|| update a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods DELETE]	|| /photos/:id	|| destroy	|| delete a specific photo&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
resources :photos&lt;br /&gt;
creates seven different routes in your application, all mapping to the Photos controller:&lt;br /&gt;
&lt;br /&gt;
==Object relational Mapping==&lt;br /&gt;
Rails handles Object Relational mapping using Active Records. ActiveRecords allow a Rails program to deal with objects and itself handles the mapping of these objects to relational databases. In Active Record, &lt;br /&gt;
# Database tables correspond to '''Rails classes'''&lt;br /&gt;
# Database records (rows) correspond to '''Rails objects'''.&lt;br /&gt;
ActiveRecord also allow us to query the database in the object domain by using the Active Record Query Interface&lt;br /&gt;
&lt;br /&gt;
=Developing the Cookbook App=&lt;br /&gt;
We now will look into how to develop the cookbook application, and talk about how to get started with the same. &lt;br /&gt;
To get started, we first create a new rails project called say 'CookBookApp', which is done by simply running the command : &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails new CookBookApp&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Once this is run, a series of folders are created, like app, config, db, etc. In developing the cookbook app, we will mainly look into the app, config and db folders mainly.&lt;br /&gt;
==Scaffolding==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Scaffold_%28programming%29 Scaffolding] is a very powerful command that creates code to create, read, update and delete (CRUD) data from the database. In the Cookbook application, the first scaffold is to create categories. Each Category will then have multiple recipes, and the main functionality of our cookbook app would be done.&lt;br /&gt;
The scaffold command to create categories is done by simply giving the command :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails generate scaffold category name:string&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
As seen, this generates the model called category, a bunch of views, and a category controller.&lt;br /&gt;
It also generates a migration, to create a new table called categories (notice that rails called the table as categories, which is the plural of category automatically).&lt;br /&gt;
===DB Migration===&lt;br /&gt;
The scaffold command generates a lot of files, and also a migration in db\migrate with a name like '''&amp;lt;timestamp&amp;gt;_create_categories.rb'''. This migration contains the details of the table that has to be created called 'categories', so the next step is to run the migrations. Before we do this, however, if we start the rails server, we get an error mentioning that the table 'categories' doesn't exist. &lt;br /&gt;
Let us now run the migration using the command :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After the migration is run, we know that the categories table has been created, and now run the server.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails server&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Creating Categories===&lt;br /&gt;
Now, if we goto the default localhost:3000 page, we see one option : Create a new category, as shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:2.jpg]]&lt;br /&gt;
&lt;br /&gt;
We can click on it, and make a new category, lets say 'Main Course'&lt;br /&gt;
&lt;br /&gt;
[[File:3.jpg]]&lt;br /&gt;
&lt;br /&gt;
When clicking on a new category notice that the URL is '''localhost:3000/categories/new'''. So we are executing the new method of the categories controller. On clicking 'Create Category', a new category with the name 'Main Course' is created, and a flash notice saying 'Successfully Create' is shown. We can now see the category in our list of categories.&lt;br /&gt;
&lt;br /&gt;
[[File:5.jpg]]&lt;br /&gt;
&lt;br /&gt;
So, as we saw, with just a simple scaffold command and db:migrate, we have an application with the basic CRUD functionality.&lt;br /&gt;
&lt;br /&gt;
===Creating Recipes===&lt;br /&gt;
Now, similar to creating categories, we create recipes using the scaffold command. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails generate scaffold recipe title:string description:string instructions:text&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Once this is done, we again have to run a '''db:migrate''', so that the recipes table is in the database.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, the recipes have been created, and as seen in the recipe model, each recipe belongs to a category.&lt;br /&gt;
&lt;br /&gt;
==Relationships==&lt;br /&gt;
Relationships are always defined in the model. We first want to say that each recipe belongs to a category. This can be done in the recipe controller as shown below :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similarly, we want to add a detail mentioning that each Category can have many Recipes : a one to many relationship. To do this, we need to edit the Category model, and include the '''has_many''' relationship with Recipe. The Category model now looks like :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
  has_many :recipes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We also have to add a reference in the migration, to indicate that the recipes table references the categories table. This can be done in the '''&amp;lt;timestamp&amp;gt;_create_recipes.rb''' migration, and now it would look like :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateRecipes &amp;lt; ActiveRecord::Migration&lt;br /&gt;
  def change&lt;br /&gt;
    create_table :recipes do |t|&lt;br /&gt;
      t.string :title&lt;br /&gt;
      t.string :description&lt;br /&gt;
      t.text :instructions&lt;br /&gt;
      t.references :category&lt;br /&gt;
      t.timestamps&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As before, since we have made a change to the migration, we now need to do a '''db:migrate'''.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Valdations==&lt;br /&gt;
Validations, just like Relationships, are also defined in the model. Suppose we want to make sure that each recipe entered has a valid title and instructions, and the condition being that they cannot be empty. We can add this to the Recipe model, which now looks like :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :category&lt;br /&gt;
  validates :title, :presence =&amp;gt; true&lt;br /&gt;
  validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
  validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Thus, the title, instructions and category are all validated, and it makes sure that title and instructions are not empty, and that the category it belongs also exists.&lt;br /&gt;
&lt;br /&gt;
==Routes.rb==&lt;br /&gt;
The '''routes.rb''' file is located in the '''config folder'''. routes.rb, as the name suggests is used to route to the corresponding controller's method. Suppose we have defined a new method in the Category controller. Now, if we want to route a link to the Category controller's new method from some recipe view, we have to include this in the routes.rb file. If we don't, we get an error saying 'No route matches ...'. Whenever we decide to add a new method to a controller, we have to add something similar to :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
match 'Controller_name/new_method_name' =&amp;gt; 'Controller_name#new_method_name'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Our application starts are localhost:3000\categories. Suppose we want to have the root localhost:3000 to the index method in the category controller, we can do:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to =&amp;gt; 'categories#index'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This has to be done after deleting the index.html file from the public directory.&lt;br /&gt;
&lt;br /&gt;
==Views &amp;amp; Layouts==&lt;br /&gt;
Now that the major portion of the application has been done, let us talk about Views and Layouts. &lt;br /&gt;
First, let us add the following code to application.html.erb in the app\views\layouts folder.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to &amp;quot;Show all recipes&amp;quot;, recipes_url %&amp;gt; &amp;lt;%= link_to &amp;quot;Show all categories&amp;quot;, categories_url %&amp;gt; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, when we start the rails server, we see the following page :&lt;br /&gt;
&lt;br /&gt;
[[File:6.jpg]]&lt;br /&gt;
&lt;br /&gt;
Since we want the user to choose a category for each recipe, we include the following code in our view (in the recipes\_form.html.erb in the views section of our app) :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= f.label :category %&amp;gt;&lt;br /&gt;
&amp;lt;%= select(&amp;quot;recipe&amp;quot;, &amp;quot;category_id&amp;quot;, Category.find(:all).collect{ |c| [ c.name, c.id] }) %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view now would have a drop box to choose which category and the user can write the recipe for that category. The new recipe page would now look like :&lt;br /&gt;
&lt;br /&gt;
[[File:7.jpg]]&lt;br /&gt;
&lt;br /&gt;
The CookBookApp now can have many categories, and recipes for each category. Note that we just used a few commands : scaffold, db:migrate, etc. to create this application. Since rails uses the Convention over Configuration principle, it makes it easier to write applications.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
[1] Ruby on Rails Guides: Getting Started With Rails :http://guides.rubyonrails.org/getting_started.html&lt;br /&gt;
&lt;br /&gt;
[2] CookBookApp : http://courses.ncsu.edu/csc517/common/exercises/cookbook3/&lt;br /&gt;
&lt;br /&gt;
[3] CSC 517 Fall 2011 : Engineering Online Video : http://mediasite.online.ncsu.edu/online/Viewer/?peid=5a5b4de717dc4108a1b04133575b22181d&lt;br /&gt;
&lt;br /&gt;
[4] Course Schedule, and Lecture #9 Materials : http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures&lt;br /&gt;
&lt;br /&gt;
[5] Ruby on Rails official site : http://rubyonrails.org/&lt;br /&gt;
&lt;br /&gt;
[6] Sam Ruby, Agile Web Development with Rails, ISBN:1934356549&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=External Links=&lt;br /&gt;
*[http://ruby.railstutorial.org/ Ruby on Rails Tutorial by Michael Hartl] &lt;br /&gt;
*[http://www.rubyonrailstutorials.com/ Ruby on Rails Tutorials]&lt;br /&gt;
*[http://www.buildingwebapps.com/learningrails Audio lessons on Rails]&lt;br /&gt;
*[http://rubyonrails.org/screencasts Video Lectures on Rails]&lt;br /&gt;
*[http://www.ruby-lang.org/en/downloads/ Installing Ruby]&lt;br /&gt;
*[http://rubyforge.org/projects/instantrails/ Installing Rails]&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=54201</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4d mt</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=54201"/>
		<updated>2011-10-27T18:04:32Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''' The CookBook Application (Lecture 9)'''&lt;br /&gt;
=Introduction=&lt;br /&gt;
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.Rails includes a simple light weight application server [http://en.wikipedia.org/wiki/WEBrick Webrick] and a build system [http://en.wikipedia.org/wiki/Rake_(software) Rake] which makes development on rails easy.&lt;br /&gt;
&lt;br /&gt;
==Ruby on Rails==&lt;br /&gt;
Ruby on Rails, is an [http://en.wikipedia.org/wiki/Web_application_framework open source web application framework] for the [http://en.wikipedia.org/wiki/Ruby_(programming_language) 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:&lt;br /&gt;
===Convention over Configuration===&lt;br /&gt;
[http://en.wikipedia.org/wiki/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.&lt;br /&gt;
&lt;br /&gt;
===Don't repeat yourself=== &lt;br /&gt;
[http://en.wikipedia.org/wiki/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. Also in ActiveRecord there is no need to specify attribute names since it is picked up from the database tables itself.&lt;br /&gt;
&lt;br /&gt;
==Model/View/Controller Framework==&lt;br /&gt;
All Rails applications are implemented using the [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller 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.&lt;br /&gt;
Key components of MVC architecture include:&lt;br /&gt;
# 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.&lt;br /&gt;
#Views: Views provide a '''human interface(GUI)''' for the model. Views in Rails are implemented using embedded ruby (.html.erb) files&lt;br /&gt;
#Controllers: Controllers process the '''user actions''' and help redirect to the appropriate model ,build a user response and update the View. &lt;br /&gt;
The typical user interaction on a Rails application happens as follows:&lt;br /&gt;
#The user responds to the UI (ex : clicks a button)&lt;br /&gt;
#The router routes the action to the appropriate controller and action in it&lt;br /&gt;
#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&lt;br /&gt;
#A view queries the model and uses its data to build a response .&lt;br /&gt;
#The view updates the UI with the view and waits for further user interaction.&lt;br /&gt;
&lt;br /&gt;
==CRUD==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Create,_read,_update_and_delete 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:&lt;br /&gt;
#Create or add new entries&lt;br /&gt;
#Read, retrieve, search, or view existing entries&lt;br /&gt;
#Update or edit existing entries&lt;br /&gt;
#Delete existing entries&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
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 &amp;quot;contact management&amp;quot; or &amp;quot;contact maintenance&amp;quot; (or &amp;quot;document management&amp;quot; in general, depending on the basic storage unit for the particular application).&lt;br /&gt;
&lt;br /&gt;
==Routing in Rails==&lt;br /&gt;
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: &lt;br /&gt;
resources :photos &lt;br /&gt;
can allow us to specify 7 routes for the application.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! HTTP Verb&lt;br /&gt;
! Path &lt;br /&gt;
! action &lt;br /&gt;
! used for&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods GET]   || /photos || index || display a list of all photos&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods GET] || /photos/new || new  || return an HTML form for creating a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods POST]   ||	/photos	||create||	create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods GET]	||/photos/new ||	new ||	return an HTML form for creating a new photo &lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods POST]	|| /photos	|| create	|| create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods GET]	|| /photos/:id	|| show	|| display a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods GET]	|| /photos/:id/edit	|| edit	|| return an HTML form for editing a photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods PUT]	|| /photos/:id ||	update	|| update a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods DELETE]	|| /photos/:id	|| destroy	|| delete a specific photo&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
resources :photos&lt;br /&gt;
creates seven different routes in your application, all mapping to the Photos controller:&lt;br /&gt;
&lt;br /&gt;
==Object relational Mapping==&lt;br /&gt;
Rails handles Object Relational mapping using Active Records. ActiveRecords allow a Rails program to deal with objects and itself handles the mapping of these objects to relational databases. In Active Record, &lt;br /&gt;
# Database tables correspond to '''Rails classes'''&lt;br /&gt;
# Database records (rows) correspond to '''Rails objects'''.&lt;br /&gt;
ActiveRecord also allow us to query the database in the object domain by using the Active Record Query Interface&lt;br /&gt;
&lt;br /&gt;
=Developing the Cookbook App=&lt;br /&gt;
We now will look into how to develop the cookbook application, and talk about how to get started with the same. &lt;br /&gt;
To get started, we first create a new rails project called say 'CookBookApp', which is done by simply running the command : &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails new CookBookApp&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Once this is run, a series of folders are created, like app, config, db, etc. In developing the cookbook app, we will mainly look into the app, config and db folders mainly.&lt;br /&gt;
==Scaffolding==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Scaffold_%28programming%29 Scaffolding] is a very powerful command that creates code to create, read, update and delete (CRUD) data from the database. In the Cookbook application, the first scaffold is to create categories. Each Category will then have multiple recipes, and the main functionality of our cookbook app would be done.&lt;br /&gt;
The scaffold command to create categories is done by simply giving the command :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails generate scaffold category name:string&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
As seen, this generates the model called category, a bunch of views, and a category controller.&lt;br /&gt;
It also generates a migration, to create a new table called categories (notice that rails called the table as categories, which is the plural of category automatically).&lt;br /&gt;
===DB Migration===&lt;br /&gt;
The scaffold command generates a lot of files, and also a migration in db\migrate with a name like '''&amp;lt;timestamp&amp;gt;_create_categories.rb'''. This migration contains the details of the table that has to be created called 'categories', so the next step is to run the migrations. Before we do this, however, if we start the rails server, we get an error mentioning that the table 'categories' doesn't exist. &lt;br /&gt;
Let us now run the migration using the command :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After the migration is run, we know that the categories table has been created, and now run the server.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails server&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Creating Categories===&lt;br /&gt;
Now, if we goto the default localhost:3000 page, we see one option : Create a new category, as shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:2.jpg]]&lt;br /&gt;
&lt;br /&gt;
We can click on it, and make a new category, lets say 'Main Course'&lt;br /&gt;
&lt;br /&gt;
[[File:3.jpg]]&lt;br /&gt;
&lt;br /&gt;
When clicking on a new category notice that the URL is '''localhost:3000/categories/new'''. So we are executing the new method of the categories controller. On clicking 'Create Category', a new category with the name 'Main Course' is created, and a flash notice saying 'Successfully Create' is shown. We can now see the category in our list of categories.&lt;br /&gt;
&lt;br /&gt;
[[File:5.jpg]]&lt;br /&gt;
&lt;br /&gt;
So, as we saw, with just a simple scaffold command and db:migrate, we have an application with the basic CRUD functionality.&lt;br /&gt;
&lt;br /&gt;
===Creating Recipes===&lt;br /&gt;
Now, similar to creating categories, we create recipes using the scaffold command. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails generate scaffold recipe title:string description:string instructions:text&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Once this is done, we again have to run a '''db:migrate''', so that the recipes table is in the database.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, the recipes have been created, and as seen in the recipe model, each recipe belongs to a category.&lt;br /&gt;
&lt;br /&gt;
==Relationships==&lt;br /&gt;
Relationships are always defined in the model. We first want to say that each recipe belongs to a category. This can be done in the recipe controller as shown below :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similarly, we want to add a detail mentioning that each Category can have many Recipes : a one to many relationship. To do this, we need to edit the Category model, and include the '''has_many''' relationship with Recipe. The Category model now looks like :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
  has_many :recipes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We also have to add a reference in the migration, to indicate that the recipes table references the categories table. This can be done in the '''&amp;lt;timestamp&amp;gt;_create_recipes.rb''' migration, and now it would look like :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateRecipes &amp;lt; ActiveRecord::Migration&lt;br /&gt;
  def change&lt;br /&gt;
    create_table :recipes do |t|&lt;br /&gt;
      t.string :title&lt;br /&gt;
      t.string :description&lt;br /&gt;
      t.text :instructions&lt;br /&gt;
      t.references :category&lt;br /&gt;
      t.timestamps&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As before, since we have made a change to the migration, we now need to do a '''db:migrate'''.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Valdations==&lt;br /&gt;
Validations, just like Relationships, are also defined in the model. Suppose we want to make sure that each recipe entered has a valid title and instructions, and the condition being that they cannot be empty. We can add this to the Recipe model, which now looks like :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :category&lt;br /&gt;
  validates :title, :presence =&amp;gt; true&lt;br /&gt;
  validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
  validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Thus, the title, instructions and category are all validated, and it makes sure that title and instructions are not empty, and that the category it belongs also exists.&lt;br /&gt;
&lt;br /&gt;
==Routes.rb==&lt;br /&gt;
The '''routes.rb''' file is located in the '''config folder'''. routes.rb, as the name suggests is used to route to the corresponding controller's method. Suppose we have defined a new method in the Category controller. Now, if we want to route a link to the Category controller's new method from some recipe view, we have to include this in the routes.rb file. If we don't, we get an error saying 'No route matches ...'. Whenever we decide to add a new method to a controller, we have to add something similar to :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
match 'Controller_name/new_method_name' =&amp;gt; 'Controller_name#new_method_name'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Our application starts are localhost:3000\categories. Suppose we want to have the root localhost:3000 to the index method in the category controller, we can do:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to =&amp;gt; 'categories#index'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This has to be done after deleting the index.html file from the public directory.&lt;br /&gt;
&lt;br /&gt;
==Views &amp;amp; Layouts==&lt;br /&gt;
Now that the major portion of the application has been done, let us talk about Views and Layouts. &lt;br /&gt;
First, let us add the following code to application.html.erb in the app\views\layouts folder.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to &amp;quot;Show all recipes&amp;quot;, recipes_url %&amp;gt; &amp;lt;%= link_to &amp;quot;Show all categories&amp;quot;, categories_url %&amp;gt; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, when we start the rails server, we see the following page :&lt;br /&gt;
&lt;br /&gt;
[[File:6.jpg]]&lt;br /&gt;
&lt;br /&gt;
Since we want the user to choose a category for each recipe, we include the following code in our view (in the recipes\_form.html.erb in the views section of our app) :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= f.label :category %&amp;gt;&lt;br /&gt;
&amp;lt;%= select(&amp;quot;recipe&amp;quot;, &amp;quot;category_id&amp;quot;, Category.find(:all).collect{ |c| [ c.name, c.id] }) %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view now would have a drop box to choose which category and the user can write the recipe for that category. The new recipe page would now look like :&lt;br /&gt;
&lt;br /&gt;
[[File:7.jpg]]&lt;br /&gt;
&lt;br /&gt;
The CookBookApp now can have many categories, and recipes for each category. Note that we just used a few commands : scaffold, db:migrate, etc. to create this application. Since rails uses the Convention over Configuration principle, it makes it easier to write applications.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
[1] Ruby on Rails Guides: Getting Started With Rails :http://guides.rubyonrails.org/getting_started.html&lt;br /&gt;
&lt;br /&gt;
[2] CookBookApp : http://courses.ncsu.edu/csc517/common/exercises/cookbook3/&lt;br /&gt;
&lt;br /&gt;
[3] CSC 517 Fall 2011 : Engineering Online Video : http://mediasite.online.ncsu.edu/online/Viewer/?peid=5a5b4de717dc4108a1b04133575b22181d&lt;br /&gt;
&lt;br /&gt;
[4] Course Schedule, and Lecture #9 Materials : http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures&lt;br /&gt;
&lt;br /&gt;
[5] Ruby on Rails official site : http://rubyonrails.org/&lt;br /&gt;
&lt;br /&gt;
[6] Sam Ruby, Agile Web Development with Rails, ISBN:1934356549&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=External Links=&lt;br /&gt;
*[http://ruby.railstutorial.org/ Ruby on Rails Tutorial by Michael Hartl] &lt;br /&gt;
*[http://www.rubyonrailstutorials.com/ Ruby on Rails Tutorials]&lt;br /&gt;
*[http://www.buildingwebapps.com/learningrails Audio lessons on Rails]&lt;br /&gt;
*[http://rubyonrails.org/screencasts Video Lectures on Rails]&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=54200</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4d mt</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=54200"/>
		<updated>2011-10-27T18:04:15Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: /* External Links */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''' The CookBook Application (Lecture 9)'''&lt;br /&gt;
=Introduction=&lt;br /&gt;
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.Rails includes a simple light weight application server [http://en.wikipedia.org/wiki/WEBrick Webrick] and a build system [http://en.wikipedia.org/wiki/Rake_(software) Rake] which makes development on rails easy.&lt;br /&gt;
&lt;br /&gt;
==Ruby on Rails==&lt;br /&gt;
Ruby on Rails, is an [http://en.wikipedia.org/wiki/Web_application_framework open source web application framework] for the [http://en.wikipedia.org/wiki/Ruby_(programming_language) 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:&lt;br /&gt;
===Convention over Configuration===&lt;br /&gt;
[http://en.wikipedia.org/wiki/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.&lt;br /&gt;
&lt;br /&gt;
===Don't repeat yourself=== &lt;br /&gt;
[http://en.wikipedia.org/wiki/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. Also in ActiveRecord there is no need to specify attribute names since it is picked up from the database tables itself.&lt;br /&gt;
&lt;br /&gt;
==Model/View/Controller Framework==&lt;br /&gt;
All Rails applications are implemented using the [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller 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.&lt;br /&gt;
Key components of MVC architecture include:&lt;br /&gt;
# 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.&lt;br /&gt;
#Views: Views provide a '''human interface(GUI)''' for the model. Views in Rails are implemented using embedded ruby (.html.erb) files&lt;br /&gt;
#Controllers: Controllers process the '''user actions''' and help redirect to the appropriate model ,build a user response and update the View. &lt;br /&gt;
The typical user interaction on a Rails application happens as follows:&lt;br /&gt;
#The user responds to the UI (ex : clicks a button)&lt;br /&gt;
#The router routes the action to the appropriate controller and action in it&lt;br /&gt;
#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&lt;br /&gt;
#A view queries the model and uses its data to build a response .&lt;br /&gt;
#The view updates the UI with the view and waits for further user interaction.&lt;br /&gt;
&lt;br /&gt;
==CRUD==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Create,_read,_update_and_delete 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:&lt;br /&gt;
#Create or add new entries&lt;br /&gt;
#Read, retrieve, search, or view existing entries&lt;br /&gt;
#Update or edit existing entries&lt;br /&gt;
#Delete existing entries&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
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 &amp;quot;contact management&amp;quot; or &amp;quot;contact maintenance&amp;quot; (or &amp;quot;document management&amp;quot; in general, depending on the basic storage unit for the particular application).&lt;br /&gt;
&lt;br /&gt;
==Routing in Rails==&lt;br /&gt;
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: &lt;br /&gt;
resources :photos &lt;br /&gt;
can allow us to specify 7 routes for the application.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! HTTP Verb&lt;br /&gt;
! Path &lt;br /&gt;
! action &lt;br /&gt;
! used for&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods GET]   || /photos || index || display a list of all photos&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods GET] || /photos/new || new  || return an HTML form for creating a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods POST]   ||	/photos	||create||	create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods GET]	||/photos/new ||	new ||	return an HTML form for creating a new photo &lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods POST]	|| /photos	|| create	|| create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods GET]	|| /photos/:id	|| show	|| display a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods GET]	|| /photos/:id/edit	|| edit	|| return an HTML form for editing a photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods PUT]	|| /photos/:id ||	update	|| update a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods DELETE]	|| /photos/:id	|| destroy	|| delete a specific photo&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
resources :photos&lt;br /&gt;
creates seven different routes in your application, all mapping to the Photos controller:&lt;br /&gt;
&lt;br /&gt;
==Object relational Mapping==&lt;br /&gt;
Rails handles Object Relational mapping using Active Records. ActiveRecords allow a Rails program to deal with objects and itself handles the mapping of these objects to relational databases. In Active Record, &lt;br /&gt;
# Database tables correspond to '''Rails classes'''&lt;br /&gt;
# Database records (rows) correspond to '''Rails objects'''.&lt;br /&gt;
ActiveRecord also allow us to query the database in the object domain by using the Active Record Query Interface&lt;br /&gt;
&lt;br /&gt;
=Developing the Cookbook App=&lt;br /&gt;
We now will look into how to develop the cookbook application, and talk about how to get started with the same. &lt;br /&gt;
To get started, we first create a new rails project called say 'CookBookApp', which is done by simply running the command : &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails new CookBookApp&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Once this is run, a series of folders are created, like app, config, db, etc. In developing the cookbook app, we will mainly look into the app, config and db folders mainly.&lt;br /&gt;
==Scaffolding==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Scaffold_%28programming%29 Scaffolding] is a very powerful command that creates code to create, read, update and delete (CRUD) data from the database. In the Cookbook application, the first scaffold is to create categories. Each Category will then have multiple recipes, and the main functionality of our cookbook app would be done.&lt;br /&gt;
The scaffold command to create categories is done by simply giving the command :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails generate scaffold category name:string&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
As seen, this generates the model called category, a bunch of views, and a category controller.&lt;br /&gt;
It also generates a migration, to create a new table called categories (notice that rails called the table as categories, which is the plural of category automatically).&lt;br /&gt;
===DB Migration===&lt;br /&gt;
The scaffold command generates a lot of files, and also a migration in db\migrate with a name like '''&amp;lt;timestamp&amp;gt;_create_categories.rb'''. This migration contains the details of the table that has to be created called 'categories', so the next step is to run the migrations. Before we do this, however, if we start the rails server, we get an error mentioning that the table 'categories' doesn't exist. &lt;br /&gt;
Let us now run the migration using the command :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After the migration is run, we know that the categories table has been created, and now run the server.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails server&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Creating Categories===&lt;br /&gt;
Now, if we goto the default localhost:3000 page, we see one option : Create a new category, as shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:2.jpg]]&lt;br /&gt;
&lt;br /&gt;
We can click on it, and make a new category, lets say 'Main Course'&lt;br /&gt;
&lt;br /&gt;
[[File:3.jpg]]&lt;br /&gt;
&lt;br /&gt;
When clicking on a new category notice that the URL is '''localhost:3000/categories/new'''. So we are executing the new method of the categories controller. On clicking 'Create Category', a new category with the name 'Main Course' is created, and a flash notice saying 'Successfully Create' is shown. We can now see the category in our list of categories.&lt;br /&gt;
&lt;br /&gt;
[[File:5.jpg]]&lt;br /&gt;
&lt;br /&gt;
So, as we saw, with just a simple scaffold command and db:migrate, we have an application with the basic CRUD functionality.&lt;br /&gt;
&lt;br /&gt;
===Creating Recipes===&lt;br /&gt;
Now, similar to creating categories, we create recipes using the scaffold command. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails generate scaffold recipe title:string description:string instructions:text&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Once this is done, we again have to run a '''db:migrate''', so that the recipes table is in the database.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, the recipes have been created, and as seen in the recipe model, each recipe belongs to a category.&lt;br /&gt;
&lt;br /&gt;
==Relationships==&lt;br /&gt;
Relationships are always defined in the model. We first want to say that each recipe belongs to a category. This can be done in the recipe controller as shown below :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similarly, we want to add a detail mentioning that each Category can have many Recipes : a one to many relationship. To do this, we need to edit the Category model, and include the '''has_many''' relationship with Recipe. The Category model now looks like :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
  has_many :recipes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We also have to add a reference in the migration, to indicate that the recipes table references the categories table. This can be done in the '''&amp;lt;timestamp&amp;gt;_create_recipes.rb''' migration, and now it would look like :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateRecipes &amp;lt; ActiveRecord::Migration&lt;br /&gt;
  def change&lt;br /&gt;
    create_table :recipes do |t|&lt;br /&gt;
      t.string :title&lt;br /&gt;
      t.string :description&lt;br /&gt;
      t.text :instructions&lt;br /&gt;
      t.references :category&lt;br /&gt;
      t.timestamps&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As before, since we have made a change to the migration, we now need to do a '''db:migrate'''.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Valdations==&lt;br /&gt;
Validations, just like Relationships, are also defined in the model. Suppose we want to make sure that each recipe entered has a valid title and instructions, and the condition being that they cannot be empty. We can add this to the Recipe model, which now looks like :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :category&lt;br /&gt;
  validates :title, :presence =&amp;gt; true&lt;br /&gt;
  validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
  validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Thus, the title, instructions and category are all validated, and it makes sure that title and instructions are not empty, and that the category it belongs also exists.&lt;br /&gt;
&lt;br /&gt;
==Routes.rb==&lt;br /&gt;
The '''routes.rb''' file is located in the '''config folder'''. routes.rb, as the name suggests is used to route to the corresponding controller's method. Suppose we have defined a new method in the Category controller. Now, if we want to route a link to the Category controller's new method from some recipe view, we have to include this in the routes.rb file. If we don't, we get an error saying 'No route matches ...'. Whenever we decide to add a new method to a controller, we have to add something similar to :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
match 'Controller_name/new_method_name' =&amp;gt; 'Controller_name#new_method_name'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Our application starts are localhost:3000\categories. Suppose we want to have the root localhost:3000 to the index method in the category controller, we can do:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to =&amp;gt; 'categories#index'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This has to be done after deleting the index.html file from the public directory.&lt;br /&gt;
&lt;br /&gt;
==Views &amp;amp; Layouts==&lt;br /&gt;
Now that the major portion of the application has been done, let us talk about Views and Layouts. &lt;br /&gt;
First, let us add the following code to application.html.erb in the app\views\layouts folder.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to &amp;quot;Show all recipes&amp;quot;, recipes_url %&amp;gt; &amp;lt;%= link_to &amp;quot;Show all categories&amp;quot;, categories_url %&amp;gt; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, when we start the rails server, we see the following page :&lt;br /&gt;
&lt;br /&gt;
[[File:6.jpg]]&lt;br /&gt;
&lt;br /&gt;
Since we want the user to choose a category for each recipe, we include the following code in our view (in the recipes\_form.html.erb in the views section of our app) :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= f.label :category %&amp;gt;&lt;br /&gt;
&amp;lt;%= select(&amp;quot;recipe&amp;quot;, &amp;quot;category_id&amp;quot;, Category.find(:all).collect{ |c| [ c.name, c.id] }) %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view now would have a drop box to choose which category and the user can write the recipe for that category. The new recipe page would now look like :&lt;br /&gt;
&lt;br /&gt;
[[File:7.jpg]]&lt;br /&gt;
&lt;br /&gt;
The CookBookApp now can have many categories, and recipes for each category. Note that we just used a few commands : scaffold, db:migrate, etc. to create this application. Since rails uses the Convention over Configuration principle, it makes it easier to write applications.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
[1] Ruby on Rails Guides: Getting Started With Rails :http://guides.rubyonrails.org/getting_started.html&lt;br /&gt;
&lt;br /&gt;
[2] CookBookApp : http://courses.ncsu.edu/csc517/common/exercises/cookbook3/&lt;br /&gt;
&lt;br /&gt;
[3] CSC 517 Fall 2011 : Engineering Online Video : http://mediasite.online.ncsu.edu/online/Viewer/?peid=5a5b4de717dc4108a1b04133575b22181d&lt;br /&gt;
&lt;br /&gt;
[4] Course Schedule, and Lecture #9 Materials : http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures&lt;br /&gt;
&lt;br /&gt;
[5] Ruby on Rails official site : http://rubyonrails.org/&lt;br /&gt;
&lt;br /&gt;
[6] Sam Ruby, Agile Web Development with Rails, ISBN:1934356549&lt;br /&gt;
&lt;br /&gt;
[8] Installing Ruby : http://www.ruby-lang.org/en/downloads/&lt;br /&gt;
&lt;br /&gt;
[7] Installing Rails : http://rubyforge.org/projects/instantrails/&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=External Links=&lt;br /&gt;
*[http://ruby.railstutorial.org/ Ruby on Rails Tutorial by Michael Hartl] &lt;br /&gt;
*[http://www.rubyonrailstutorials.com/ Ruby on Rails Tutorials]&lt;br /&gt;
*[http://www.buildingwebapps.com/learningrails Audio lessons on Rails]&lt;br /&gt;
*[http://rubyonrails.org/screencasts Video Lectures on Rails]&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=54199</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4d mt</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=54199"/>
		<updated>2011-10-27T18:02:04Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''' The CookBook Application (Lecture 9)'''&lt;br /&gt;
=Introduction=&lt;br /&gt;
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.Rails includes a simple light weight application server [http://en.wikipedia.org/wiki/WEBrick Webrick] and a build system [http://en.wikipedia.org/wiki/Rake_(software) Rake] which makes development on rails easy.&lt;br /&gt;
&lt;br /&gt;
==Ruby on Rails==&lt;br /&gt;
Ruby on Rails, is an [http://en.wikipedia.org/wiki/Web_application_framework open source web application framework] for the [http://en.wikipedia.org/wiki/Ruby_(programming_language) 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:&lt;br /&gt;
===Convention over Configuration===&lt;br /&gt;
[http://en.wikipedia.org/wiki/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.&lt;br /&gt;
&lt;br /&gt;
===Don't repeat yourself=== &lt;br /&gt;
[http://en.wikipedia.org/wiki/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. Also in ActiveRecord there is no need to specify attribute names since it is picked up from the database tables itself.&lt;br /&gt;
&lt;br /&gt;
==Model/View/Controller Framework==&lt;br /&gt;
All Rails applications are implemented using the [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller 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.&lt;br /&gt;
Key components of MVC architecture include:&lt;br /&gt;
# 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.&lt;br /&gt;
#Views: Views provide a '''human interface(GUI)''' for the model. Views in Rails are implemented using embedded ruby (.html.erb) files&lt;br /&gt;
#Controllers: Controllers process the '''user actions''' and help redirect to the appropriate model ,build a user response and update the View. &lt;br /&gt;
The typical user interaction on a Rails application happens as follows:&lt;br /&gt;
#The user responds to the UI (ex : clicks a button)&lt;br /&gt;
#The router routes the action to the appropriate controller and action in it&lt;br /&gt;
#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&lt;br /&gt;
#A view queries the model and uses its data to build a response .&lt;br /&gt;
#The view updates the UI with the view and waits for further user interaction.&lt;br /&gt;
&lt;br /&gt;
==CRUD==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Create,_read,_update_and_delete 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:&lt;br /&gt;
#Create or add new entries&lt;br /&gt;
#Read, retrieve, search, or view existing entries&lt;br /&gt;
#Update or edit existing entries&lt;br /&gt;
#Delete existing entries&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
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 &amp;quot;contact management&amp;quot; or &amp;quot;contact maintenance&amp;quot; (or &amp;quot;document management&amp;quot; in general, depending on the basic storage unit for the particular application).&lt;br /&gt;
&lt;br /&gt;
==Routing in Rails==&lt;br /&gt;
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: &lt;br /&gt;
resources :photos &lt;br /&gt;
can allow us to specify 7 routes for the application.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! HTTP Verb&lt;br /&gt;
! Path &lt;br /&gt;
! action &lt;br /&gt;
! used for&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods GET]   || /photos || index || display a list of all photos&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods GET] || /photos/new || new  || return an HTML form for creating a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods POST]   ||	/photos	||create||	create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods GET]	||/photos/new ||	new ||	return an HTML form for creating a new photo &lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods POST]	|| /photos	|| create	|| create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods GET]	|| /photos/:id	|| show	|| display a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods GET]	|| /photos/:id/edit	|| edit	|| return an HTML form for editing a photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods PUT]	|| /photos/:id ||	update	|| update a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods DELETE]	|| /photos/:id	|| destroy	|| delete a specific photo&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
resources :photos&lt;br /&gt;
creates seven different routes in your application, all mapping to the Photos controller:&lt;br /&gt;
&lt;br /&gt;
==Object relational Mapping==&lt;br /&gt;
Rails handles Object Relational mapping using Active Records. ActiveRecords allow a Rails program to deal with objects and itself handles the mapping of these objects to relational databases. In Active Record, &lt;br /&gt;
# Database tables correspond to '''Rails classes'''&lt;br /&gt;
# Database records (rows) correspond to '''Rails objects'''.&lt;br /&gt;
ActiveRecord also allow us to query the database in the object domain by using the Active Record Query Interface&lt;br /&gt;
&lt;br /&gt;
=Developing the Cookbook App=&lt;br /&gt;
We now will look into how to develop the cookbook application, and talk about how to get started with the same. &lt;br /&gt;
To get started, we first create a new rails project called say 'CookBookApp', which is done by simply running the command : &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails new CookBookApp&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Once this is run, a series of folders are created, like app, config, db, etc. In developing the cookbook app, we will mainly look into the app, config and db folders mainly.&lt;br /&gt;
==Scaffolding==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Scaffold_%28programming%29 Scaffolding] is a very powerful command that creates code to create, read, update and delete (CRUD) data from the database. In the Cookbook application, the first scaffold is to create categories. Each Category will then have multiple recipes, and the main functionality of our cookbook app would be done.&lt;br /&gt;
The scaffold command to create categories is done by simply giving the command :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails generate scaffold category name:string&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
As seen, this generates the model called category, a bunch of views, and a category controller.&lt;br /&gt;
It also generates a migration, to create a new table called categories (notice that rails called the table as categories, which is the plural of category automatically).&lt;br /&gt;
===DB Migration===&lt;br /&gt;
The scaffold command generates a lot of files, and also a migration in db\migrate with a name like '''&amp;lt;timestamp&amp;gt;_create_categories.rb'''. This migration contains the details of the table that has to be created called 'categories', so the next step is to run the migrations. Before we do this, however, if we start the rails server, we get an error mentioning that the table 'categories' doesn't exist. &lt;br /&gt;
Let us now run the migration using the command :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After the migration is run, we know that the categories table has been created, and now run the server.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails server&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Creating Categories===&lt;br /&gt;
Now, if we goto the default localhost:3000 page, we see one option : Create a new category, as shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:2.jpg]]&lt;br /&gt;
&lt;br /&gt;
We can click on it, and make a new category, lets say 'Main Course'&lt;br /&gt;
&lt;br /&gt;
[[File:3.jpg]]&lt;br /&gt;
&lt;br /&gt;
When clicking on a new category notice that the URL is '''localhost:3000/categories/new'''. So we are executing the new method of the categories controller. On clicking 'Create Category', a new category with the name 'Main Course' is created, and a flash notice saying 'Successfully Create' is shown. We can now see the category in our list of categories.&lt;br /&gt;
&lt;br /&gt;
[[File:5.jpg]]&lt;br /&gt;
&lt;br /&gt;
So, as we saw, with just a simple scaffold command and db:migrate, we have an application with the basic CRUD functionality.&lt;br /&gt;
&lt;br /&gt;
===Creating Recipes===&lt;br /&gt;
Now, similar to creating categories, we create recipes using the scaffold command. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails generate scaffold recipe title:string description:string instructions:text&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Once this is done, we again have to run a '''db:migrate''', so that the recipes table is in the database.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, the recipes have been created, and as seen in the recipe model, each recipe belongs to a category.&lt;br /&gt;
&lt;br /&gt;
==Relationships==&lt;br /&gt;
Relationships are always defined in the model. We first want to say that each recipe belongs to a category. This can be done in the recipe controller as shown below :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similarly, we want to add a detail mentioning that each Category can have many Recipes : a one to many relationship. To do this, we need to edit the Category model, and include the '''has_many''' relationship with Recipe. The Category model now looks like :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
  has_many :recipes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We also have to add a reference in the migration, to indicate that the recipes table references the categories table. This can be done in the '''&amp;lt;timestamp&amp;gt;_create_recipes.rb''' migration, and now it would look like :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateRecipes &amp;lt; ActiveRecord::Migration&lt;br /&gt;
  def change&lt;br /&gt;
    create_table :recipes do |t|&lt;br /&gt;
      t.string :title&lt;br /&gt;
      t.string :description&lt;br /&gt;
      t.text :instructions&lt;br /&gt;
      t.references :category&lt;br /&gt;
      t.timestamps&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As before, since we have made a change to the migration, we now need to do a '''db:migrate'''.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Valdations==&lt;br /&gt;
Validations, just like Relationships, are also defined in the model. Suppose we want to make sure that each recipe entered has a valid title and instructions, and the condition being that they cannot be empty. We can add this to the Recipe model, which now looks like :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :category&lt;br /&gt;
  validates :title, :presence =&amp;gt; true&lt;br /&gt;
  validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
  validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Thus, the title, instructions and category are all validated, and it makes sure that title and instructions are not empty, and that the category it belongs also exists.&lt;br /&gt;
&lt;br /&gt;
==Routes.rb==&lt;br /&gt;
The '''routes.rb''' file is located in the '''config folder'''. routes.rb, as the name suggests is used to route to the corresponding controller's method. Suppose we have defined a new method in the Category controller. Now, if we want to route a link to the Category controller's new method from some recipe view, we have to include this in the routes.rb file. If we don't, we get an error saying 'No route matches ...'. Whenever we decide to add a new method to a controller, we have to add something similar to :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
match 'Controller_name/new_method_name' =&amp;gt; 'Controller_name#new_method_name'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Our application starts are localhost:3000\categories. Suppose we want to have the root localhost:3000 to the index method in the category controller, we can do:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to =&amp;gt; 'categories#index'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This has to be done after deleting the index.html file from the public directory.&lt;br /&gt;
&lt;br /&gt;
==Views &amp;amp; Layouts==&lt;br /&gt;
Now that the major portion of the application has been done, let us talk about Views and Layouts. &lt;br /&gt;
First, let us add the following code to application.html.erb in the app\views\layouts folder.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to &amp;quot;Show all recipes&amp;quot;, recipes_url %&amp;gt; &amp;lt;%= link_to &amp;quot;Show all categories&amp;quot;, categories_url %&amp;gt; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, when we start the rails server, we see the following page :&lt;br /&gt;
&lt;br /&gt;
[[File:6.jpg]]&lt;br /&gt;
&lt;br /&gt;
Since we want the user to choose a category for each recipe, we include the following code in our view (in the recipes\_form.html.erb in the views section of our app) :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= f.label :category %&amp;gt;&lt;br /&gt;
&amp;lt;%= select(&amp;quot;recipe&amp;quot;, &amp;quot;category_id&amp;quot;, Category.find(:all).collect{ |c| [ c.name, c.id] }) %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view now would have a drop box to choose which category and the user can write the recipe for that category. The new recipe page would now look like :&lt;br /&gt;
&lt;br /&gt;
[[File:7.jpg]]&lt;br /&gt;
&lt;br /&gt;
The CookBookApp now can have many categories, and recipes for each category. Note that we just used a few commands : scaffold, db:migrate, etc. to create this application. Since rails uses the Convention over Configuration principle, it makes it easier to write applications.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
[1] Ruby on Rails Guides: Getting Started With Rails :http://guides.rubyonrails.org/getting_started.html&lt;br /&gt;
&lt;br /&gt;
[2] CookBookApp : http://courses.ncsu.edu/csc517/common/exercises/cookbook3/&lt;br /&gt;
&lt;br /&gt;
[3] CSC 517 Fall 2011 : Engineering Online Video : http://mediasite.online.ncsu.edu/online/Viewer/?peid=5a5b4de717dc4108a1b04133575b22181d&lt;br /&gt;
&lt;br /&gt;
[4] Course Schedule, and Lecture #9 Materials : http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures&lt;br /&gt;
&lt;br /&gt;
[5] Ruby on Rails official site : http://rubyonrails.org/&lt;br /&gt;
&lt;br /&gt;
[6] Sam Ruby, Agile Web Development with Rails, ISBN:1934356549&lt;br /&gt;
&lt;br /&gt;
[8] Installing Ruby : http://www.ruby-lang.org/en/downloads/&lt;br /&gt;
&lt;br /&gt;
[7] Installing Rails : http://rubyforge.org/projects/instantrails/&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=External Links=&lt;br /&gt;
[1] Ruby on Rails Tutorial by Michael Hartl : http://ruby.railstutorial.org/&lt;br /&gt;
[2] Ruby on Rails Tutorials : http://www.rubyonrailstutorials.com/&lt;br /&gt;
[3] Audio lessons on Rails : http://www.buildingwebapps.com/learningrails&lt;br /&gt;
[4] Video Lectures on Rails : http://rubyonrails.org/screencasts&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=54198</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4d mt</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=54198"/>
		<updated>2011-10-27T18:01:41Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: /* External Links */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''' The CookBook Application (Lecture 9)'''&lt;br /&gt;
=Introduction=&lt;br /&gt;
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.Rails includes a simple light weight application server [http://en.wikipedia.org/wiki/WEBrick Webrick] and a build system [http://en.wikipedia.org/wiki/Rake_(software) Rake] which makes development on rails easy.&lt;br /&gt;
&lt;br /&gt;
==Ruby on Rails==&lt;br /&gt;
Ruby on Rails, is an [http://en.wikipedia.org/wiki/Web_application_framework open source web application framework] for the [http://en.wikipedia.org/wiki/Ruby_(programming_language) 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:&lt;br /&gt;
===Convention over Configuration===&lt;br /&gt;
[http://en.wikipedia.org/wiki/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.&lt;br /&gt;
&lt;br /&gt;
===Don't repeat yourself=== &lt;br /&gt;
[http://en.wikipedia.org/wiki/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. Also in ActiveRecord there is no need to specify attribute names since it is picked up from the database tables itself.&lt;br /&gt;
&lt;br /&gt;
==Model/View/Controller Framework==&lt;br /&gt;
All Rails applications are implemented using the [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller 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.&lt;br /&gt;
Key components of MVC architecture include:&lt;br /&gt;
# 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.&lt;br /&gt;
#Views: Views provide a '''human interface(GUI)''' for the model. Views in Rails are implemented using embedded ruby (.html.erb) files&lt;br /&gt;
#Controllers: Controllers process the '''user actions''' and help redirect to the appropriate model ,build a user response and update the View. &lt;br /&gt;
The typical user interaction on a Rails application happens as follows:&lt;br /&gt;
#The user responds to the UI (ex : clicks a button)&lt;br /&gt;
#The router routes the action to the appropriate controller and action in it&lt;br /&gt;
#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&lt;br /&gt;
#A view queries the model and uses its data to build a response .&lt;br /&gt;
#The view updates the UI with the view and waits for further user interaction.&lt;br /&gt;
&lt;br /&gt;
==CRUD==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Create,_read,_update_and_delete 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:&lt;br /&gt;
#Create or add new entries&lt;br /&gt;
#Read, retrieve, search, or view existing entries&lt;br /&gt;
#Update or edit existing entries&lt;br /&gt;
#Delete existing entries&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
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 &amp;quot;contact management&amp;quot; or &amp;quot;contact maintenance&amp;quot; (or &amp;quot;document management&amp;quot; in general, depending on the basic storage unit for the particular application).&lt;br /&gt;
&lt;br /&gt;
==Routing in Rails==&lt;br /&gt;
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: &lt;br /&gt;
resources :photos &lt;br /&gt;
can allow us to specify 7 routes for the application.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! HTTP Verb&lt;br /&gt;
! Path &lt;br /&gt;
! action &lt;br /&gt;
! used for&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods GET]   || /photos || index || display a list of all photos&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods GET] || /photos/new || new  || return an HTML form for creating a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods POST]   ||	/photos	||create||	create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods GET]	||/photos/new ||	new ||	return an HTML form for creating a new photo &lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods POST]	|| /photos	|| create	|| create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods GET]	|| /photos/:id	|| show	|| display a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods GET]	|| /photos/:id/edit	|| edit	|| return an HTML form for editing a photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods PUT]	|| /photos/:id ||	update	|| update a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods DELETE]	|| /photos/:id	|| destroy	|| delete a specific photo&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
resources :photos&lt;br /&gt;
creates seven different routes in your application, all mapping to the Photos controller:&lt;br /&gt;
&lt;br /&gt;
==Object relational Mapping==&lt;br /&gt;
Rails handles Object Relational mapping using Active Records. ActiveRecords allow a Rails program to deal with objects and itself handles the mapping of these objects to relational databases. In Active Record, &lt;br /&gt;
# Database tables correspond to '''Rails classes'''&lt;br /&gt;
# Database records (rows) correspond to '''Rails objects'''.&lt;br /&gt;
ActiveRecord also allow us to query the database in the object domain by using the Active Record Query Interface&lt;br /&gt;
&lt;br /&gt;
=Developing the Cookbook App=&lt;br /&gt;
We now will look into how to develop the cookbook application, and talk about how to get started with the same. &lt;br /&gt;
To get started, we first create a new rails project called say 'CookBookApp', which is done by simply running the command : &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails new CookBookApp&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Once this is run, a series of folders are created, like app, config, db, etc. In developing the cookbook app, we will mainly look into the app, config and db folders mainly.&lt;br /&gt;
==Scaffolding==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Scaffold_%28programming%29 Scaffolding] is a very powerful command that creates code to create, read, update and delete (CRUD) data from the database. In the Cookbook application, the first scaffold is to create categories. Each Category will then have multiple recipes, and the main functionality of our cookbook app would be done.&lt;br /&gt;
The scaffold command to create categories is done by simply giving the command :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails generate scaffold category name:string&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
As seen, this generates the model called category, a bunch of views, and a category controller.&lt;br /&gt;
It also generates a migration, to create a new table called categories (notice that rails called the table as categories, which is the plural of category automatically).&lt;br /&gt;
===DB Migration===&lt;br /&gt;
The scaffold command generates a lot of files, and also a migration in db\migrate with a name like '''&amp;lt;timestamp&amp;gt;_create_categories.rb'''. This migration contains the details of the table that has to be created called 'categories', so the next step is to run the migrations. Before we do this, however, if we start the rails server, we get an error mentioning that the table 'categories' doesn't exist. &lt;br /&gt;
Let us now run the migration using the command :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After the migration is run, we know that the categories table has been created, and now run the server.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails server&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Creating Categories===&lt;br /&gt;
Now, if we goto the default localhost:3000 page, we see one option : Create a new category, as shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:2.jpg]]&lt;br /&gt;
&lt;br /&gt;
We can click on it, and make a new category, lets say 'Main Course'&lt;br /&gt;
&lt;br /&gt;
[[File:3.jpg]]&lt;br /&gt;
&lt;br /&gt;
When clicking on a new category notice that the URL is '''localhost:3000/categories/new'''. So we are executing the new method of the categories controller. On clicking 'Create Category', a new category with the name 'Main Course' is created, and a flash notice saying 'Successfully Create' is shown. We can now see the category in our list of categories.&lt;br /&gt;
&lt;br /&gt;
[[File:5.jpg]]&lt;br /&gt;
&lt;br /&gt;
So, as we saw, with just a simple scaffold command and db:migrate, we have an application with the basic CRUD functionality.&lt;br /&gt;
&lt;br /&gt;
===Creating Recipes===&lt;br /&gt;
Now, similar to creating categories, we create recipes using the scaffold command. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails generate scaffold recipe title:string description:string instructions:text&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Once this is done, we again have to run a '''db:migrate''', so that the recipes table is in the database.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, the recipes have been created, and as seen in the recipe model, each recipe belongs to a category.&lt;br /&gt;
&lt;br /&gt;
==Relationships==&lt;br /&gt;
Relationships are always defined in the model. We first want to say that each recipe belongs to a category. This can be done in the recipe controller as shown below :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similarly, we want to add a detail mentioning that each Category can have many Recipes : a one to many relationship. To do this, we need to edit the Category model, and include the '''has_many''' relationship with Recipe. The Category model now looks like :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
  has_many :recipes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We also have to add a reference in the migration, to indicate that the recipes table references the categories table. This can be done in the '''&amp;lt;timestamp&amp;gt;_create_recipes.rb''' migration, and now it would look like :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateRecipes &amp;lt; ActiveRecord::Migration&lt;br /&gt;
  def change&lt;br /&gt;
    create_table :recipes do |t|&lt;br /&gt;
      t.string :title&lt;br /&gt;
      t.string :description&lt;br /&gt;
      t.text :instructions&lt;br /&gt;
      t.references :category&lt;br /&gt;
      t.timestamps&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As before, since we have made a change to the migration, we now need to do a '''db:migrate'''.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Valdations==&lt;br /&gt;
Validations, just like Relationships, are also defined in the model. Suppose we want to make sure that each recipe entered has a valid title and instructions, and the condition being that they cannot be empty. We can add this to the Recipe model, which now looks like :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :category&lt;br /&gt;
  validates :title, :presence =&amp;gt; true&lt;br /&gt;
  validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
  validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Thus, the title, instructions and category are all validated, and it makes sure that title and instructions are not empty, and that the category it belongs also exists.&lt;br /&gt;
&lt;br /&gt;
==Routes.rb==&lt;br /&gt;
The '''routes.rb''' file is located in the '''config folder'''. routes.rb, as the name suggests is used to route to the corresponding controller's method. Suppose we have defined a new method in the Category controller. Now, if we want to route a link to the Category controller's new method from some recipe view, we have to include this in the routes.rb file. If we don't, we get an error saying 'No route matches ...'. Whenever we decide to add a new method to a controller, we have to add something similar to :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
match 'Controller_name/new_method_name' =&amp;gt; 'Controller_name#new_method_name'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Our application starts are localhost:3000\categories. Suppose we want to have the root localhost:3000 to the index method in the category controller, we can do:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to =&amp;gt; 'categories#index'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This has to be done after deleting the index.html file from the public directory.&lt;br /&gt;
&lt;br /&gt;
==Views &amp;amp; Layouts==&lt;br /&gt;
Now that the major portion of the application has been done, let us talk about Views and Layouts. &lt;br /&gt;
First, let us add the following code to application.html.erb in the app\views\layouts folder.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to &amp;quot;Show all recipes&amp;quot;, recipes_url %&amp;gt; &amp;lt;%= link_to &amp;quot;Show all categories&amp;quot;, categories_url %&amp;gt; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, when we start the rails server, we see the following page :&lt;br /&gt;
&lt;br /&gt;
[[File:6.jpg]]&lt;br /&gt;
&lt;br /&gt;
Since we want the user to choose a category for each recipe, we include the following code in our view (in the recipes\_form.html.erb in the views section of our app) :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= f.label :category %&amp;gt;&lt;br /&gt;
&amp;lt;%= select(&amp;quot;recipe&amp;quot;, &amp;quot;category_id&amp;quot;, Category.find(:all).collect{ |c| [ c.name, c.id] }) %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view now would have a drop box to choose which category and the user can write the recipe for that category. The new recipe page would now look like :&lt;br /&gt;
&lt;br /&gt;
[[File:7.jpg]]&lt;br /&gt;
&lt;br /&gt;
The CookBookApp now can have many categories, and recipes for each category. Note that we just used a few commands : scaffold, db:migrate, etc. to create this application. Since rails uses the Convention over Configuration principle, it makes it easier to write applications.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
[1] Ruby on Rails Guides: Getting Started With Rails :http://guides.rubyonrails.org/getting_started.html&lt;br /&gt;
&lt;br /&gt;
[2] CookBookApp : http://courses.ncsu.edu/csc517/common/exercises/cookbook3/&lt;br /&gt;
&lt;br /&gt;
[3] CSC 517 Fall 2011 : Engineering Online Video : http://mediasite.online.ncsu.edu/online/Viewer/?peid=5a5b4de717dc4108a1b04133575b22181d&lt;br /&gt;
&lt;br /&gt;
[4] Course Schedule, and Lecture #9 Materials : http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures&lt;br /&gt;
&lt;br /&gt;
[5] Ruby on Rails official site : http://rubyonrails.org/&lt;br /&gt;
&lt;br /&gt;
[6] Sam Ruby, Agile Web Development with Rails, ISBN:1934356549&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[8] Installing Ruby : http://www.ruby-lang.org/en/downloads/&lt;br /&gt;
&lt;br /&gt;
[7] Installing Rails : http://rubyforge.org/projects/instantrails/ &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=External Links=&lt;br /&gt;
[1] Ruby on Rails Tutorial by Michael Hartl : http://ruby.railstutorial.org/&lt;br /&gt;
[2] Ruby on Rails Tutorials : http://www.rubyonrailstutorials.com/&lt;br /&gt;
[3] Audio lessons on Rails : http://www.buildingwebapps.com/learningrails&lt;br /&gt;
[4] Video Lectures on Rails : http://rubyonrails.org/screencasts&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=54197</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4d mt</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=54197"/>
		<updated>2011-10-27T18:01:29Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''' The CookBook Application (Lecture 9)'''&lt;br /&gt;
=Introduction=&lt;br /&gt;
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.Rails includes a simple light weight application server [http://en.wikipedia.org/wiki/WEBrick Webrick] and a build system [http://en.wikipedia.org/wiki/Rake_(software) Rake] which makes development on rails easy.&lt;br /&gt;
&lt;br /&gt;
==Ruby on Rails==&lt;br /&gt;
Ruby on Rails, is an [http://en.wikipedia.org/wiki/Web_application_framework open source web application framework] for the [http://en.wikipedia.org/wiki/Ruby_(programming_language) 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:&lt;br /&gt;
===Convention over Configuration===&lt;br /&gt;
[http://en.wikipedia.org/wiki/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.&lt;br /&gt;
&lt;br /&gt;
===Don't repeat yourself=== &lt;br /&gt;
[http://en.wikipedia.org/wiki/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. Also in ActiveRecord there is no need to specify attribute names since it is picked up from the database tables itself.&lt;br /&gt;
&lt;br /&gt;
==Model/View/Controller Framework==&lt;br /&gt;
All Rails applications are implemented using the [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller 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.&lt;br /&gt;
Key components of MVC architecture include:&lt;br /&gt;
# 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.&lt;br /&gt;
#Views: Views provide a '''human interface(GUI)''' for the model. Views in Rails are implemented using embedded ruby (.html.erb) files&lt;br /&gt;
#Controllers: Controllers process the '''user actions''' and help redirect to the appropriate model ,build a user response and update the View. &lt;br /&gt;
The typical user interaction on a Rails application happens as follows:&lt;br /&gt;
#The user responds to the UI (ex : clicks a button)&lt;br /&gt;
#The router routes the action to the appropriate controller and action in it&lt;br /&gt;
#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&lt;br /&gt;
#A view queries the model and uses its data to build a response .&lt;br /&gt;
#The view updates the UI with the view and waits for further user interaction.&lt;br /&gt;
&lt;br /&gt;
==CRUD==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Create,_read,_update_and_delete 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:&lt;br /&gt;
#Create or add new entries&lt;br /&gt;
#Read, retrieve, search, or view existing entries&lt;br /&gt;
#Update or edit existing entries&lt;br /&gt;
#Delete existing entries&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
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 &amp;quot;contact management&amp;quot; or &amp;quot;contact maintenance&amp;quot; (or &amp;quot;document management&amp;quot; in general, depending on the basic storage unit for the particular application).&lt;br /&gt;
&lt;br /&gt;
==Routing in Rails==&lt;br /&gt;
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: &lt;br /&gt;
resources :photos &lt;br /&gt;
can allow us to specify 7 routes for the application.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! HTTP Verb&lt;br /&gt;
! Path &lt;br /&gt;
! action &lt;br /&gt;
! used for&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods GET]   || /photos || index || display a list of all photos&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods GET] || /photos/new || new  || return an HTML form for creating a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods POST]   ||	/photos	||create||	create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods GET]	||/photos/new ||	new ||	return an HTML form for creating a new photo &lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods POST]	|| /photos	|| create	|| create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods GET]	|| /photos/:id	|| show	|| display a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods GET]	|| /photos/:id/edit	|| edit	|| return an HTML form for editing a photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods PUT]	|| /photos/:id ||	update	|| update a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods DELETE]	|| /photos/:id	|| destroy	|| delete a specific photo&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
resources :photos&lt;br /&gt;
creates seven different routes in your application, all mapping to the Photos controller:&lt;br /&gt;
&lt;br /&gt;
==Object relational Mapping==&lt;br /&gt;
Rails handles Object Relational mapping using Active Records. ActiveRecords allow a Rails program to deal with objects and itself handles the mapping of these objects to relational databases. In Active Record, &lt;br /&gt;
# Database tables correspond to '''Rails classes'''&lt;br /&gt;
# Database records (rows) correspond to '''Rails objects'''.&lt;br /&gt;
ActiveRecord also allow us to query the database in the object domain by using the Active Record Query Interface&lt;br /&gt;
&lt;br /&gt;
=Developing the Cookbook App=&lt;br /&gt;
We now will look into how to develop the cookbook application, and talk about how to get started with the same. &lt;br /&gt;
To get started, we first create a new rails project called say 'CookBookApp', which is done by simply running the command : &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails new CookBookApp&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Once this is run, a series of folders are created, like app, config, db, etc. In developing the cookbook app, we will mainly look into the app, config and db folders mainly.&lt;br /&gt;
==Scaffolding==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Scaffold_%28programming%29 Scaffolding] is a very powerful command that creates code to create, read, update and delete (CRUD) data from the database. In the Cookbook application, the first scaffold is to create categories. Each Category will then have multiple recipes, and the main functionality of our cookbook app would be done.&lt;br /&gt;
The scaffold command to create categories is done by simply giving the command :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails generate scaffold category name:string&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
As seen, this generates the model called category, a bunch of views, and a category controller.&lt;br /&gt;
It also generates a migration, to create a new table called categories (notice that rails called the table as categories, which is the plural of category automatically).&lt;br /&gt;
===DB Migration===&lt;br /&gt;
The scaffold command generates a lot of files, and also a migration in db\migrate with a name like '''&amp;lt;timestamp&amp;gt;_create_categories.rb'''. This migration contains the details of the table that has to be created called 'categories', so the next step is to run the migrations. Before we do this, however, if we start the rails server, we get an error mentioning that the table 'categories' doesn't exist. &lt;br /&gt;
Let us now run the migration using the command :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After the migration is run, we know that the categories table has been created, and now run the server.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails server&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Creating Categories===&lt;br /&gt;
Now, if we goto the default localhost:3000 page, we see one option : Create a new category, as shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:2.jpg]]&lt;br /&gt;
&lt;br /&gt;
We can click on it, and make a new category, lets say 'Main Course'&lt;br /&gt;
&lt;br /&gt;
[[File:3.jpg]]&lt;br /&gt;
&lt;br /&gt;
When clicking on a new category notice that the URL is '''localhost:3000/categories/new'''. So we are executing the new method of the categories controller. On clicking 'Create Category', a new category with the name 'Main Course' is created, and a flash notice saying 'Successfully Create' is shown. We can now see the category in our list of categories.&lt;br /&gt;
&lt;br /&gt;
[[File:5.jpg]]&lt;br /&gt;
&lt;br /&gt;
So, as we saw, with just a simple scaffold command and db:migrate, we have an application with the basic CRUD functionality.&lt;br /&gt;
&lt;br /&gt;
===Creating Recipes===&lt;br /&gt;
Now, similar to creating categories, we create recipes using the scaffold command. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails generate scaffold recipe title:string description:string instructions:text&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Once this is done, we again have to run a '''db:migrate''', so that the recipes table is in the database.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, the recipes have been created, and as seen in the recipe model, each recipe belongs to a category.&lt;br /&gt;
&lt;br /&gt;
==Relationships==&lt;br /&gt;
Relationships are always defined in the model. We first want to say that each recipe belongs to a category. This can be done in the recipe controller as shown below :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similarly, we want to add a detail mentioning that each Category can have many Recipes : a one to many relationship. To do this, we need to edit the Category model, and include the '''has_many''' relationship with Recipe. The Category model now looks like :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
  has_many :recipes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We also have to add a reference in the migration, to indicate that the recipes table references the categories table. This can be done in the '''&amp;lt;timestamp&amp;gt;_create_recipes.rb''' migration, and now it would look like :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateRecipes &amp;lt; ActiveRecord::Migration&lt;br /&gt;
  def change&lt;br /&gt;
    create_table :recipes do |t|&lt;br /&gt;
      t.string :title&lt;br /&gt;
      t.string :description&lt;br /&gt;
      t.text :instructions&lt;br /&gt;
      t.references :category&lt;br /&gt;
      t.timestamps&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As before, since we have made a change to the migration, we now need to do a '''db:migrate'''.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Valdations==&lt;br /&gt;
Validations, just like Relationships, are also defined in the model. Suppose we want to make sure that each recipe entered has a valid title and instructions, and the condition being that they cannot be empty. We can add this to the Recipe model, which now looks like :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :category&lt;br /&gt;
  validates :title, :presence =&amp;gt; true&lt;br /&gt;
  validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
  validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Thus, the title, instructions and category are all validated, and it makes sure that title and instructions are not empty, and that the category it belongs also exists.&lt;br /&gt;
&lt;br /&gt;
==Routes.rb==&lt;br /&gt;
The '''routes.rb''' file is located in the '''config folder'''. routes.rb, as the name suggests is used to route to the corresponding controller's method. Suppose we have defined a new method in the Category controller. Now, if we want to route a link to the Category controller's new method from some recipe view, we have to include this in the routes.rb file. If we don't, we get an error saying 'No route matches ...'. Whenever we decide to add a new method to a controller, we have to add something similar to :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
match 'Controller_name/new_method_name' =&amp;gt; 'Controller_name#new_method_name'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Our application starts are localhost:3000\categories. Suppose we want to have the root localhost:3000 to the index method in the category controller, we can do:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to =&amp;gt; 'categories#index'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This has to be done after deleting the index.html file from the public directory.&lt;br /&gt;
&lt;br /&gt;
==Views &amp;amp; Layouts==&lt;br /&gt;
Now that the major portion of the application has been done, let us talk about Views and Layouts. &lt;br /&gt;
First, let us add the following code to application.html.erb in the app\views\layouts folder.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to &amp;quot;Show all recipes&amp;quot;, recipes_url %&amp;gt; &amp;lt;%= link_to &amp;quot;Show all categories&amp;quot;, categories_url %&amp;gt; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, when we start the rails server, we see the following page :&lt;br /&gt;
&lt;br /&gt;
[[File:6.jpg]]&lt;br /&gt;
&lt;br /&gt;
Since we want the user to choose a category for each recipe, we include the following code in our view (in the recipes\_form.html.erb in the views section of our app) :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= f.label :category %&amp;gt;&lt;br /&gt;
&amp;lt;%= select(&amp;quot;recipe&amp;quot;, &amp;quot;category_id&amp;quot;, Category.find(:all).collect{ |c| [ c.name, c.id] }) %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view now would have a drop box to choose which category and the user can write the recipe for that category. The new recipe page would now look like :&lt;br /&gt;
&lt;br /&gt;
[[File:7.jpg]]&lt;br /&gt;
&lt;br /&gt;
The CookBookApp now can have many categories, and recipes for each category. Note that we just used a few commands : scaffold, db:migrate, etc. to create this application. Since rails uses the Convention over Configuration principle, it makes it easier to write applications.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
[1] Ruby on Rails Guides: Getting Started With Rails :http://guides.rubyonrails.org/getting_started.html&lt;br /&gt;
&lt;br /&gt;
[2] CookBookApp : http://courses.ncsu.edu/csc517/common/exercises/cookbook3/&lt;br /&gt;
&lt;br /&gt;
[3] CSC 517 Fall 2011 : Engineering Online Video : http://mediasite.online.ncsu.edu/online/Viewer/?peid=5a5b4de717dc4108a1b04133575b22181d&lt;br /&gt;
&lt;br /&gt;
[4] Course Schedule, and Lecture #9 Materials : http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures&lt;br /&gt;
&lt;br /&gt;
[5] Ruby on Rails official site : http://rubyonrails.org/&lt;br /&gt;
&lt;br /&gt;
[6] Sam Ruby, Agile Web Development with Rails, ISBN:1934356549&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[8] Installing Ruby : http://www.ruby-lang.org/en/downloads/&lt;br /&gt;
&lt;br /&gt;
[7] Installing Rails : http://rubyforge.org/projects/instantrails/ &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=External Links=&lt;br /&gt;
[1] Ruby on Rails Tutorial by Michael Hartl : http://ruby.railstutorial.org/&lt;br /&gt;
[2] Ruby on Rails Tutorials : http://www.rubyonrailstutorials.com/&lt;br /&gt;
[3] Audio lessons on Rails : http://www.buildingwebapps.com/learningrails&lt;br /&gt;
[4] Video Lectures on Rails : http://rubyonrails.org/screencasts&lt;br /&gt;
[5]&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=54196</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4d mt</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=54196"/>
		<updated>2011-10-27T17:53:53Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: /* DB Migration */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''' The CookBook Application (Lecture 9)'''&lt;br /&gt;
=Introduction=&lt;br /&gt;
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.Rails includes a simple light weight application server [http://en.wikipedia.org/wiki/WEBrick Webrick] and a build system [http://en.wikipedia.org/wiki/Rake_(software) Rake] which makes development on rails easy.&lt;br /&gt;
&lt;br /&gt;
==Ruby on Rails==&lt;br /&gt;
Ruby on Rails, is an [http://en.wikipedia.org/wiki/Web_application_framework open source web application framework] for the [http://en.wikipedia.org/wiki/Ruby_(programming_language) 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:&lt;br /&gt;
===Convention over Configuration===&lt;br /&gt;
[http://en.wikipedia.org/wiki/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.&lt;br /&gt;
&lt;br /&gt;
===Don't repeat yourself=== &lt;br /&gt;
[http://en.wikipedia.org/wiki/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. Also in ActiveRecord there is no need to specify attribute names since it is picked up from the database tables itself.&lt;br /&gt;
&lt;br /&gt;
==Model/View/Controller Framework==&lt;br /&gt;
All Rails applications are implemented using the [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller 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.&lt;br /&gt;
Key components of MVC architecture include:&lt;br /&gt;
# 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.&lt;br /&gt;
#Views: Views provide a '''human interface(GUI)''' for the model. Views in Rails are implemented using embedded ruby (.html.erb) files&lt;br /&gt;
#Controllers: Controllers process the '''user actions''' and help redirect to the appropriate model ,build a user response and update the View. &lt;br /&gt;
The typical user interaction on a Rails application happens as follows:&lt;br /&gt;
#The user responds to the UI (ex : clicks a button)&lt;br /&gt;
#The router routes the action to the appropriate controller and action in it&lt;br /&gt;
#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&lt;br /&gt;
#A view queries the model and uses its data to build a response .&lt;br /&gt;
#The view updates the UI with the view and waits for further user interaction.&lt;br /&gt;
&lt;br /&gt;
==CRUD==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Create,_read,_update_and_delete 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:&lt;br /&gt;
#Create or add new entries&lt;br /&gt;
#Read, retrieve, search, or view existing entries&lt;br /&gt;
#Update or edit existing entries&lt;br /&gt;
#Delete existing entries&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
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 &amp;quot;contact management&amp;quot; or &amp;quot;contact maintenance&amp;quot; (or &amp;quot;document management&amp;quot; in general, depending on the basic storage unit for the particular application).&lt;br /&gt;
&lt;br /&gt;
==Routing in Rails==&lt;br /&gt;
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: &lt;br /&gt;
resources :photos &lt;br /&gt;
can allow us to specify 7 routes for the application.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! HTTP Verb&lt;br /&gt;
! Path &lt;br /&gt;
! action &lt;br /&gt;
! used for&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods GET]   || /photos || index || display a list of all photos&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods GET] || /photos/new || new  || return an HTML form for creating a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods POST]   ||	/photos	||create||	create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods GET]	||/photos/new ||	new ||	return an HTML form for creating a new photo &lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods POST]	|| /photos	|| create	|| create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods GET]	|| /photos/:id	|| show	|| display a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods GET]	|| /photos/:id/edit	|| edit	|| return an HTML form for editing a photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods PUT]	|| /photos/:id ||	update	|| update a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods DELETE]	|| /photos/:id	|| destroy	|| delete a specific photo&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
resources :photos&lt;br /&gt;
creates seven different routes in your application, all mapping to the Photos controller:&lt;br /&gt;
&lt;br /&gt;
==Object relational Mapping==&lt;br /&gt;
Rails handles Object Relational mapping using Active Records. ActiveRecords allow a Rails program to deal with objects and itself handles the mapping of these objects to relational databases. In Active Record, &lt;br /&gt;
# Database tables correspond to '''Rails classes'''&lt;br /&gt;
# Database records (rows) correspond to '''Rails objects'''.&lt;br /&gt;
ActiveRecord also allow us to query the database in the object domain by using the Active Record Query Interface&lt;br /&gt;
&lt;br /&gt;
=Developing the Cookbook App=&lt;br /&gt;
We now will look into how to develop the cookbook application, and talk about how to get started with the same. &lt;br /&gt;
To get started, we first create a new rails project called say 'CookBookApp', which is done by simply running the command : &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails new CookBookApp&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Once this is run, a series of folders are created, like app, config, db, etc. In developing the cookbook app, we will mainly look into the app, config and db folders mainly.&lt;br /&gt;
==Scaffolding==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Scaffold_%28programming%29 Scaffolding] is a very powerful command that creates code to create, read, update and delete (CRUD) data from the database. In the Cookbook application, the first scaffold is to create categories. Each Category will then have multiple recipes, and the main functionality of our cookbook app would be done.&lt;br /&gt;
The scaffold command to create categories is done by simply giving the command :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails generate scaffold category name:string&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
As seen, this generates the model called category, a bunch of views, and a category controller.&lt;br /&gt;
It also generates a migration, to create a new table called categories (notice that rails called the table as categories, which is the plural of category automatically).&lt;br /&gt;
===DB Migration===&lt;br /&gt;
The scaffold command generates a lot of files, and also a migration in db\migrate with a name like '''&amp;lt;timestamp&amp;gt;_create_categories.rb'''. This migration contains the details of the table that has to be created called 'categories', so the next step is to run the migrations. Before we do this, however, if we start the rails server, we get an error mentioning that the table 'categories' doesn't exist. &lt;br /&gt;
Let us now run the migration using the command :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After the migration is run, we know that the categories table has been created, and now run the server.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails server&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Creating Categories===&lt;br /&gt;
Now, if we goto the default localhost:3000 page, we see one option : Create a new category, as shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:2.jpg]]&lt;br /&gt;
&lt;br /&gt;
We can click on it, and make a new category, lets say 'Main Course'&lt;br /&gt;
&lt;br /&gt;
[[File:3.jpg]]&lt;br /&gt;
&lt;br /&gt;
When clicking on a new category notice that the URL is '''localhost:3000/categories/new'''. So we are executing the new method of the categories controller. On clicking 'Create Category', a new category with the name 'Main Course' is created, and a flash notice saying 'Successfully Create' is shown. We can now see the category in our list of categories.&lt;br /&gt;
&lt;br /&gt;
[[File:5.jpg]]&lt;br /&gt;
&lt;br /&gt;
So, as we saw, with just a simple scaffold command and db:migrate, we have an application with the basic CRUD functionality.&lt;br /&gt;
&lt;br /&gt;
===Creating Recipes===&lt;br /&gt;
Now, similar to creating categories, we create recipes using the scaffold command. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails generate scaffold recipe title:string description:string instructions:text&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Once this is done, we again have to run a '''db:migrate''', so that the recipes table is in the database.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, the recipes have been created, and as seen in the recipe model, each recipe belongs to a category.&lt;br /&gt;
&lt;br /&gt;
==Relationships==&lt;br /&gt;
Relationships are always defined in the model. We first want to say that each recipe belongs to a category. This can be done in the recipe controller as shown below :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similarly, we want to add a detail mentioning that each Category can have many Recipes : a one to many relationship. To do this, we need to edit the Category model, and include the '''has_many''' relationship with Recipe. The Category model now looks like :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
  has_many :recipes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We also have to add a reference in the migration, to indicate that the recipes table references the categories table. This can be done in the '''&amp;lt;timestamp&amp;gt;_create_recipes.rb''' migration, and now it would look like :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateRecipes &amp;lt; ActiveRecord::Migration&lt;br /&gt;
  def change&lt;br /&gt;
    create_table :recipes do |t|&lt;br /&gt;
      t.string :title&lt;br /&gt;
      t.string :description&lt;br /&gt;
      t.text :instructions&lt;br /&gt;
      t.references :category&lt;br /&gt;
      t.timestamps&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As before, since we have made a change to the migration, we now need to do a '''db:migrate'''.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Valdations==&lt;br /&gt;
Validations, just like Relationships, are also defined in the model. Suppose we want to make sure that each recipe entered has a valid title and instructions, and the condition being that they cannot be empty. We can add this to the Recipe model, which now looks like :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :category&lt;br /&gt;
  validates :title, :presence =&amp;gt; true&lt;br /&gt;
  validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
  validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Thus, the title, instructions and category are all validated, and it makes sure that title and instructions are not empty, and that the category it belongs also exists.&lt;br /&gt;
&lt;br /&gt;
==Routes.rb==&lt;br /&gt;
The '''routes.rb''' file is located in the '''config folder'''. routes.rb, as the name suggests is used to route to the corresponding controller's method. Suppose we have defined a new method in the Category controller. Now, if we want to route a link to the Category controller's new method from some recipe view, we have to include this in the routes.rb file. If we don't, we get an error saying 'No route matches ...'. Whenever we decide to add a new method to a controller, we have to add something similar to :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
match 'Controller_name/new_method_name' =&amp;gt; 'Controller_name#new_method_name'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Our application starts are localhost:3000\categories. Suppose we want to have the root localhost:3000 to the index method in the category controller, we can do:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to =&amp;gt; 'categories#index'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This has to be done after deleting the index.html file from the public directory.&lt;br /&gt;
&lt;br /&gt;
==Views &amp;amp; Layouts==&lt;br /&gt;
Now that the major portion of the application has been done, let us talk about Views and Layouts. &lt;br /&gt;
First, let us add the following code to application.html.erb in the app\views\layouts folder.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to &amp;quot;Show all recipes&amp;quot;, recipes_url %&amp;gt; &amp;lt;%= link_to &amp;quot;Show all categories&amp;quot;, categories_url %&amp;gt; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, when we start the rails server, we see the following page :&lt;br /&gt;
&lt;br /&gt;
[[File:6.jpg]]&lt;br /&gt;
&lt;br /&gt;
Since we want the user to choose a category for each recipe, we include the following code in our view (in the recipes\_form.html.erb in the views section of our app) :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= f.label :category %&amp;gt;&lt;br /&gt;
&amp;lt;%= select(&amp;quot;recipe&amp;quot;, &amp;quot;category_id&amp;quot;, Category.find(:all).collect{ |c| [ c.name, c.id] }) %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view now would have a drop box to choose which category and the user can write the recipe for that category. The new recipe page would now look like :&lt;br /&gt;
&lt;br /&gt;
[[File:7.jpg]]&lt;br /&gt;
&lt;br /&gt;
The CookBookApp now can have many categories, and recipes for each category. Note that we just used a few commands : scaffold, db:migrate, etc. to create this application. Since rails uses the Convention over Configuration principle, it makes it easier to write applications.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
[1] Ruby on Rails Guides: Getting Started With Rails :http://guides.rubyonrails.org/getting_started.html&lt;br /&gt;
&lt;br /&gt;
[2] CookBookApp : http://courses.ncsu.edu/csc517/common/exercises/cookbook3/&lt;br /&gt;
&lt;br /&gt;
[3] CSC 517 Fall 2011 : Engineering Online Video : http://mediasite.online.ncsu.edu/online/Viewer/?peid=5a5b4de717dc4108a1b04133575b22181d&lt;br /&gt;
&lt;br /&gt;
[4] Course Schedule, and Lecture #9 Materials : http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures&lt;br /&gt;
&lt;br /&gt;
[5] Ruby on Rails official site : http://rubyonrails.org/&lt;br /&gt;
&lt;br /&gt;
[6] Sam Ruby, Agile Web Development with Rails, ISBN:1934356549&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=54195</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4d mt</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=54195"/>
		<updated>2011-10-27T17:53:10Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: /* Routing in Rails */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''' The CookBook Application (Lecture 9)'''&lt;br /&gt;
=Introduction=&lt;br /&gt;
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.Rails includes a simple light weight application server [http://en.wikipedia.org/wiki/WEBrick Webrick] and a build system [http://en.wikipedia.org/wiki/Rake_(software) Rake] which makes development on rails easy.&lt;br /&gt;
&lt;br /&gt;
==Ruby on Rails==&lt;br /&gt;
Ruby on Rails, is an [http://en.wikipedia.org/wiki/Web_application_framework open source web application framework] for the [http://en.wikipedia.org/wiki/Ruby_(programming_language) 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:&lt;br /&gt;
===Convention over Configuration===&lt;br /&gt;
[http://en.wikipedia.org/wiki/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.&lt;br /&gt;
&lt;br /&gt;
===Don't repeat yourself=== &lt;br /&gt;
[http://en.wikipedia.org/wiki/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. Also in ActiveRecord there is no need to specify attribute names since it is picked up from the database tables itself.&lt;br /&gt;
&lt;br /&gt;
==Model/View/Controller Framework==&lt;br /&gt;
All Rails applications are implemented using the [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller 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.&lt;br /&gt;
Key components of MVC architecture include:&lt;br /&gt;
# 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.&lt;br /&gt;
#Views: Views provide a '''human interface(GUI)''' for the model. Views in Rails are implemented using embedded ruby (.html.erb) files&lt;br /&gt;
#Controllers: Controllers process the '''user actions''' and help redirect to the appropriate model ,build a user response and update the View. &lt;br /&gt;
The typical user interaction on a Rails application happens as follows:&lt;br /&gt;
#The user responds to the UI (ex : clicks a button)&lt;br /&gt;
#The router routes the action to the appropriate controller and action in it&lt;br /&gt;
#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&lt;br /&gt;
#A view queries the model and uses its data to build a response .&lt;br /&gt;
#The view updates the UI with the view and waits for further user interaction.&lt;br /&gt;
&lt;br /&gt;
==CRUD==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Create,_read,_update_and_delete 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:&lt;br /&gt;
#Create or add new entries&lt;br /&gt;
#Read, retrieve, search, or view existing entries&lt;br /&gt;
#Update or edit existing entries&lt;br /&gt;
#Delete existing entries&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
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 &amp;quot;contact management&amp;quot; or &amp;quot;contact maintenance&amp;quot; (or &amp;quot;document management&amp;quot; in general, depending on the basic storage unit for the particular application).&lt;br /&gt;
&lt;br /&gt;
==Routing in Rails==&lt;br /&gt;
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: &lt;br /&gt;
resources :photos &lt;br /&gt;
can allow us to specify 7 routes for the application.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! HTTP Verb&lt;br /&gt;
! Path &lt;br /&gt;
! action &lt;br /&gt;
! used for&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods GET]   || /photos || index || display a list of all photos&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods GET] || /photos/new || new  || return an HTML form for creating a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods POST]   ||	/photos	||create||	create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods GET]	||/photos/new ||	new ||	return an HTML form for creating a new photo &lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods POST]	|| /photos	|| create	|| create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods GET]	|| /photos/:id	|| show	|| display a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods GET]	|| /photos/:id/edit	|| edit	|| return an HTML form for editing a photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods PUT]	|| /photos/:id ||	update	|| update a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods DELETE]	|| /photos/:id	|| destroy	|| delete a specific photo&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
resources :photos&lt;br /&gt;
creates seven different routes in your application, all mapping to the Photos controller:&lt;br /&gt;
&lt;br /&gt;
==Object relational Mapping==&lt;br /&gt;
Rails handles Object Relational mapping using Active Records. ActiveRecords allow a Rails program to deal with objects and itself handles the mapping of these objects to relational databases. In Active Record, &lt;br /&gt;
# Database tables correspond to '''Rails classes'''&lt;br /&gt;
# Database records (rows) correspond to '''Rails objects'''.&lt;br /&gt;
ActiveRecord also allow us to query the database in the object domain by using the Active Record Query Interface&lt;br /&gt;
&lt;br /&gt;
=Developing the Cookbook App=&lt;br /&gt;
We now will look into how to develop the cookbook application, and talk about how to get started with the same. &lt;br /&gt;
To get started, we first create a new rails project called say 'CookBookApp', which is done by simply running the command : &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails new CookBookApp&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Once this is run, a series of folders are created, like app, config, db, etc. In developing the cookbook app, we will mainly look into the app, config and db folders mainly.&lt;br /&gt;
==Scaffolding==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Scaffold_%28programming%29 Scaffolding] is a very powerful command that creates code to create, read, update and delete (CRUD) data from the database. In the Cookbook application, the first scaffold is to create categories. Each Category will then have multiple recipes, and the main functionality of our cookbook app would be done.&lt;br /&gt;
The scaffold command to create categories is done by simply giving the command :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails generate scaffold category name:string&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
As seen, this generates the model called category, a bunch of views, and a category controller.&lt;br /&gt;
It also generates a migration, to create a new table called categories (notice that rails called the table as categories, which is the plural of category automatically).&lt;br /&gt;
===DB Migration===&lt;br /&gt;
The scaffold command generates a lot of files, and also a migration in db\migrate with a name like &amp;lt;timestamp&amp;gt;_create_categories.rb. This migration contains the details of the table that has to be created called 'categories', so the next step is to run the migrations. Before we do this, however, if we start the rails server, we get an error mentioning that the table 'categories' doesn't exist. &lt;br /&gt;
Let us now run the migration using the command :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After the migration is run, we know that the categories table has been created, and now run the server.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails server&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
===Creating Categories===&lt;br /&gt;
Now, if we goto the default localhost:3000 page, we see one option : Create a new category, as shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:2.jpg]]&lt;br /&gt;
&lt;br /&gt;
We can click on it, and make a new category, lets say 'Main Course'&lt;br /&gt;
&lt;br /&gt;
[[File:3.jpg]]&lt;br /&gt;
&lt;br /&gt;
When clicking on a new category notice that the URL is '''localhost:3000/categories/new'''. So we are executing the new method of the categories controller. On clicking 'Create Category', a new category with the name 'Main Course' is created, and a flash notice saying 'Successfully Create' is shown. We can now see the category in our list of categories.&lt;br /&gt;
&lt;br /&gt;
[[File:5.jpg]]&lt;br /&gt;
&lt;br /&gt;
So, as we saw, with just a simple scaffold command and db:migrate, we have an application with the basic CRUD functionality.&lt;br /&gt;
&lt;br /&gt;
===Creating Recipes===&lt;br /&gt;
Now, similar to creating categories, we create recipes using the scaffold command. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails generate scaffold recipe title:string description:string instructions:text&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Once this is done, we again have to run a '''db:migrate''', so that the recipes table is in the database.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, the recipes have been created, and as seen in the recipe model, each recipe belongs to a category.&lt;br /&gt;
&lt;br /&gt;
==Relationships==&lt;br /&gt;
Relationships are always defined in the model. We first want to say that each recipe belongs to a category. This can be done in the recipe controller as shown below :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similarly, we want to add a detail mentioning that each Category can have many Recipes : a one to many relationship. To do this, we need to edit the Category model, and include the '''has_many''' relationship with Recipe. The Category model now looks like :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
  has_many :recipes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We also have to add a reference in the migration, to indicate that the recipes table references the categories table. This can be done in the '''&amp;lt;timestamp&amp;gt;_create_recipes.rb''' migration, and now it would look like :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateRecipes &amp;lt; ActiveRecord::Migration&lt;br /&gt;
  def change&lt;br /&gt;
    create_table :recipes do |t|&lt;br /&gt;
      t.string :title&lt;br /&gt;
      t.string :description&lt;br /&gt;
      t.text :instructions&lt;br /&gt;
      t.references :category&lt;br /&gt;
      t.timestamps&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As before, since we have made a change to the migration, we now need to do a '''db:migrate'''.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Valdations==&lt;br /&gt;
Validations, just like Relationships, are also defined in the model. Suppose we want to make sure that each recipe entered has a valid title and instructions, and the condition being that they cannot be empty. We can add this to the Recipe model, which now looks like :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :category&lt;br /&gt;
  validates :title, :presence =&amp;gt; true&lt;br /&gt;
  validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
  validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Thus, the title, instructions and category are all validated, and it makes sure that title and instructions are not empty, and that the category it belongs also exists.&lt;br /&gt;
&lt;br /&gt;
==Routes.rb==&lt;br /&gt;
The '''routes.rb''' file is located in the '''config folder'''. routes.rb, as the name suggests is used to route to the corresponding controller's method. Suppose we have defined a new method in the Category controller. Now, if we want to route a link to the Category controller's new method from some recipe view, we have to include this in the routes.rb file. If we don't, we get an error saying 'No route matches ...'. Whenever we decide to add a new method to a controller, we have to add something similar to :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
match 'Controller_name/new_method_name' =&amp;gt; 'Controller_name#new_method_name'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Our application starts are localhost:3000\categories. Suppose we want to have the root localhost:3000 to the index method in the category controller, we can do:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to =&amp;gt; 'categories#index'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This has to be done after deleting the index.html file from the public directory.&lt;br /&gt;
&lt;br /&gt;
==Views &amp;amp; Layouts==&lt;br /&gt;
Now that the major portion of the application has been done, let us talk about Views and Layouts. &lt;br /&gt;
First, let us add the following code to application.html.erb in the app\views\layouts folder.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to &amp;quot;Show all recipes&amp;quot;, recipes_url %&amp;gt; &amp;lt;%= link_to &amp;quot;Show all categories&amp;quot;, categories_url %&amp;gt; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, when we start the rails server, we see the following page :&lt;br /&gt;
&lt;br /&gt;
[[File:6.jpg]]&lt;br /&gt;
&lt;br /&gt;
Since we want the user to choose a category for each recipe, we include the following code in our view (in the recipes\_form.html.erb in the views section of our app) :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= f.label :category %&amp;gt;&lt;br /&gt;
&amp;lt;%= select(&amp;quot;recipe&amp;quot;, &amp;quot;category_id&amp;quot;, Category.find(:all).collect{ |c| [ c.name, c.id] }) %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view now would have a drop box to choose which category and the user can write the recipe for that category. The new recipe page would now look like :&lt;br /&gt;
&lt;br /&gt;
[[File:7.jpg]]&lt;br /&gt;
&lt;br /&gt;
The CookBookApp now can have many categories, and recipes for each category. Note that we just used a few commands : scaffold, db:migrate, etc. to create this application. Since rails uses the Convention over Configuration principle, it makes it easier to write applications.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
[1] Ruby on Rails Guides: Getting Started With Rails :http://guides.rubyonrails.org/getting_started.html&lt;br /&gt;
&lt;br /&gt;
[2] CookBookApp : http://courses.ncsu.edu/csc517/common/exercises/cookbook3/&lt;br /&gt;
&lt;br /&gt;
[3] CSC 517 Fall 2011 : Engineering Online Video : http://mediasite.online.ncsu.edu/online/Viewer/?peid=5a5b4de717dc4108a1b04133575b22181d&lt;br /&gt;
&lt;br /&gt;
[4] Course Schedule, and Lecture #9 Materials : http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures&lt;br /&gt;
&lt;br /&gt;
[5] Ruby on Rails official site : http://rubyonrails.org/&lt;br /&gt;
&lt;br /&gt;
[6] Sam Ruby, Agile Web Development with Rails, ISBN:1934356549&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=54194</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4d mt</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=54194"/>
		<updated>2011-10-27T17:49:04Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: /* CRUD */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''' The CookBook Application (Lecture 9)'''&lt;br /&gt;
=Introduction=&lt;br /&gt;
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.Rails includes a simple light weight application server [http://en.wikipedia.org/wiki/WEBrick Webrick] and a build system [http://en.wikipedia.org/wiki/Rake_(software) Rake] which makes development on rails easy.&lt;br /&gt;
&lt;br /&gt;
==Ruby on Rails==&lt;br /&gt;
Ruby on Rails, is an [http://en.wikipedia.org/wiki/Web_application_framework open source web application framework] for the [http://en.wikipedia.org/wiki/Ruby_(programming_language) 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:&lt;br /&gt;
===Convention over Configuration===&lt;br /&gt;
[http://en.wikipedia.org/wiki/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.&lt;br /&gt;
&lt;br /&gt;
===Don't repeat yourself=== &lt;br /&gt;
[http://en.wikipedia.org/wiki/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. Also in ActiveRecord there is no need to specify attribute names since it is picked up from the database tables itself.&lt;br /&gt;
&lt;br /&gt;
==Model/View/Controller Framework==&lt;br /&gt;
All Rails applications are implemented using the [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller 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.&lt;br /&gt;
Key components of MVC architecture include:&lt;br /&gt;
# 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.&lt;br /&gt;
#Views: Views provide a '''human interface(GUI)''' for the model. Views in Rails are implemented using embedded ruby (.html.erb) files&lt;br /&gt;
#Controllers: Controllers process the '''user actions''' and help redirect to the appropriate model ,build a user response and update the View. &lt;br /&gt;
The typical user interaction on a Rails application happens as follows:&lt;br /&gt;
#The user responds to the UI (ex : clicks a button)&lt;br /&gt;
#The router routes the action to the appropriate controller and action in it&lt;br /&gt;
#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&lt;br /&gt;
#A view queries the model and uses its data to build a response .&lt;br /&gt;
#The view updates the UI with the view and waits for further user interaction.&lt;br /&gt;
&lt;br /&gt;
==CRUD==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Create,_read,_update_and_delete 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:&lt;br /&gt;
#Create or add new entries&lt;br /&gt;
#Read, retrieve, search, or view existing entries&lt;br /&gt;
#Update or edit existing entries&lt;br /&gt;
#Delete existing entries&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
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 &amp;quot;contact management&amp;quot; or &amp;quot;contact maintenance&amp;quot; (or &amp;quot;document management&amp;quot; in general, depending on the basic storage unit for the particular application).&lt;br /&gt;
&lt;br /&gt;
==Routing in Rails==&lt;br /&gt;
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: &lt;br /&gt;
resources :photos &lt;br /&gt;
can allow us to specify 7 routes for the application.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! HTTP Verb&lt;br /&gt;
! Path &lt;br /&gt;
! action &lt;br /&gt;
! used for&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos || index || display a list of all photos&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos/new || new  || return an HTML form for creating a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  POST   ||	/photos	||create||	create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	||/photos/new ||	new ||	return an HTML form for creating a new photo &lt;br /&gt;
|-&lt;br /&gt;
|  POST	|| /photos	|| create	|| create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id	|| show	|| display a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id/edit	|| edit	|| return an HTML form for editing a photo&lt;br /&gt;
|-&lt;br /&gt;
|  PUT	|| /photos/:id ||	update	|| update a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  DELETE	|| /photos/:id	|| destroy	|| delete a specific photo&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
resources :photos&lt;br /&gt;
creates seven different routes in your application, all mapping to the Photos controller:&lt;br /&gt;
&lt;br /&gt;
==Object relational Mapping==&lt;br /&gt;
Rails handles Object Relational mapping using Active Records. ActiveRecords allow a Rails program to deal with objects and itself handles the mapping of these objects to relational databases. In Active Record, &lt;br /&gt;
# Database tables correspond to '''Rails classes'''&lt;br /&gt;
# Database records (rows) correspond to '''Rails objects'''.&lt;br /&gt;
ActiveRecord also allow us to query the database in the object domain by using the Active Record Query Interface&lt;br /&gt;
&lt;br /&gt;
=Developing the Cookbook App=&lt;br /&gt;
We now will look into how to develop the cookbook application, and talk about how to get started with the same. &lt;br /&gt;
To get started, we first create a new rails project called say 'CookBookApp', which is done by simply running the command : &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails new CookBookApp&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Once this is run, a series of folders are created, like app, config, db, etc. In developing the cookbook app, we will mainly look into the app, config and db folders mainly.&lt;br /&gt;
==Scaffolding==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Scaffold_%28programming%29 Scaffolding] is a very powerful command that creates code to create, read, update and delete (CRUD) data from the database. In the Cookbook application, the first scaffold is to create categories. Each Category will then have multiple recipes, and the main functionality of our cookbook app would be done.&lt;br /&gt;
The scaffold command to create categories is done by simply giving the command :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails generate scaffold category name:string&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
As seen, this generates the model called category, a bunch of views, and a category controller.&lt;br /&gt;
It also generates a migration, to create a new table called categories (notice that rails called the table as categories, which is the plural of category automatically).&lt;br /&gt;
===DB Migration===&lt;br /&gt;
The scaffold command generates a lot of files, and also a migration in db\migrate with a name like &amp;lt;timestamp&amp;gt;_create_categories.rb. This migration contains the details of the table that has to be created called 'categories', so the next step is to run the migrations. Before we do this, however, if we start the rails server, we get an error mentioning that the table 'categories' doesn't exist. &lt;br /&gt;
Let us now run the migration using the command :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After the migration is run, we know that the categories table has been created, and now run the server.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails server&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
===Creating Categories===&lt;br /&gt;
Now, if we goto the default localhost:3000 page, we see one option : Create a new category, as shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:2.jpg]]&lt;br /&gt;
&lt;br /&gt;
We can click on it, and make a new category, lets say 'Main Course'&lt;br /&gt;
&lt;br /&gt;
[[File:3.jpg]]&lt;br /&gt;
&lt;br /&gt;
When clicking on a new category notice that the URL is '''localhost:3000/categories/new'''. So we are executing the new method of the categories controller. On clicking 'Create Category', a new category with the name 'Main Course' is created, and a flash notice saying 'Successfully Create' is shown. We can now see the category in our list of categories.&lt;br /&gt;
&lt;br /&gt;
[[File:5.jpg]]&lt;br /&gt;
&lt;br /&gt;
So, as we saw, with just a simple scaffold command and db:migrate, we have an application with the basic CRUD functionality.&lt;br /&gt;
&lt;br /&gt;
===Creating Recipes===&lt;br /&gt;
Now, similar to creating categories, we create recipes using the scaffold command. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails generate scaffold recipe title:string description:string instructions:text&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Once this is done, we again have to run a '''db:migrate''', so that the recipes table is in the database.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, the recipes have been created, and as seen in the recipe model, each recipe belongs to a category.&lt;br /&gt;
&lt;br /&gt;
==Relationships==&lt;br /&gt;
Relationships are always defined in the model. We first want to say that each recipe belongs to a category. This can be done in the recipe controller as shown below :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similarly, we want to add a detail mentioning that each Category can have many Recipes : a one to many relationship. To do this, we need to edit the Category model, and include the '''has_many''' relationship with Recipe. The Category model now looks like :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
  has_many :recipes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We also have to add a reference in the migration, to indicate that the recipes table references the categories table. This can be done in the '''&amp;lt;timestamp&amp;gt;_create_recipes.rb''' migration, and now it would look like :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateRecipes &amp;lt; ActiveRecord::Migration&lt;br /&gt;
  def change&lt;br /&gt;
    create_table :recipes do |t|&lt;br /&gt;
      t.string :title&lt;br /&gt;
      t.string :description&lt;br /&gt;
      t.text :instructions&lt;br /&gt;
      t.references :category&lt;br /&gt;
      t.timestamps&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As before, since we have made a change to the migration, we now need to do a '''db:migrate'''.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Valdations==&lt;br /&gt;
Validations, just like Relationships, are also defined in the model. Suppose we want to make sure that each recipe entered has a valid title and instructions, and the condition being that they cannot be empty. We can add this to the Recipe model, which now looks like :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :category&lt;br /&gt;
  validates :title, :presence =&amp;gt; true&lt;br /&gt;
  validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
  validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Thus, the title, instructions and category are all validated, and it makes sure that title and instructions are not empty, and that the category it belongs also exists.&lt;br /&gt;
&lt;br /&gt;
==Routes.rb==&lt;br /&gt;
The '''routes.rb''' file is located in the '''config folder'''. routes.rb, as the name suggests is used to route to the corresponding controller's method. Suppose we have defined a new method in the Category controller. Now, if we want to route a link to the Category controller's new method from some recipe view, we have to include this in the routes.rb file. If we don't, we get an error saying 'No route matches ...'. Whenever we decide to add a new method to a controller, we have to add something similar to :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
match 'Controller_name/new_method_name' =&amp;gt; 'Controller_name#new_method_name'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Our application starts are localhost:3000\categories. Suppose we want to have the root localhost:3000 to the index method in the category controller, we can do:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to =&amp;gt; 'categories#index'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This has to be done after deleting the index.html file from the public directory.&lt;br /&gt;
&lt;br /&gt;
==Views &amp;amp; Layouts==&lt;br /&gt;
Now that the major portion of the application has been done, let us talk about Views and Layouts. &lt;br /&gt;
First, let us add the following code to application.html.erb in the app\views\layouts folder.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to &amp;quot;Show all recipes&amp;quot;, recipes_url %&amp;gt; &amp;lt;%= link_to &amp;quot;Show all categories&amp;quot;, categories_url %&amp;gt; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, when we start the rails server, we see the following page :&lt;br /&gt;
&lt;br /&gt;
[[File:6.jpg]]&lt;br /&gt;
&lt;br /&gt;
Since we want the user to choose a category for each recipe, we include the following code in our view (in the recipes\_form.html.erb in the views section of our app) :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= f.label :category %&amp;gt;&lt;br /&gt;
&amp;lt;%= select(&amp;quot;recipe&amp;quot;, &amp;quot;category_id&amp;quot;, Category.find(:all).collect{ |c| [ c.name, c.id] }) %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view now would have a drop box to choose which category and the user can write the recipe for that category. The new recipe page would now look like :&lt;br /&gt;
&lt;br /&gt;
[[File:7.jpg]]&lt;br /&gt;
&lt;br /&gt;
The CookBookApp now can have many categories, and recipes for each category. Note that we just used a few commands : scaffold, db:migrate, etc. to create this application. Since rails uses the Convention over Configuration principle, it makes it easier to write applications.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
[1] Ruby on Rails Guides: Getting Started With Rails :http://guides.rubyonrails.org/getting_started.html&lt;br /&gt;
&lt;br /&gt;
[2] CookBookApp : http://courses.ncsu.edu/csc517/common/exercises/cookbook3/&lt;br /&gt;
&lt;br /&gt;
[3] CSC 517 Fall 2011 : Engineering Online Video : http://mediasite.online.ncsu.edu/online/Viewer/?peid=5a5b4de717dc4108a1b04133575b22181d&lt;br /&gt;
&lt;br /&gt;
[4] Course Schedule, and Lecture #9 Materials : http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures&lt;br /&gt;
&lt;br /&gt;
[5] Ruby on Rails official site : http://rubyonrails.org/&lt;br /&gt;
&lt;br /&gt;
[6] Sam Ruby, Agile Web Development with Rails, ISBN:1934356549&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=54193</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4d mt</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=54193"/>
		<updated>2011-10-27T17:48:32Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: /* Object relational Mapping */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''' The CookBook Application (Lecture 9)'''&lt;br /&gt;
=Introduction=&lt;br /&gt;
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.Rails includes a simple light weight application server [http://en.wikipedia.org/wiki/WEBrick Webrick] and a build system [http://en.wikipedia.org/wiki/Rake_(software) Rake] which makes development on rails easy.&lt;br /&gt;
&lt;br /&gt;
==Ruby on Rails==&lt;br /&gt;
Ruby on Rails, is an [http://en.wikipedia.org/wiki/Web_application_framework open source web application framework] for the [http://en.wikipedia.org/wiki/Ruby_(programming_language) 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:&lt;br /&gt;
===Convention over Configuration===&lt;br /&gt;
[http://en.wikipedia.org/wiki/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.&lt;br /&gt;
&lt;br /&gt;
===Don't repeat yourself=== &lt;br /&gt;
[http://en.wikipedia.org/wiki/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. Also in ActiveRecord there is no need to specify attribute names since it is picked up from the database tables itself.&lt;br /&gt;
&lt;br /&gt;
==Model/View/Controller Framework==&lt;br /&gt;
All Rails applications are implemented using the [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller 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.&lt;br /&gt;
Key components of MVC architecture include:&lt;br /&gt;
# 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.&lt;br /&gt;
#Views: Views provide a '''human interface(GUI)''' for the model. Views in Rails are implemented using embedded ruby (.html.erb) files&lt;br /&gt;
#Controllers: Controllers process the '''user actions''' and help redirect to the appropriate model ,build a user response and update the View. &lt;br /&gt;
The typical user interaction on a Rails application happens as follows:&lt;br /&gt;
#The user responds to the UI (ex : clicks a button)&lt;br /&gt;
#The router routes the action to the appropriate controller and action in it&lt;br /&gt;
#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&lt;br /&gt;
#A view queries the model and uses its data to build a response .&lt;br /&gt;
#The view updates the UI with the view and waits for further user interaction.&lt;br /&gt;
&lt;br /&gt;
==CRUD==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Create,_read,_update_and_delete 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:&lt;br /&gt;
#Create or add new entries&lt;br /&gt;
#Read, retrieve, search, or view existing entries&lt;br /&gt;
#Update or edit existing entries&lt;br /&gt;
#Delete existing entries&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
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 &amp;quot;contact management&amp;quot; or &amp;quot;contact maintenance&amp;quot; (or &amp;quot;document management&amp;quot; in general, depending on the basic storage unit for the particular application).&lt;br /&gt;
&lt;br /&gt;
==Routing in Rails==&lt;br /&gt;
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: &lt;br /&gt;
resources :photos &lt;br /&gt;
can allow us to specify 7 routes for the application.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! HTTP Verb&lt;br /&gt;
! Path &lt;br /&gt;
! action &lt;br /&gt;
! used for&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos || index || display a list of all photos&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos/new || new  || return an HTML form for creating a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  POST   ||	/photos	||create||	create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	||/photos/new ||	new ||	return an HTML form for creating a new photo &lt;br /&gt;
|-&lt;br /&gt;
|  POST	|| /photos	|| create	|| create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id	|| show	|| display a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id/edit	|| edit	|| return an HTML form for editing a photo&lt;br /&gt;
|-&lt;br /&gt;
|  PUT	|| /photos/:id ||	update	|| update a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  DELETE	|| /photos/:id	|| destroy	|| delete a specific photo&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
resources :photos&lt;br /&gt;
creates seven different routes in your application, all mapping to the Photos controller:&lt;br /&gt;
&lt;br /&gt;
==Object relational Mapping==&lt;br /&gt;
Rails handles Object Relational mapping using Active Records. ActiveRecords allow a Rails program to deal with objects and itself handles the mapping of these objects to relational databases. In Active Record, &lt;br /&gt;
# Database tables correspond to '''Rails classes'''&lt;br /&gt;
# Database records (rows) correspond to '''Rails objects'''.&lt;br /&gt;
ActiveRecord also allow us to query the database in the object domain by using the Active Record Query Interface&lt;br /&gt;
&lt;br /&gt;
=Developing the Cookbook App=&lt;br /&gt;
We now will look into how to develop the cookbook application, and talk about how to get started with the same. &lt;br /&gt;
To get started, we first create a new rails project called say 'CookBookApp', which is done by simply running the command : &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails new CookBookApp&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Once this is run, a series of folders are created, like app, config, db, etc. In developing the cookbook app, we will mainly look into the app, config and db folders mainly.&lt;br /&gt;
==Scaffolding==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Scaffold_%28programming%29 Scaffolding] is a very powerful command that creates code to create, read, update and delete (CRUD) data from the database. In the Cookbook application, the first scaffold is to create categories. Each Category will then have multiple recipes, and the main functionality of our cookbook app would be done.&lt;br /&gt;
The scaffold command to create categories is done by simply giving the command :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails generate scaffold category name:string&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
As seen, this generates the model called category, a bunch of views, and a category controller.&lt;br /&gt;
It also generates a migration, to create a new table called categories (notice that rails called the table as categories, which is the plural of category automatically).&lt;br /&gt;
===DB Migration===&lt;br /&gt;
The scaffold command generates a lot of files, and also a migration in db\migrate with a name like &amp;lt;timestamp&amp;gt;_create_categories.rb. This migration contains the details of the table that has to be created called 'categories', so the next step is to run the migrations. Before we do this, however, if we start the rails server, we get an error mentioning that the table 'categories' doesn't exist. &lt;br /&gt;
Let us now run the migration using the command :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After the migration is run, we know that the categories table has been created, and now run the server.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails server&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
===Creating Categories===&lt;br /&gt;
Now, if we goto the default localhost:3000 page, we see one option : Create a new category, as shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:2.jpg]]&lt;br /&gt;
&lt;br /&gt;
We can click on it, and make a new category, lets say 'Main Course'&lt;br /&gt;
&lt;br /&gt;
[[File:3.jpg]]&lt;br /&gt;
&lt;br /&gt;
When clicking on a new category notice that the URL is '''localhost:3000/categories/new'''. So we are executing the new method of the categories controller. On clicking 'Create Category', a new category with the name 'Main Course' is created, and a flash notice saying 'Successfully Create' is shown. We can now see the category in our list of categories.&lt;br /&gt;
&lt;br /&gt;
[[File:5.jpg]]&lt;br /&gt;
&lt;br /&gt;
So, as we saw, with just a simple scaffold command and db:migrate, we have an application with the basic CRUD functionality.&lt;br /&gt;
&lt;br /&gt;
===Creating Recipes===&lt;br /&gt;
Now, similar to creating categories, we create recipes using the scaffold command. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails generate scaffold recipe title:string description:string instructions:text&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Once this is done, we again have to run a '''db:migrate''', so that the recipes table is in the database.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, the recipes have been created, and as seen in the recipe model, each recipe belongs to a category.&lt;br /&gt;
&lt;br /&gt;
==Relationships==&lt;br /&gt;
Relationships are always defined in the model. We first want to say that each recipe belongs to a category. This can be done in the recipe controller as shown below :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similarly, we want to add a detail mentioning that each Category can have many Recipes : a one to many relationship. To do this, we need to edit the Category model, and include the '''has_many''' relationship with Recipe. The Category model now looks like :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
  has_many :recipes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We also have to add a reference in the migration, to indicate that the recipes table references the categories table. This can be done in the '''&amp;lt;timestamp&amp;gt;_create_recipes.rb''' migration, and now it would look like :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateRecipes &amp;lt; ActiveRecord::Migration&lt;br /&gt;
  def change&lt;br /&gt;
    create_table :recipes do |t|&lt;br /&gt;
      t.string :title&lt;br /&gt;
      t.string :description&lt;br /&gt;
      t.text :instructions&lt;br /&gt;
      t.references :category&lt;br /&gt;
      t.timestamps&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As before, since we have made a change to the migration, we now need to do a '''db:migrate'''.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Valdations==&lt;br /&gt;
Validations, just like Relationships, are also defined in the model. Suppose we want to make sure that each recipe entered has a valid title and instructions, and the condition being that they cannot be empty. We can add this to the Recipe model, which now looks like :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :category&lt;br /&gt;
  validates :title, :presence =&amp;gt; true&lt;br /&gt;
  validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
  validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Thus, the title, instructions and category are all validated, and it makes sure that title and instructions are not empty, and that the category it belongs also exists.&lt;br /&gt;
&lt;br /&gt;
==Routes.rb==&lt;br /&gt;
The '''routes.rb''' file is located in the '''config folder'''. routes.rb, as the name suggests is used to route to the corresponding controller's method. Suppose we have defined a new method in the Category controller. Now, if we want to route a link to the Category controller's new method from some recipe view, we have to include this in the routes.rb file. If we don't, we get an error saying 'No route matches ...'. Whenever we decide to add a new method to a controller, we have to add something similar to :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
match 'Controller_name/new_method_name' =&amp;gt; 'Controller_name#new_method_name'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Our application starts are localhost:3000\categories. Suppose we want to have the root localhost:3000 to the index method in the category controller, we can do:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to =&amp;gt; 'categories#index'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This has to be done after deleting the index.html file from the public directory.&lt;br /&gt;
&lt;br /&gt;
==Views &amp;amp; Layouts==&lt;br /&gt;
Now that the major portion of the application has been done, let us talk about Views and Layouts. &lt;br /&gt;
First, let us add the following code to application.html.erb in the app\views\layouts folder.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to &amp;quot;Show all recipes&amp;quot;, recipes_url %&amp;gt; &amp;lt;%= link_to &amp;quot;Show all categories&amp;quot;, categories_url %&amp;gt; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, when we start the rails server, we see the following page :&lt;br /&gt;
&lt;br /&gt;
[[File:6.jpg]]&lt;br /&gt;
&lt;br /&gt;
Since we want the user to choose a category for each recipe, we include the following code in our view (in the recipes\_form.html.erb in the views section of our app) :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= f.label :category %&amp;gt;&lt;br /&gt;
&amp;lt;%= select(&amp;quot;recipe&amp;quot;, &amp;quot;category_id&amp;quot;, Category.find(:all).collect{ |c| [ c.name, c.id] }) %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view now would have a drop box to choose which category and the user can write the recipe for that category. The new recipe page would now look like :&lt;br /&gt;
&lt;br /&gt;
[[File:7.jpg]]&lt;br /&gt;
&lt;br /&gt;
The CookBookApp now can have many categories, and recipes for each category. Note that we just used a few commands : scaffold, db:migrate, etc. to create this application. Since rails uses the Convention over Configuration principle, it makes it easier to write applications.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
[1] Ruby on Rails Guides: Getting Started With Rails :http://guides.rubyonrails.org/getting_started.html&lt;br /&gt;
&lt;br /&gt;
[2] CookBookApp : http://courses.ncsu.edu/csc517/common/exercises/cookbook3/&lt;br /&gt;
&lt;br /&gt;
[3] CSC 517 Fall 2011 : Engineering Online Video : http://mediasite.online.ncsu.edu/online/Viewer/?peid=5a5b4de717dc4108a1b04133575b22181d&lt;br /&gt;
&lt;br /&gt;
[4] Course Schedule, and Lecture #9 Materials : http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures&lt;br /&gt;
&lt;br /&gt;
[5] Ruby on Rails official site : http://rubyonrails.org/&lt;br /&gt;
&lt;br /&gt;
[6] Sam Ruby, Agile Web Development with Rails, ISBN:1934356549&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=54192</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4d mt</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=54192"/>
		<updated>2011-10-27T17:47:44Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: /* Relationships */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''' The CookBook Application (Lecture 9)'''&lt;br /&gt;
=Introduction=&lt;br /&gt;
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.Rails includes a simple light weight application server [http://en.wikipedia.org/wiki/WEBrick Webrick] and a build system [http://en.wikipedia.org/wiki/Rake_(software) Rake] which makes development on rails easy.&lt;br /&gt;
&lt;br /&gt;
==Ruby on Rails==&lt;br /&gt;
Ruby on Rails, is an [http://en.wikipedia.org/wiki/Web_application_framework open source web application framework] for the [http://en.wikipedia.org/wiki/Ruby_(programming_language) 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:&lt;br /&gt;
===Convention over Configuration===&lt;br /&gt;
[http://en.wikipedia.org/wiki/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.&lt;br /&gt;
&lt;br /&gt;
===Don't repeat yourself=== &lt;br /&gt;
[http://en.wikipedia.org/wiki/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. Also in ActiveRecord there is no need to specify attribute names since it is picked up from the database tables itself.&lt;br /&gt;
&lt;br /&gt;
==Model/View/Controller Framework==&lt;br /&gt;
All Rails applications are implemented using the [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller 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.&lt;br /&gt;
Key components of MVC architecture include:&lt;br /&gt;
# 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.&lt;br /&gt;
#Views: Views provide a '''human interface(GUI)''' for the model. Views in Rails are implemented using embedded ruby (.html.erb) files&lt;br /&gt;
#Controllers: Controllers process the '''user actions''' and help redirect to the appropriate model ,build a user response and update the View. &lt;br /&gt;
The typical user interaction on a Rails application happens as follows:&lt;br /&gt;
#The user responds to the UI (ex : clicks a button)&lt;br /&gt;
#The router routes the action to the appropriate controller and action in it&lt;br /&gt;
#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&lt;br /&gt;
#A view queries the model and uses its data to build a response .&lt;br /&gt;
#The view updates the UI with the view and waits for further user interaction.&lt;br /&gt;
&lt;br /&gt;
==CRUD==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Create,_read,_update_and_delete 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:&lt;br /&gt;
#Create or add new entries&lt;br /&gt;
#Read, retrieve, search, or view existing entries&lt;br /&gt;
#Update or edit existing entries&lt;br /&gt;
#Delete existing entries&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
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 &amp;quot;contact management&amp;quot; or &amp;quot;contact maintenance&amp;quot; (or &amp;quot;document management&amp;quot; in general, depending on the basic storage unit for the particular application).&lt;br /&gt;
&lt;br /&gt;
==Routing in Rails==&lt;br /&gt;
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: &lt;br /&gt;
resources :photos &lt;br /&gt;
can allow us to specify 7 routes for the application.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! HTTP Verb&lt;br /&gt;
! Path &lt;br /&gt;
! action &lt;br /&gt;
! used for&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos || index || display a list of all photos&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos/new || new  || return an HTML form for creating a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  POST   ||	/photos	||create||	create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	||/photos/new ||	new ||	return an HTML form for creating a new photo &lt;br /&gt;
|-&lt;br /&gt;
|  POST	|| /photos	|| create	|| create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id	|| show	|| display a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id/edit	|| edit	|| return an HTML form for editing a photo&lt;br /&gt;
|-&lt;br /&gt;
|  PUT	|| /photos/:id ||	update	|| update a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  DELETE	|| /photos/:id	|| destroy	|| delete a specific photo&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
resources :photos&lt;br /&gt;
creates seven different routes in your application, all mapping to the Photos controller:&lt;br /&gt;
&lt;br /&gt;
==Object relational Mapping==&lt;br /&gt;
Rails handles Object Relational mapping using Active Records. ActiveRecords allow a Rails program to deal with objects and itself handles the mapping of these objects to relational databases. In Active Record, &lt;br /&gt;
# Database tables correspond to Rails classes&lt;br /&gt;
# Database records (rows) correspond to Rails objects.&lt;br /&gt;
ActiveRecord also allow us to query the database in the object domain by using the Active Record Query Interface&lt;br /&gt;
&lt;br /&gt;
=Developing the Cookbook App=&lt;br /&gt;
We now will look into how to develop the cookbook application, and talk about how to get started with the same. &lt;br /&gt;
To get started, we first create a new rails project called say 'CookBookApp', which is done by simply running the command : &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails new CookBookApp&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Once this is run, a series of folders are created, like app, config, db, etc. In developing the cookbook app, we will mainly look into the app, config and db folders mainly.&lt;br /&gt;
==Scaffolding==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Scaffold_%28programming%29 Scaffolding] is a very powerful command that creates code to create, read, update and delete (CRUD) data from the database. In the Cookbook application, the first scaffold is to create categories. Each Category will then have multiple recipes, and the main functionality of our cookbook app would be done.&lt;br /&gt;
The scaffold command to create categories is done by simply giving the command :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails generate scaffold category name:string&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
As seen, this generates the model called category, a bunch of views, and a category controller.&lt;br /&gt;
It also generates a migration, to create a new table called categories (notice that rails called the table as categories, which is the plural of category automatically).&lt;br /&gt;
===DB Migration===&lt;br /&gt;
The scaffold command generates a lot of files, and also a migration in db\migrate with a name like &amp;lt;timestamp&amp;gt;_create_categories.rb. This migration contains the details of the table that has to be created called 'categories', so the next step is to run the migrations. Before we do this, however, if we start the rails server, we get an error mentioning that the table 'categories' doesn't exist. &lt;br /&gt;
Let us now run the migration using the command :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After the migration is run, we know that the categories table has been created, and now run the server.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails server&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
===Creating Categories===&lt;br /&gt;
Now, if we goto the default localhost:3000 page, we see one option : Create a new category, as shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:2.jpg]]&lt;br /&gt;
&lt;br /&gt;
We can click on it, and make a new category, lets say 'Main Course'&lt;br /&gt;
&lt;br /&gt;
[[File:3.jpg]]&lt;br /&gt;
&lt;br /&gt;
When clicking on a new category notice that the URL is '''localhost:3000/categories/new'''. So we are executing the new method of the categories controller. On clicking 'Create Category', a new category with the name 'Main Course' is created, and a flash notice saying 'Successfully Create' is shown. We can now see the category in our list of categories.&lt;br /&gt;
&lt;br /&gt;
[[File:5.jpg]]&lt;br /&gt;
&lt;br /&gt;
So, as we saw, with just a simple scaffold command and db:migrate, we have an application with the basic CRUD functionality.&lt;br /&gt;
&lt;br /&gt;
===Creating Recipes===&lt;br /&gt;
Now, similar to creating categories, we create recipes using the scaffold command. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails generate scaffold recipe title:string description:string instructions:text&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Once this is done, we again have to run a '''db:migrate''', so that the recipes table is in the database.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, the recipes have been created, and as seen in the recipe model, each recipe belongs to a category.&lt;br /&gt;
&lt;br /&gt;
==Relationships==&lt;br /&gt;
Relationships are always defined in the model. We first want to say that each recipe belongs to a category. This can be done in the recipe controller as shown below :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similarly, we want to add a detail mentioning that each Category can have many Recipes : a one to many relationship. To do this, we need to edit the Category model, and include the '''has_many''' relationship with Recipe. The Category model now looks like :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
  has_many :recipes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We also have to add a reference in the migration, to indicate that the recipes table references the categories table. This can be done in the '''&amp;lt;timestamp&amp;gt;_create_recipes.rb''' migration, and now it would look like :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateRecipes &amp;lt; ActiveRecord::Migration&lt;br /&gt;
  def change&lt;br /&gt;
    create_table :recipes do |t|&lt;br /&gt;
      t.string :title&lt;br /&gt;
      t.string :description&lt;br /&gt;
      t.text :instructions&lt;br /&gt;
      t.references :category&lt;br /&gt;
      t.timestamps&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As before, since we have made a change to the migration, we now need to do a '''db:migrate'''.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Valdations==&lt;br /&gt;
Validations, just like Relationships, are also defined in the model. Suppose we want to make sure that each recipe entered has a valid title and instructions, and the condition being that they cannot be empty. We can add this to the Recipe model, which now looks like :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :category&lt;br /&gt;
  validates :title, :presence =&amp;gt; true&lt;br /&gt;
  validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
  validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Thus, the title, instructions and category are all validated, and it makes sure that title and instructions are not empty, and that the category it belongs also exists.&lt;br /&gt;
&lt;br /&gt;
==Routes.rb==&lt;br /&gt;
The '''routes.rb''' file is located in the '''config folder'''. routes.rb, as the name suggests is used to route to the corresponding controller's method. Suppose we have defined a new method in the Category controller. Now, if we want to route a link to the Category controller's new method from some recipe view, we have to include this in the routes.rb file. If we don't, we get an error saying 'No route matches ...'. Whenever we decide to add a new method to a controller, we have to add something similar to :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
match 'Controller_name/new_method_name' =&amp;gt; 'Controller_name#new_method_name'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Our application starts are localhost:3000\categories. Suppose we want to have the root localhost:3000 to the index method in the category controller, we can do:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to =&amp;gt; 'categories#index'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This has to be done after deleting the index.html file from the public directory.&lt;br /&gt;
&lt;br /&gt;
==Views &amp;amp; Layouts==&lt;br /&gt;
Now that the major portion of the application has been done, let us talk about Views and Layouts. &lt;br /&gt;
First, let us add the following code to application.html.erb in the app\views\layouts folder.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to &amp;quot;Show all recipes&amp;quot;, recipes_url %&amp;gt; &amp;lt;%= link_to &amp;quot;Show all categories&amp;quot;, categories_url %&amp;gt; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, when we start the rails server, we see the following page :&lt;br /&gt;
&lt;br /&gt;
[[File:6.jpg]]&lt;br /&gt;
&lt;br /&gt;
Since we want the user to choose a category for each recipe, we include the following code in our view (in the recipes\_form.html.erb in the views section of our app) :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= f.label :category %&amp;gt;&lt;br /&gt;
&amp;lt;%= select(&amp;quot;recipe&amp;quot;, &amp;quot;category_id&amp;quot;, Category.find(:all).collect{ |c| [ c.name, c.id] }) %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view now would have a drop box to choose which category and the user can write the recipe for that category. The new recipe page would now look like :&lt;br /&gt;
&lt;br /&gt;
[[File:7.jpg]]&lt;br /&gt;
&lt;br /&gt;
The CookBookApp now can have many categories, and recipes for each category. Note that we just used a few commands : scaffold, db:migrate, etc. to create this application. Since rails uses the Convention over Configuration principle, it makes it easier to write applications.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
[1] Ruby on Rails Guides: Getting Started With Rails :http://guides.rubyonrails.org/getting_started.html&lt;br /&gt;
&lt;br /&gt;
[2] CookBookApp : http://courses.ncsu.edu/csc517/common/exercises/cookbook3/&lt;br /&gt;
&lt;br /&gt;
[3] CSC 517 Fall 2011 : Engineering Online Video : http://mediasite.online.ncsu.edu/online/Viewer/?peid=5a5b4de717dc4108a1b04133575b22181d&lt;br /&gt;
&lt;br /&gt;
[4] Course Schedule, and Lecture #9 Materials : http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures&lt;br /&gt;
&lt;br /&gt;
[5] Ruby on Rails official site : http://rubyonrails.org/&lt;br /&gt;
&lt;br /&gt;
[6] Sam Ruby, Agile Web Development with Rails, ISBN:1934356549&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=54191</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4d mt</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=54191"/>
		<updated>2011-10-27T17:47:02Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: /* Routes.rb */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''' The CookBook Application (Lecture 9)'''&lt;br /&gt;
=Introduction=&lt;br /&gt;
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.Rails includes a simple light weight application server [http://en.wikipedia.org/wiki/WEBrick Webrick] and a build system [http://en.wikipedia.org/wiki/Rake_(software) Rake] which makes development on rails easy.&lt;br /&gt;
&lt;br /&gt;
==Ruby on Rails==&lt;br /&gt;
Ruby on Rails, is an [http://en.wikipedia.org/wiki/Web_application_framework open source web application framework] for the [http://en.wikipedia.org/wiki/Ruby_(programming_language) 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:&lt;br /&gt;
===Convention over Configuration===&lt;br /&gt;
[http://en.wikipedia.org/wiki/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.&lt;br /&gt;
&lt;br /&gt;
===Don't repeat yourself=== &lt;br /&gt;
[http://en.wikipedia.org/wiki/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. Also in ActiveRecord there is no need to specify attribute names since it is picked up from the database tables itself.&lt;br /&gt;
&lt;br /&gt;
==Model/View/Controller Framework==&lt;br /&gt;
All Rails applications are implemented using the [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller 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.&lt;br /&gt;
Key components of MVC architecture include:&lt;br /&gt;
# 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.&lt;br /&gt;
#Views: Views provide a '''human interface(GUI)''' for the model. Views in Rails are implemented using embedded ruby (.html.erb) files&lt;br /&gt;
#Controllers: Controllers process the '''user actions''' and help redirect to the appropriate model ,build a user response and update the View. &lt;br /&gt;
The typical user interaction on a Rails application happens as follows:&lt;br /&gt;
#The user responds to the UI (ex : clicks a button)&lt;br /&gt;
#The router routes the action to the appropriate controller and action in it&lt;br /&gt;
#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&lt;br /&gt;
#A view queries the model and uses its data to build a response .&lt;br /&gt;
#The view updates the UI with the view and waits for further user interaction.&lt;br /&gt;
&lt;br /&gt;
==CRUD==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Create,_read,_update_and_delete 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:&lt;br /&gt;
#Create or add new entries&lt;br /&gt;
#Read, retrieve, search, or view existing entries&lt;br /&gt;
#Update or edit existing entries&lt;br /&gt;
#Delete existing entries&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
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 &amp;quot;contact management&amp;quot; or &amp;quot;contact maintenance&amp;quot; (or &amp;quot;document management&amp;quot; in general, depending on the basic storage unit for the particular application).&lt;br /&gt;
&lt;br /&gt;
==Routing in Rails==&lt;br /&gt;
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: &lt;br /&gt;
resources :photos &lt;br /&gt;
can allow us to specify 7 routes for the application.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! HTTP Verb&lt;br /&gt;
! Path &lt;br /&gt;
! action &lt;br /&gt;
! used for&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos || index || display a list of all photos&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos/new || new  || return an HTML form for creating a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  POST   ||	/photos	||create||	create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	||/photos/new ||	new ||	return an HTML form for creating a new photo &lt;br /&gt;
|-&lt;br /&gt;
|  POST	|| /photos	|| create	|| create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id	|| show	|| display a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id/edit	|| edit	|| return an HTML form for editing a photo&lt;br /&gt;
|-&lt;br /&gt;
|  PUT	|| /photos/:id ||	update	|| update a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  DELETE	|| /photos/:id	|| destroy	|| delete a specific photo&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
resources :photos&lt;br /&gt;
creates seven different routes in your application, all mapping to the Photos controller:&lt;br /&gt;
&lt;br /&gt;
==Object relational Mapping==&lt;br /&gt;
Rails handles Object Relational mapping using Active Records. ActiveRecords allow a Rails program to deal with objects and itself handles the mapping of these objects to relational databases. In Active Record, &lt;br /&gt;
# Database tables correspond to Rails classes&lt;br /&gt;
# Database records (rows) correspond to Rails objects.&lt;br /&gt;
ActiveRecord also allow us to query the database in the object domain by using the Active Record Query Interface&lt;br /&gt;
&lt;br /&gt;
=Developing the Cookbook App=&lt;br /&gt;
We now will look into how to develop the cookbook application, and talk about how to get started with the same. &lt;br /&gt;
To get started, we first create a new rails project called say 'CookBookApp', which is done by simply running the command : &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails new CookBookApp&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Once this is run, a series of folders are created, like app, config, db, etc. In developing the cookbook app, we will mainly look into the app, config and db folders mainly.&lt;br /&gt;
==Scaffolding==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Scaffold_%28programming%29 Scaffolding] is a very powerful command that creates code to create, read, update and delete (CRUD) data from the database. In the Cookbook application, the first scaffold is to create categories. Each Category will then have multiple recipes, and the main functionality of our cookbook app would be done.&lt;br /&gt;
The scaffold command to create categories is done by simply giving the command :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails generate scaffold category name:string&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
As seen, this generates the model called category, a bunch of views, and a category controller.&lt;br /&gt;
It also generates a migration, to create a new table called categories (notice that rails called the table as categories, which is the plural of category automatically).&lt;br /&gt;
===DB Migration===&lt;br /&gt;
The scaffold command generates a lot of files, and also a migration in db\migrate with a name like &amp;lt;timestamp&amp;gt;_create_categories.rb. This migration contains the details of the table that has to be created called 'categories', so the next step is to run the migrations. Before we do this, however, if we start the rails server, we get an error mentioning that the table 'categories' doesn't exist. &lt;br /&gt;
Let us now run the migration using the command :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After the migration is run, we know that the categories table has been created, and now run the server.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails server&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
===Creating Categories===&lt;br /&gt;
Now, if we goto the default localhost:3000 page, we see one option : Create a new category, as shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:2.jpg]]&lt;br /&gt;
&lt;br /&gt;
We can click on it, and make a new category, lets say 'Main Course'&lt;br /&gt;
&lt;br /&gt;
[[File:3.jpg]]&lt;br /&gt;
&lt;br /&gt;
When clicking on a new category notice that the URL is '''localhost:3000/categories/new'''. So we are executing the new method of the categories controller. On clicking 'Create Category', a new category with the name 'Main Course' is created, and a flash notice saying 'Successfully Create' is shown. We can now see the category in our list of categories.&lt;br /&gt;
&lt;br /&gt;
[[File:5.jpg]]&lt;br /&gt;
&lt;br /&gt;
So, as we saw, with just a simple scaffold command and db:migrate, we have an application with the basic CRUD functionality.&lt;br /&gt;
&lt;br /&gt;
===Creating Recipes===&lt;br /&gt;
Now, similar to creating categories, we create recipes using the scaffold command. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails generate scaffold recipe title:string description:string instructions:text&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Once this is done, we again have to run a '''db:migrate''', so that the recipes table is in the database.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, the recipes have been created, and as seen in the recipe model, each recipe belongs to a category.&lt;br /&gt;
&lt;br /&gt;
==Relationships==&lt;br /&gt;
Relationships are always defined in the model. We first want to say that each recipe belongs to a category. This can be done in the recipe controller as shown below :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similarly, we want to add a detail mentioning that each Category can have many Recipes : a one to many relationship. To do this, we need to edit the Category model, and include the '''has_many''' relationship with Recipe. The Category model now looks like :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
  has_many :recipes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We also have to add a reference in the migration, to indicate that the recipes table references the categories table. This can be done in the &amp;lt;timestamp&amp;gt;_create_recipes.rb migration, and now it would look like :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateRecipes &amp;lt; ActiveRecord::Migration&lt;br /&gt;
  def change&lt;br /&gt;
    create_table :recipes do |t|&lt;br /&gt;
      t.string :title&lt;br /&gt;
      t.string :description&lt;br /&gt;
      t.text :instructions&lt;br /&gt;
      t.references :category&lt;br /&gt;
      t.timestamps&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As before, since we have made a change to the migration, we now need to do a '''db:migrate'''.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Valdations==&lt;br /&gt;
Validations, just like Relationships, are also defined in the model. Suppose we want to make sure that each recipe entered has a valid title and instructions, and the condition being that they cannot be empty. We can add this to the Recipe model, which now looks like :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :category&lt;br /&gt;
  validates :title, :presence =&amp;gt; true&lt;br /&gt;
  validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
  validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Thus, the title, instructions and category are all validated, and it makes sure that title and instructions are not empty, and that the category it belongs also exists.&lt;br /&gt;
&lt;br /&gt;
==Routes.rb==&lt;br /&gt;
The '''routes.rb''' file is located in the '''config folder'''. routes.rb, as the name suggests is used to route to the corresponding controller's method. Suppose we have defined a new method in the Category controller. Now, if we want to route a link to the Category controller's new method from some recipe view, we have to include this in the routes.rb file. If we don't, we get an error saying 'No route matches ...'. Whenever we decide to add a new method to a controller, we have to add something similar to :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
match 'Controller_name/new_method_name' =&amp;gt; 'Controller_name#new_method_name'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Our application starts are localhost:3000\categories. Suppose we want to have the root localhost:3000 to the index method in the category controller, we can do:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to =&amp;gt; 'categories#index'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This has to be done after deleting the index.html file from the public directory.&lt;br /&gt;
&lt;br /&gt;
==Views &amp;amp; Layouts==&lt;br /&gt;
Now that the major portion of the application has been done, let us talk about Views and Layouts. &lt;br /&gt;
First, let us add the following code to application.html.erb in the app\views\layouts folder.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to &amp;quot;Show all recipes&amp;quot;, recipes_url %&amp;gt; &amp;lt;%= link_to &amp;quot;Show all categories&amp;quot;, categories_url %&amp;gt; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, when we start the rails server, we see the following page :&lt;br /&gt;
&lt;br /&gt;
[[File:6.jpg]]&lt;br /&gt;
&lt;br /&gt;
Since we want the user to choose a category for each recipe, we include the following code in our view (in the recipes\_form.html.erb in the views section of our app) :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= f.label :category %&amp;gt;&lt;br /&gt;
&amp;lt;%= select(&amp;quot;recipe&amp;quot;, &amp;quot;category_id&amp;quot;, Category.find(:all).collect{ |c| [ c.name, c.id] }) %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view now would have a drop box to choose which category and the user can write the recipe for that category. The new recipe page would now look like :&lt;br /&gt;
&lt;br /&gt;
[[File:7.jpg]]&lt;br /&gt;
&lt;br /&gt;
The CookBookApp now can have many categories, and recipes for each category. Note that we just used a few commands : scaffold, db:migrate, etc. to create this application. Since rails uses the Convention over Configuration principle, it makes it easier to write applications.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
[1] Ruby on Rails Guides: Getting Started With Rails :http://guides.rubyonrails.org/getting_started.html&lt;br /&gt;
&lt;br /&gt;
[2] CookBookApp : http://courses.ncsu.edu/csc517/common/exercises/cookbook3/&lt;br /&gt;
&lt;br /&gt;
[3] CSC 517 Fall 2011 : Engineering Online Video : http://mediasite.online.ncsu.edu/online/Viewer/?peid=5a5b4de717dc4108a1b04133575b22181d&lt;br /&gt;
&lt;br /&gt;
[4] Course Schedule, and Lecture #9 Materials : http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures&lt;br /&gt;
&lt;br /&gt;
[5] Ruby on Rails official site : http://rubyonrails.org/&lt;br /&gt;
&lt;br /&gt;
[6] Sam Ruby, Agile Web Development with Rails, ISBN:1934356549&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=54190</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4d mt</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=54190"/>
		<updated>2011-10-27T17:46:19Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: /* Relationships */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''' The CookBook Application (Lecture 9)'''&lt;br /&gt;
=Introduction=&lt;br /&gt;
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.Rails includes a simple light weight application server [http://en.wikipedia.org/wiki/WEBrick Webrick] and a build system [http://en.wikipedia.org/wiki/Rake_(software) Rake] which makes development on rails easy.&lt;br /&gt;
&lt;br /&gt;
==Ruby on Rails==&lt;br /&gt;
Ruby on Rails, is an [http://en.wikipedia.org/wiki/Web_application_framework open source web application framework] for the [http://en.wikipedia.org/wiki/Ruby_(programming_language) 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:&lt;br /&gt;
===Convention over Configuration===&lt;br /&gt;
[http://en.wikipedia.org/wiki/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.&lt;br /&gt;
&lt;br /&gt;
===Don't repeat yourself=== &lt;br /&gt;
[http://en.wikipedia.org/wiki/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. Also in ActiveRecord there is no need to specify attribute names since it is picked up from the database tables itself.&lt;br /&gt;
&lt;br /&gt;
==Model/View/Controller Framework==&lt;br /&gt;
All Rails applications are implemented using the [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller 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.&lt;br /&gt;
Key components of MVC architecture include:&lt;br /&gt;
# 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.&lt;br /&gt;
#Views: Views provide a '''human interface(GUI)''' for the model. Views in Rails are implemented using embedded ruby (.html.erb) files&lt;br /&gt;
#Controllers: Controllers process the '''user actions''' and help redirect to the appropriate model ,build a user response and update the View. &lt;br /&gt;
The typical user interaction on a Rails application happens as follows:&lt;br /&gt;
#The user responds to the UI (ex : clicks a button)&lt;br /&gt;
#The router routes the action to the appropriate controller and action in it&lt;br /&gt;
#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&lt;br /&gt;
#A view queries the model and uses its data to build a response .&lt;br /&gt;
#The view updates the UI with the view and waits for further user interaction.&lt;br /&gt;
&lt;br /&gt;
==CRUD==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Create,_read,_update_and_delete 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:&lt;br /&gt;
#Create or add new entries&lt;br /&gt;
#Read, retrieve, search, or view existing entries&lt;br /&gt;
#Update or edit existing entries&lt;br /&gt;
#Delete existing entries&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
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 &amp;quot;contact management&amp;quot; or &amp;quot;contact maintenance&amp;quot; (or &amp;quot;document management&amp;quot; in general, depending on the basic storage unit for the particular application).&lt;br /&gt;
&lt;br /&gt;
==Routing in Rails==&lt;br /&gt;
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: &lt;br /&gt;
resources :photos &lt;br /&gt;
can allow us to specify 7 routes for the application.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! HTTP Verb&lt;br /&gt;
! Path &lt;br /&gt;
! action &lt;br /&gt;
! used for&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos || index || display a list of all photos&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos/new || new  || return an HTML form for creating a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  POST   ||	/photos	||create||	create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	||/photos/new ||	new ||	return an HTML form for creating a new photo &lt;br /&gt;
|-&lt;br /&gt;
|  POST	|| /photos	|| create	|| create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id	|| show	|| display a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id/edit	|| edit	|| return an HTML form for editing a photo&lt;br /&gt;
|-&lt;br /&gt;
|  PUT	|| /photos/:id ||	update	|| update a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  DELETE	|| /photos/:id	|| destroy	|| delete a specific photo&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
resources :photos&lt;br /&gt;
creates seven different routes in your application, all mapping to the Photos controller:&lt;br /&gt;
&lt;br /&gt;
==Object relational Mapping==&lt;br /&gt;
Rails handles Object Relational mapping using Active Records. ActiveRecords allow a Rails program to deal with objects and itself handles the mapping of these objects to relational databases. In Active Record, &lt;br /&gt;
# Database tables correspond to Rails classes&lt;br /&gt;
# Database records (rows) correspond to Rails objects.&lt;br /&gt;
ActiveRecord also allow us to query the database in the object domain by using the Active Record Query Interface&lt;br /&gt;
&lt;br /&gt;
=Developing the Cookbook App=&lt;br /&gt;
We now will look into how to develop the cookbook application, and talk about how to get started with the same. &lt;br /&gt;
To get started, we first create a new rails project called say 'CookBookApp', which is done by simply running the command : &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails new CookBookApp&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Once this is run, a series of folders are created, like app, config, db, etc. In developing the cookbook app, we will mainly look into the app, config and db folders mainly.&lt;br /&gt;
==Scaffolding==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Scaffold_%28programming%29 Scaffolding] is a very powerful command that creates code to create, read, update and delete (CRUD) data from the database. In the Cookbook application, the first scaffold is to create categories. Each Category will then have multiple recipes, and the main functionality of our cookbook app would be done.&lt;br /&gt;
The scaffold command to create categories is done by simply giving the command :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails generate scaffold category name:string&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
As seen, this generates the model called category, a bunch of views, and a category controller.&lt;br /&gt;
It also generates a migration, to create a new table called categories (notice that rails called the table as categories, which is the plural of category automatically).&lt;br /&gt;
===DB Migration===&lt;br /&gt;
The scaffold command generates a lot of files, and also a migration in db\migrate with a name like &amp;lt;timestamp&amp;gt;_create_categories.rb. This migration contains the details of the table that has to be created called 'categories', so the next step is to run the migrations. Before we do this, however, if we start the rails server, we get an error mentioning that the table 'categories' doesn't exist. &lt;br /&gt;
Let us now run the migration using the command :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After the migration is run, we know that the categories table has been created, and now run the server.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails server&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
===Creating Categories===&lt;br /&gt;
Now, if we goto the default localhost:3000 page, we see one option : Create a new category, as shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:2.jpg]]&lt;br /&gt;
&lt;br /&gt;
We can click on it, and make a new category, lets say 'Main Course'&lt;br /&gt;
&lt;br /&gt;
[[File:3.jpg]]&lt;br /&gt;
&lt;br /&gt;
When clicking on a new category notice that the URL is '''localhost:3000/categories/new'''. So we are executing the new method of the categories controller. On clicking 'Create Category', a new category with the name 'Main Course' is created, and a flash notice saying 'Successfully Create' is shown. We can now see the category in our list of categories.&lt;br /&gt;
&lt;br /&gt;
[[File:5.jpg]]&lt;br /&gt;
&lt;br /&gt;
So, as we saw, with just a simple scaffold command and db:migrate, we have an application with the basic CRUD functionality.&lt;br /&gt;
&lt;br /&gt;
===Creating Recipes===&lt;br /&gt;
Now, similar to creating categories, we create recipes using the scaffold command. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails generate scaffold recipe title:string description:string instructions:text&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Once this is done, we again have to run a '''db:migrate''', so that the recipes table is in the database.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, the recipes have been created, and as seen in the recipe model, each recipe belongs to a category.&lt;br /&gt;
&lt;br /&gt;
==Relationships==&lt;br /&gt;
Relationships are always defined in the model. We first want to say that each recipe belongs to a category. This can be done in the recipe controller as shown below :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similarly, we want to add a detail mentioning that each Category can have many Recipes : a one to many relationship. To do this, we need to edit the Category model, and include the '''has_many''' relationship with Recipe. The Category model now looks like :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
  has_many :recipes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We also have to add a reference in the migration, to indicate that the recipes table references the categories table. This can be done in the &amp;lt;timestamp&amp;gt;_create_recipes.rb migration, and now it would look like :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateRecipes &amp;lt; ActiveRecord::Migration&lt;br /&gt;
  def change&lt;br /&gt;
    create_table :recipes do |t|&lt;br /&gt;
      t.string :title&lt;br /&gt;
      t.string :description&lt;br /&gt;
      t.text :instructions&lt;br /&gt;
      t.references :category&lt;br /&gt;
      t.timestamps&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As before, since we have made a change to the migration, we now need to do a '''db:migrate'''.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Valdations==&lt;br /&gt;
Validations, just like Relationships, are also defined in the model. Suppose we want to make sure that each recipe entered has a valid title and instructions, and the condition being that they cannot be empty. We can add this to the Recipe model, which now looks like :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :category&lt;br /&gt;
  validates :title, :presence =&amp;gt; true&lt;br /&gt;
  validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
  validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Thus, the title, instructions and category are all validated, and it makes sure that title and instructions are not empty, and that the category it belongs also exists.&lt;br /&gt;
&lt;br /&gt;
==Routes.rb==&lt;br /&gt;
The routes.rb file is located in the config folder. routes.rb, as the name suggests is used to route to the corresponding controller's method. Suppose we have defined a new method in the Category controller. Now, if we want to route a link to the Category controller's new method from some recipe view, we have to include this in the routes.rb file. If we don't, we get an error saying 'No route matches ...'. Whenever we decide to add a new method to a controller, we have to add something similar to :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
match 'Controller_name/new_method_name' =&amp;gt; 'Controller_name#new_method_name'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Our application starts are localhost:3000\categories. Suppose we want to have the root localhost:3000 to the index method in the category controller, we can do:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to =&amp;gt; 'categories#index'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This has to be done after deleting the index.html file from the public directory.&lt;br /&gt;
&lt;br /&gt;
==Views &amp;amp; Layouts==&lt;br /&gt;
Now that the major portion of the application has been done, let us talk about Views and Layouts. &lt;br /&gt;
First, let us add the following code to application.html.erb in the app\views\layouts folder.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to &amp;quot;Show all recipes&amp;quot;, recipes_url %&amp;gt; &amp;lt;%= link_to &amp;quot;Show all categories&amp;quot;, categories_url %&amp;gt; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, when we start the rails server, we see the following page :&lt;br /&gt;
&lt;br /&gt;
[[File:6.jpg]]&lt;br /&gt;
&lt;br /&gt;
Since we want the user to choose a category for each recipe, we include the following code in our view (in the recipes\_form.html.erb in the views section of our app) :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= f.label :category %&amp;gt;&lt;br /&gt;
&amp;lt;%= select(&amp;quot;recipe&amp;quot;, &amp;quot;category_id&amp;quot;, Category.find(:all).collect{ |c| [ c.name, c.id] }) %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view now would have a drop box to choose which category and the user can write the recipe for that category. The new recipe page would now look like :&lt;br /&gt;
&lt;br /&gt;
[[File:7.jpg]]&lt;br /&gt;
&lt;br /&gt;
The CookBookApp now can have many categories, and recipes for each category. Note that we just used a few commands : scaffold, db:migrate, etc. to create this application. Since rails uses the Convention over Configuration principle, it makes it easier to write applications.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
[1] Ruby on Rails Guides: Getting Started With Rails :http://guides.rubyonrails.org/getting_started.html&lt;br /&gt;
&lt;br /&gt;
[2] CookBookApp : http://courses.ncsu.edu/csc517/common/exercises/cookbook3/&lt;br /&gt;
&lt;br /&gt;
[3] CSC 517 Fall 2011 : Engineering Online Video : http://mediasite.online.ncsu.edu/online/Viewer/?peid=5a5b4de717dc4108a1b04133575b22181d&lt;br /&gt;
&lt;br /&gt;
[4] Course Schedule, and Lecture #9 Materials : http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures&lt;br /&gt;
&lt;br /&gt;
[5] Ruby on Rails official site : http://rubyonrails.org/&lt;br /&gt;
&lt;br /&gt;
[6] Sam Ruby, Agile Web Development with Rails, ISBN:1934356549&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=54189</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4d mt</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=54189"/>
		<updated>2011-10-27T17:45:37Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: /* Creating Recipes */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''' The CookBook Application (Lecture 9)'''&lt;br /&gt;
=Introduction=&lt;br /&gt;
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.Rails includes a simple light weight application server [http://en.wikipedia.org/wiki/WEBrick Webrick] and a build system [http://en.wikipedia.org/wiki/Rake_(software) Rake] which makes development on rails easy.&lt;br /&gt;
&lt;br /&gt;
==Ruby on Rails==&lt;br /&gt;
Ruby on Rails, is an [http://en.wikipedia.org/wiki/Web_application_framework open source web application framework] for the [http://en.wikipedia.org/wiki/Ruby_(programming_language) 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:&lt;br /&gt;
===Convention over Configuration===&lt;br /&gt;
[http://en.wikipedia.org/wiki/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.&lt;br /&gt;
&lt;br /&gt;
===Don't repeat yourself=== &lt;br /&gt;
[http://en.wikipedia.org/wiki/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. Also in ActiveRecord there is no need to specify attribute names since it is picked up from the database tables itself.&lt;br /&gt;
&lt;br /&gt;
==Model/View/Controller Framework==&lt;br /&gt;
All Rails applications are implemented using the [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller 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.&lt;br /&gt;
Key components of MVC architecture include:&lt;br /&gt;
# 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.&lt;br /&gt;
#Views: Views provide a '''human interface(GUI)''' for the model. Views in Rails are implemented using embedded ruby (.html.erb) files&lt;br /&gt;
#Controllers: Controllers process the '''user actions''' and help redirect to the appropriate model ,build a user response and update the View. &lt;br /&gt;
The typical user interaction on a Rails application happens as follows:&lt;br /&gt;
#The user responds to the UI (ex : clicks a button)&lt;br /&gt;
#The router routes the action to the appropriate controller and action in it&lt;br /&gt;
#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&lt;br /&gt;
#A view queries the model and uses its data to build a response .&lt;br /&gt;
#The view updates the UI with the view and waits for further user interaction.&lt;br /&gt;
&lt;br /&gt;
==CRUD==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Create,_read,_update_and_delete 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:&lt;br /&gt;
#Create or add new entries&lt;br /&gt;
#Read, retrieve, search, or view existing entries&lt;br /&gt;
#Update or edit existing entries&lt;br /&gt;
#Delete existing entries&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
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 &amp;quot;contact management&amp;quot; or &amp;quot;contact maintenance&amp;quot; (or &amp;quot;document management&amp;quot; in general, depending on the basic storage unit for the particular application).&lt;br /&gt;
&lt;br /&gt;
==Routing in Rails==&lt;br /&gt;
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: &lt;br /&gt;
resources :photos &lt;br /&gt;
can allow us to specify 7 routes for the application.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! HTTP Verb&lt;br /&gt;
! Path &lt;br /&gt;
! action &lt;br /&gt;
! used for&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos || index || display a list of all photos&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos/new || new  || return an HTML form for creating a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  POST   ||	/photos	||create||	create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	||/photos/new ||	new ||	return an HTML form for creating a new photo &lt;br /&gt;
|-&lt;br /&gt;
|  POST	|| /photos	|| create	|| create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id	|| show	|| display a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id/edit	|| edit	|| return an HTML form for editing a photo&lt;br /&gt;
|-&lt;br /&gt;
|  PUT	|| /photos/:id ||	update	|| update a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  DELETE	|| /photos/:id	|| destroy	|| delete a specific photo&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
resources :photos&lt;br /&gt;
creates seven different routes in your application, all mapping to the Photos controller:&lt;br /&gt;
&lt;br /&gt;
==Object relational Mapping==&lt;br /&gt;
Rails handles Object Relational mapping using Active Records. ActiveRecords allow a Rails program to deal with objects and itself handles the mapping of these objects to relational databases. In Active Record, &lt;br /&gt;
# Database tables correspond to Rails classes&lt;br /&gt;
# Database records (rows) correspond to Rails objects.&lt;br /&gt;
ActiveRecord also allow us to query the database in the object domain by using the Active Record Query Interface&lt;br /&gt;
&lt;br /&gt;
=Developing the Cookbook App=&lt;br /&gt;
We now will look into how to develop the cookbook application, and talk about how to get started with the same. &lt;br /&gt;
To get started, we first create a new rails project called say 'CookBookApp', which is done by simply running the command : &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails new CookBookApp&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Once this is run, a series of folders are created, like app, config, db, etc. In developing the cookbook app, we will mainly look into the app, config and db folders mainly.&lt;br /&gt;
==Scaffolding==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Scaffold_%28programming%29 Scaffolding] is a very powerful command that creates code to create, read, update and delete (CRUD) data from the database. In the Cookbook application, the first scaffold is to create categories. Each Category will then have multiple recipes, and the main functionality of our cookbook app would be done.&lt;br /&gt;
The scaffold command to create categories is done by simply giving the command :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails generate scaffold category name:string&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
As seen, this generates the model called category, a bunch of views, and a category controller.&lt;br /&gt;
It also generates a migration, to create a new table called categories (notice that rails called the table as categories, which is the plural of category automatically).&lt;br /&gt;
===DB Migration===&lt;br /&gt;
The scaffold command generates a lot of files, and also a migration in db\migrate with a name like &amp;lt;timestamp&amp;gt;_create_categories.rb. This migration contains the details of the table that has to be created called 'categories', so the next step is to run the migrations. Before we do this, however, if we start the rails server, we get an error mentioning that the table 'categories' doesn't exist. &lt;br /&gt;
Let us now run the migration using the command :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After the migration is run, we know that the categories table has been created, and now run the server.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails server&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
===Creating Categories===&lt;br /&gt;
Now, if we goto the default localhost:3000 page, we see one option : Create a new category, as shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:2.jpg]]&lt;br /&gt;
&lt;br /&gt;
We can click on it, and make a new category, lets say 'Main Course'&lt;br /&gt;
&lt;br /&gt;
[[File:3.jpg]]&lt;br /&gt;
&lt;br /&gt;
When clicking on a new category notice that the URL is '''localhost:3000/categories/new'''. So we are executing the new method of the categories controller. On clicking 'Create Category', a new category with the name 'Main Course' is created, and a flash notice saying 'Successfully Create' is shown. We can now see the category in our list of categories.&lt;br /&gt;
&lt;br /&gt;
[[File:5.jpg]]&lt;br /&gt;
&lt;br /&gt;
So, as we saw, with just a simple scaffold command and db:migrate, we have an application with the basic CRUD functionality.&lt;br /&gt;
&lt;br /&gt;
===Creating Recipes===&lt;br /&gt;
Now, similar to creating categories, we create recipes using the scaffold command. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails generate scaffold recipe title:string description:string instructions:text&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Once this is done, we again have to run a '''db:migrate''', so that the recipes table is in the database.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, the recipes have been created, and as seen in the recipe model, each recipe belongs to a category.&lt;br /&gt;
&lt;br /&gt;
==Relationships==&lt;br /&gt;
Relationships are always defined in the model. We first want to say that each recipe belongs to a category. This can be done in the recipe controller as shown below :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similarly, we want to add a detail mentioning that each Category can have many Recipes : a one to many relationship. To do this, we need to edit the Category model, and include the 'has_many' relationship with Recipe. The Category model now looks like :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
  has_many :recipes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We also have to add a reference in the migration, to indicate that the recipes table references the categories table. This can be done in the &amp;lt;timestamp&amp;gt;_create_recipes.rb migration, and now it would look like :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateRecipes &amp;lt; ActiveRecord::Migration&lt;br /&gt;
  def change&lt;br /&gt;
    create_table :recipes do |t|&lt;br /&gt;
      t.string :title&lt;br /&gt;
      t.string :description&lt;br /&gt;
      t.text :instructions&lt;br /&gt;
      t.references :category&lt;br /&gt;
      t.timestamps&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As before, since we have made a change to the migration, we now need to do a db:migrate.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Valdations==&lt;br /&gt;
Validations, just like Relationships, are also defined in the model. Suppose we want to make sure that each recipe entered has a valid title and instructions, and the condition being that they cannot be empty. We can add this to the Recipe model, which now looks like :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :category&lt;br /&gt;
  validates :title, :presence =&amp;gt; true&lt;br /&gt;
  validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
  validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Thus, the title, instructions and category are all validated, and it makes sure that title and instructions are not empty, and that the category it belongs also exists.&lt;br /&gt;
&lt;br /&gt;
==Routes.rb==&lt;br /&gt;
The routes.rb file is located in the config folder. routes.rb, as the name suggests is used to route to the corresponding controller's method. Suppose we have defined a new method in the Category controller. Now, if we want to route a link to the Category controller's new method from some recipe view, we have to include this in the routes.rb file. If we don't, we get an error saying 'No route matches ...'. Whenever we decide to add a new method to a controller, we have to add something similar to :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
match 'Controller_name/new_method_name' =&amp;gt; 'Controller_name#new_method_name'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Our application starts are localhost:3000\categories. Suppose we want to have the root localhost:3000 to the index method in the category controller, we can do:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to =&amp;gt; 'categories#index'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This has to be done after deleting the index.html file from the public directory.&lt;br /&gt;
&lt;br /&gt;
==Views &amp;amp; Layouts==&lt;br /&gt;
Now that the major portion of the application has been done, let us talk about Views and Layouts. &lt;br /&gt;
First, let us add the following code to application.html.erb in the app\views\layouts folder.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to &amp;quot;Show all recipes&amp;quot;, recipes_url %&amp;gt; &amp;lt;%= link_to &amp;quot;Show all categories&amp;quot;, categories_url %&amp;gt; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, when we start the rails server, we see the following page :&lt;br /&gt;
&lt;br /&gt;
[[File:6.jpg]]&lt;br /&gt;
&lt;br /&gt;
Since we want the user to choose a category for each recipe, we include the following code in our view (in the recipes\_form.html.erb in the views section of our app) :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= f.label :category %&amp;gt;&lt;br /&gt;
&amp;lt;%= select(&amp;quot;recipe&amp;quot;, &amp;quot;category_id&amp;quot;, Category.find(:all).collect{ |c| [ c.name, c.id] }) %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view now would have a drop box to choose which category and the user can write the recipe for that category. The new recipe page would now look like :&lt;br /&gt;
&lt;br /&gt;
[[File:7.jpg]]&lt;br /&gt;
&lt;br /&gt;
The CookBookApp now can have many categories, and recipes for each category. Note that we just used a few commands : scaffold, db:migrate, etc. to create this application. Since rails uses the Convention over Configuration principle, it makes it easier to write applications.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
[1] Ruby on Rails Guides: Getting Started With Rails :http://guides.rubyonrails.org/getting_started.html&lt;br /&gt;
&lt;br /&gt;
[2] CookBookApp : http://courses.ncsu.edu/csc517/common/exercises/cookbook3/&lt;br /&gt;
&lt;br /&gt;
[3] CSC 517 Fall 2011 : Engineering Online Video : http://mediasite.online.ncsu.edu/online/Viewer/?peid=5a5b4de717dc4108a1b04133575b22181d&lt;br /&gt;
&lt;br /&gt;
[4] Course Schedule, and Lecture #9 Materials : http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures&lt;br /&gt;
&lt;br /&gt;
[5] Ruby on Rails official site : http://rubyonrails.org/&lt;br /&gt;
&lt;br /&gt;
[6] Sam Ruby, Agile Web Development with Rails, ISBN:1934356549&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=54188</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4d mt</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=54188"/>
		<updated>2011-10-27T17:45:19Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: /* Creating Categories */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''' The CookBook Application (Lecture 9)'''&lt;br /&gt;
=Introduction=&lt;br /&gt;
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.Rails includes a simple light weight application server [http://en.wikipedia.org/wiki/WEBrick Webrick] and a build system [http://en.wikipedia.org/wiki/Rake_(software) Rake] which makes development on rails easy.&lt;br /&gt;
&lt;br /&gt;
==Ruby on Rails==&lt;br /&gt;
Ruby on Rails, is an [http://en.wikipedia.org/wiki/Web_application_framework open source web application framework] for the [http://en.wikipedia.org/wiki/Ruby_(programming_language) 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:&lt;br /&gt;
===Convention over Configuration===&lt;br /&gt;
[http://en.wikipedia.org/wiki/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.&lt;br /&gt;
&lt;br /&gt;
===Don't repeat yourself=== &lt;br /&gt;
[http://en.wikipedia.org/wiki/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. Also in ActiveRecord there is no need to specify attribute names since it is picked up from the database tables itself.&lt;br /&gt;
&lt;br /&gt;
==Model/View/Controller Framework==&lt;br /&gt;
All Rails applications are implemented using the [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller 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.&lt;br /&gt;
Key components of MVC architecture include:&lt;br /&gt;
# 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.&lt;br /&gt;
#Views: Views provide a '''human interface(GUI)''' for the model. Views in Rails are implemented using embedded ruby (.html.erb) files&lt;br /&gt;
#Controllers: Controllers process the '''user actions''' and help redirect to the appropriate model ,build a user response and update the View. &lt;br /&gt;
The typical user interaction on a Rails application happens as follows:&lt;br /&gt;
#The user responds to the UI (ex : clicks a button)&lt;br /&gt;
#The router routes the action to the appropriate controller and action in it&lt;br /&gt;
#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&lt;br /&gt;
#A view queries the model and uses its data to build a response .&lt;br /&gt;
#The view updates the UI with the view and waits for further user interaction.&lt;br /&gt;
&lt;br /&gt;
==CRUD==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Create,_read,_update_and_delete 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:&lt;br /&gt;
#Create or add new entries&lt;br /&gt;
#Read, retrieve, search, or view existing entries&lt;br /&gt;
#Update or edit existing entries&lt;br /&gt;
#Delete existing entries&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
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 &amp;quot;contact management&amp;quot; or &amp;quot;contact maintenance&amp;quot; (or &amp;quot;document management&amp;quot; in general, depending on the basic storage unit for the particular application).&lt;br /&gt;
&lt;br /&gt;
==Routing in Rails==&lt;br /&gt;
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: &lt;br /&gt;
resources :photos &lt;br /&gt;
can allow us to specify 7 routes for the application.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! HTTP Verb&lt;br /&gt;
! Path &lt;br /&gt;
! action &lt;br /&gt;
! used for&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos || index || display a list of all photos&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos/new || new  || return an HTML form for creating a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  POST   ||	/photos	||create||	create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	||/photos/new ||	new ||	return an HTML form for creating a new photo &lt;br /&gt;
|-&lt;br /&gt;
|  POST	|| /photos	|| create	|| create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id	|| show	|| display a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id/edit	|| edit	|| return an HTML form for editing a photo&lt;br /&gt;
|-&lt;br /&gt;
|  PUT	|| /photos/:id ||	update	|| update a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  DELETE	|| /photos/:id	|| destroy	|| delete a specific photo&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
resources :photos&lt;br /&gt;
creates seven different routes in your application, all mapping to the Photos controller:&lt;br /&gt;
&lt;br /&gt;
==Object relational Mapping==&lt;br /&gt;
Rails handles Object Relational mapping using Active Records. ActiveRecords allow a Rails program to deal with objects and itself handles the mapping of these objects to relational databases. In Active Record, &lt;br /&gt;
# Database tables correspond to Rails classes&lt;br /&gt;
# Database records (rows) correspond to Rails objects.&lt;br /&gt;
ActiveRecord also allow us to query the database in the object domain by using the Active Record Query Interface&lt;br /&gt;
&lt;br /&gt;
=Developing the Cookbook App=&lt;br /&gt;
We now will look into how to develop the cookbook application, and talk about how to get started with the same. &lt;br /&gt;
To get started, we first create a new rails project called say 'CookBookApp', which is done by simply running the command : &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails new CookBookApp&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Once this is run, a series of folders are created, like app, config, db, etc. In developing the cookbook app, we will mainly look into the app, config and db folders mainly.&lt;br /&gt;
==Scaffolding==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Scaffold_%28programming%29 Scaffolding] is a very powerful command that creates code to create, read, update and delete (CRUD) data from the database. In the Cookbook application, the first scaffold is to create categories. Each Category will then have multiple recipes, and the main functionality of our cookbook app would be done.&lt;br /&gt;
The scaffold command to create categories is done by simply giving the command :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails generate scaffold category name:string&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
As seen, this generates the model called category, a bunch of views, and a category controller.&lt;br /&gt;
It also generates a migration, to create a new table called categories (notice that rails called the table as categories, which is the plural of category automatically).&lt;br /&gt;
===DB Migration===&lt;br /&gt;
The scaffold command generates a lot of files, and also a migration in db\migrate with a name like &amp;lt;timestamp&amp;gt;_create_categories.rb. This migration contains the details of the table that has to be created called 'categories', so the next step is to run the migrations. Before we do this, however, if we start the rails server, we get an error mentioning that the table 'categories' doesn't exist. &lt;br /&gt;
Let us now run the migration using the command :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After the migration is run, we know that the categories table has been created, and now run the server.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails server&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
===Creating Categories===&lt;br /&gt;
Now, if we goto the default localhost:3000 page, we see one option : Create a new category, as shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:2.jpg]]&lt;br /&gt;
&lt;br /&gt;
We can click on it, and make a new category, lets say 'Main Course'&lt;br /&gt;
&lt;br /&gt;
[[File:3.jpg]]&lt;br /&gt;
&lt;br /&gt;
When clicking on a new category notice that the URL is '''localhost:3000/categories/new'''. So we are executing the new method of the categories controller. On clicking 'Create Category', a new category with the name 'Main Course' is created, and a flash notice saying 'Successfully Create' is shown. We can now see the category in our list of categories.&lt;br /&gt;
&lt;br /&gt;
[[File:5.jpg]]&lt;br /&gt;
&lt;br /&gt;
So, as we saw, with just a simple scaffold command and db:migrate, we have an application with the basic CRUD functionality.&lt;br /&gt;
&lt;br /&gt;
===Creating Recipes===&lt;br /&gt;
Now, similar to creating categories, we create recipes using the scaffold command. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails generate scaffold recipe title:string description:string instructions:text&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Once this is done, we again have to run a db:migrate, so that the recipes table is in the database.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, the recipes have been created, and as seen in the recipe model, each recipe belongs to a category.&lt;br /&gt;
&lt;br /&gt;
==Relationships==&lt;br /&gt;
Relationships are always defined in the model. We first want to say that each recipe belongs to a category. This can be done in the recipe controller as shown below :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similarly, we want to add a detail mentioning that each Category can have many Recipes : a one to many relationship. To do this, we need to edit the Category model, and include the 'has_many' relationship with Recipe. The Category model now looks like :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
  has_many :recipes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We also have to add a reference in the migration, to indicate that the recipes table references the categories table. This can be done in the &amp;lt;timestamp&amp;gt;_create_recipes.rb migration, and now it would look like :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateRecipes &amp;lt; ActiveRecord::Migration&lt;br /&gt;
  def change&lt;br /&gt;
    create_table :recipes do |t|&lt;br /&gt;
      t.string :title&lt;br /&gt;
      t.string :description&lt;br /&gt;
      t.text :instructions&lt;br /&gt;
      t.references :category&lt;br /&gt;
      t.timestamps&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As before, since we have made a change to the migration, we now need to do a db:migrate.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Valdations==&lt;br /&gt;
Validations, just like Relationships, are also defined in the model. Suppose we want to make sure that each recipe entered has a valid title and instructions, and the condition being that they cannot be empty. We can add this to the Recipe model, which now looks like :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :category&lt;br /&gt;
  validates :title, :presence =&amp;gt; true&lt;br /&gt;
  validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
  validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Thus, the title, instructions and category are all validated, and it makes sure that title and instructions are not empty, and that the category it belongs also exists.&lt;br /&gt;
&lt;br /&gt;
==Routes.rb==&lt;br /&gt;
The routes.rb file is located in the config folder. routes.rb, as the name suggests is used to route to the corresponding controller's method. Suppose we have defined a new method in the Category controller. Now, if we want to route a link to the Category controller's new method from some recipe view, we have to include this in the routes.rb file. If we don't, we get an error saying 'No route matches ...'. Whenever we decide to add a new method to a controller, we have to add something similar to :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
match 'Controller_name/new_method_name' =&amp;gt; 'Controller_name#new_method_name'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Our application starts are localhost:3000\categories. Suppose we want to have the root localhost:3000 to the index method in the category controller, we can do:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to =&amp;gt; 'categories#index'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This has to be done after deleting the index.html file from the public directory.&lt;br /&gt;
&lt;br /&gt;
==Views &amp;amp; Layouts==&lt;br /&gt;
Now that the major portion of the application has been done, let us talk about Views and Layouts. &lt;br /&gt;
First, let us add the following code to application.html.erb in the app\views\layouts folder.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to &amp;quot;Show all recipes&amp;quot;, recipes_url %&amp;gt; &amp;lt;%= link_to &amp;quot;Show all categories&amp;quot;, categories_url %&amp;gt; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, when we start the rails server, we see the following page :&lt;br /&gt;
&lt;br /&gt;
[[File:6.jpg]]&lt;br /&gt;
&lt;br /&gt;
Since we want the user to choose a category for each recipe, we include the following code in our view (in the recipes\_form.html.erb in the views section of our app) :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= f.label :category %&amp;gt;&lt;br /&gt;
&amp;lt;%= select(&amp;quot;recipe&amp;quot;, &amp;quot;category_id&amp;quot;, Category.find(:all).collect{ |c| [ c.name, c.id] }) %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view now would have a drop box to choose which category and the user can write the recipe for that category. The new recipe page would now look like :&lt;br /&gt;
&lt;br /&gt;
[[File:7.jpg]]&lt;br /&gt;
&lt;br /&gt;
The CookBookApp now can have many categories, and recipes for each category. Note that we just used a few commands : scaffold, db:migrate, etc. to create this application. Since rails uses the Convention over Configuration principle, it makes it easier to write applications.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
[1] Ruby on Rails Guides: Getting Started With Rails :http://guides.rubyonrails.org/getting_started.html&lt;br /&gt;
&lt;br /&gt;
[2] CookBookApp : http://courses.ncsu.edu/csc517/common/exercises/cookbook3/&lt;br /&gt;
&lt;br /&gt;
[3] CSC 517 Fall 2011 : Engineering Online Video : http://mediasite.online.ncsu.edu/online/Viewer/?peid=5a5b4de717dc4108a1b04133575b22181d&lt;br /&gt;
&lt;br /&gt;
[4] Course Schedule, and Lecture #9 Materials : http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures&lt;br /&gt;
&lt;br /&gt;
[5] Ruby on Rails official site : http://rubyonrails.org/&lt;br /&gt;
&lt;br /&gt;
[6] Sam Ruby, Agile Web Development with Rails, ISBN:1934356549&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=54187</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4d mt</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=54187"/>
		<updated>2011-10-27T17:44:04Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: /* Don't repeat yourself */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''' The CookBook Application (Lecture 9)'''&lt;br /&gt;
=Introduction=&lt;br /&gt;
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.Rails includes a simple light weight application server [http://en.wikipedia.org/wiki/WEBrick Webrick] and a build system [http://en.wikipedia.org/wiki/Rake_(software) Rake] which makes development on rails easy.&lt;br /&gt;
&lt;br /&gt;
==Ruby on Rails==&lt;br /&gt;
Ruby on Rails, is an [http://en.wikipedia.org/wiki/Web_application_framework open source web application framework] for the [http://en.wikipedia.org/wiki/Ruby_(programming_language) 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:&lt;br /&gt;
===Convention over Configuration===&lt;br /&gt;
[http://en.wikipedia.org/wiki/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.&lt;br /&gt;
&lt;br /&gt;
===Don't repeat yourself=== &lt;br /&gt;
[http://en.wikipedia.org/wiki/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. Also in ActiveRecord there is no need to specify attribute names since it is picked up from the database tables itself.&lt;br /&gt;
&lt;br /&gt;
==Model/View/Controller Framework==&lt;br /&gt;
All Rails applications are implemented using the [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller 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.&lt;br /&gt;
Key components of MVC architecture include:&lt;br /&gt;
# 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.&lt;br /&gt;
#Views: Views provide a '''human interface(GUI)''' for the model. Views in Rails are implemented using embedded ruby (.html.erb) files&lt;br /&gt;
#Controllers: Controllers process the '''user actions''' and help redirect to the appropriate model ,build a user response and update the View. &lt;br /&gt;
The typical user interaction on a Rails application happens as follows:&lt;br /&gt;
#The user responds to the UI (ex : clicks a button)&lt;br /&gt;
#The router routes the action to the appropriate controller and action in it&lt;br /&gt;
#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&lt;br /&gt;
#A view queries the model and uses its data to build a response .&lt;br /&gt;
#The view updates the UI with the view and waits for further user interaction.&lt;br /&gt;
&lt;br /&gt;
==CRUD==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Create,_read,_update_and_delete 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:&lt;br /&gt;
#Create or add new entries&lt;br /&gt;
#Read, retrieve, search, or view existing entries&lt;br /&gt;
#Update or edit existing entries&lt;br /&gt;
#Delete existing entries&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
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 &amp;quot;contact management&amp;quot; or &amp;quot;contact maintenance&amp;quot; (or &amp;quot;document management&amp;quot; in general, depending on the basic storage unit for the particular application).&lt;br /&gt;
&lt;br /&gt;
==Routing in Rails==&lt;br /&gt;
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: &lt;br /&gt;
resources :photos &lt;br /&gt;
can allow us to specify 7 routes for the application.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! HTTP Verb&lt;br /&gt;
! Path &lt;br /&gt;
! action &lt;br /&gt;
! used for&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos || index || display a list of all photos&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos/new || new  || return an HTML form for creating a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  POST   ||	/photos	||create||	create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	||/photos/new ||	new ||	return an HTML form for creating a new photo &lt;br /&gt;
|-&lt;br /&gt;
|  POST	|| /photos	|| create	|| create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id	|| show	|| display a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id/edit	|| edit	|| return an HTML form for editing a photo&lt;br /&gt;
|-&lt;br /&gt;
|  PUT	|| /photos/:id ||	update	|| update a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  DELETE	|| /photos/:id	|| destroy	|| delete a specific photo&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
resources :photos&lt;br /&gt;
creates seven different routes in your application, all mapping to the Photos controller:&lt;br /&gt;
&lt;br /&gt;
==Object relational Mapping==&lt;br /&gt;
Rails handles Object Relational mapping using Active Records. ActiveRecords allow a Rails program to deal with objects and itself handles the mapping of these objects to relational databases. In Active Record, &lt;br /&gt;
# Database tables correspond to Rails classes&lt;br /&gt;
# Database records (rows) correspond to Rails objects.&lt;br /&gt;
ActiveRecord also allow us to query the database in the object domain by using the Active Record Query Interface&lt;br /&gt;
&lt;br /&gt;
=Developing the Cookbook App=&lt;br /&gt;
We now will look into how to develop the cookbook application, and talk about how to get started with the same. &lt;br /&gt;
To get started, we first create a new rails project called say 'CookBookApp', which is done by simply running the command : &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails new CookBookApp&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Once this is run, a series of folders are created, like app, config, db, etc. In developing the cookbook app, we will mainly look into the app, config and db folders mainly.&lt;br /&gt;
==Scaffolding==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Scaffold_%28programming%29 Scaffolding] is a very powerful command that creates code to create, read, update and delete (CRUD) data from the database. In the Cookbook application, the first scaffold is to create categories. Each Category will then have multiple recipes, and the main functionality of our cookbook app would be done.&lt;br /&gt;
The scaffold command to create categories is done by simply giving the command :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails generate scaffold category name:string&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
As seen, this generates the model called category, a bunch of views, and a category controller.&lt;br /&gt;
It also generates a migration, to create a new table called categories (notice that rails called the table as categories, which is the plural of category automatically).&lt;br /&gt;
===DB Migration===&lt;br /&gt;
The scaffold command generates a lot of files, and also a migration in db\migrate with a name like &amp;lt;timestamp&amp;gt;_create_categories.rb. This migration contains the details of the table that has to be created called 'categories', so the next step is to run the migrations. Before we do this, however, if we start the rails server, we get an error mentioning that the table 'categories' doesn't exist. &lt;br /&gt;
Let us now run the migration using the command :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After the migration is run, we know that the categories table has been created, and now run the server.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails server&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
===Creating Categories===&lt;br /&gt;
Now, if we goto the default localhost:3000 page, we see one option : Create a new category, as shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:2.jpg]]&lt;br /&gt;
&lt;br /&gt;
We can click on it, and make a new category, lets say 'Main Course'&lt;br /&gt;
&lt;br /&gt;
[[File:3.jpg]]&lt;br /&gt;
&lt;br /&gt;
When clicking on a new category notice that the URL is localhost:3000/categories/new. So we are executing the new method of the categories controller. On clicking 'Create Category', a new category with the name 'Main Course' is created, and a flash notice saying 'Successfully Create' is shown. We can now see the category in our list of categories.&lt;br /&gt;
&lt;br /&gt;
[[File:5.jpg]]&lt;br /&gt;
&lt;br /&gt;
So, as we saw, with just a simple scaffold command and db:migrate, we have an application with the basic CRUD functionality.&lt;br /&gt;
===Creating Recipes===&lt;br /&gt;
Now, similar to creating categories, we create recipes using the scaffold command. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails generate scaffold recipe title:string description:string instructions:text&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Once this is done, we again have to run a db:migrate, so that the recipes table is in the database.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, the recipes have been created, and as seen in the recipe model, each recipe belongs to a category.&lt;br /&gt;
&lt;br /&gt;
==Relationships==&lt;br /&gt;
Relationships are always defined in the model. We first want to say that each recipe belongs to a category. This can be done in the recipe controller as shown below :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similarly, we want to add a detail mentioning that each Category can have many Recipes : a one to many relationship. To do this, we need to edit the Category model, and include the 'has_many' relationship with Recipe. The Category model now looks like :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
  has_many :recipes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We also have to add a reference in the migration, to indicate that the recipes table references the categories table. This can be done in the &amp;lt;timestamp&amp;gt;_create_recipes.rb migration, and now it would look like :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateRecipes &amp;lt; ActiveRecord::Migration&lt;br /&gt;
  def change&lt;br /&gt;
    create_table :recipes do |t|&lt;br /&gt;
      t.string :title&lt;br /&gt;
      t.string :description&lt;br /&gt;
      t.text :instructions&lt;br /&gt;
      t.references :category&lt;br /&gt;
      t.timestamps&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As before, since we have made a change to the migration, we now need to do a db:migrate.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Valdations==&lt;br /&gt;
Validations, just like Relationships, are also defined in the model. Suppose we want to make sure that each recipe entered has a valid title and instructions, and the condition being that they cannot be empty. We can add this to the Recipe model, which now looks like :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :category&lt;br /&gt;
  validates :title, :presence =&amp;gt; true&lt;br /&gt;
  validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
  validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Thus, the title, instructions and category are all validated, and it makes sure that title and instructions are not empty, and that the category it belongs also exists.&lt;br /&gt;
&lt;br /&gt;
==Routes.rb==&lt;br /&gt;
The routes.rb file is located in the config folder. routes.rb, as the name suggests is used to route to the corresponding controller's method. Suppose we have defined a new method in the Category controller. Now, if we want to route a link to the Category controller's new method from some recipe view, we have to include this in the routes.rb file. If we don't, we get an error saying 'No route matches ...'. Whenever we decide to add a new method to a controller, we have to add something similar to :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
match 'Controller_name/new_method_name' =&amp;gt; 'Controller_name#new_method_name'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Our application starts are localhost:3000\categories. Suppose we want to have the root localhost:3000 to the index method in the category controller, we can do:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to =&amp;gt; 'categories#index'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This has to be done after deleting the index.html file from the public directory.&lt;br /&gt;
&lt;br /&gt;
==Views &amp;amp; Layouts==&lt;br /&gt;
Now that the major portion of the application has been done, let us talk about Views and Layouts. &lt;br /&gt;
First, let us add the following code to application.html.erb in the app\views\layouts folder.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to &amp;quot;Show all recipes&amp;quot;, recipes_url %&amp;gt; &amp;lt;%= link_to &amp;quot;Show all categories&amp;quot;, categories_url %&amp;gt; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, when we start the rails server, we see the following page :&lt;br /&gt;
&lt;br /&gt;
[[File:6.jpg]]&lt;br /&gt;
&lt;br /&gt;
Since we want the user to choose a category for each recipe, we include the following code in our view (in the recipes\_form.html.erb in the views section of our app) :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= f.label :category %&amp;gt;&lt;br /&gt;
&amp;lt;%= select(&amp;quot;recipe&amp;quot;, &amp;quot;category_id&amp;quot;, Category.find(:all).collect{ |c| [ c.name, c.id] }) %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view now would have a drop box to choose which category and the user can write the recipe for that category. The new recipe page would now look like :&lt;br /&gt;
&lt;br /&gt;
[[File:7.jpg]]&lt;br /&gt;
&lt;br /&gt;
The CookBookApp now can have many categories, and recipes for each category. Note that we just used a few commands : scaffold, db:migrate, etc. to create this application. Since rails uses the Convention over Configuration principle, it makes it easier to write applications.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
[1] Ruby on Rails Guides: Getting Started With Rails :http://guides.rubyonrails.org/getting_started.html&lt;br /&gt;
&lt;br /&gt;
[2] CookBookApp : http://courses.ncsu.edu/csc517/common/exercises/cookbook3/&lt;br /&gt;
&lt;br /&gt;
[3] CSC 517 Fall 2011 : Engineering Online Video : http://mediasite.online.ncsu.edu/online/Viewer/?peid=5a5b4de717dc4108a1b04133575b22181d&lt;br /&gt;
&lt;br /&gt;
[4] Course Schedule, and Lecture #9 Materials : http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures&lt;br /&gt;
&lt;br /&gt;
[5] Ruby on Rails official site : http://rubyonrails.org/&lt;br /&gt;
&lt;br /&gt;
[6] Sam Ruby, Agile Web Development with Rails, ISBN:1934356549&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=54186</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4d mt</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=54186"/>
		<updated>2011-10-27T17:43:31Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: /* Model/View/Controller Framework */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''' The CookBook Application (Lecture 9)'''&lt;br /&gt;
=Introduction=&lt;br /&gt;
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.Rails includes a simple light weight application server [http://en.wikipedia.org/wiki/WEBrick Webrick] and a build system [http://en.wikipedia.org/wiki/Rake_(software) Rake] which makes development on rails easy.&lt;br /&gt;
&lt;br /&gt;
==Ruby on Rails==&lt;br /&gt;
Ruby on Rails, is an [http://en.wikipedia.org/wiki/Web_application_framework open source web application framework] for the [http://en.wikipedia.org/wiki/Ruby_(programming_language) 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:&lt;br /&gt;
===Convention over Configuration===&lt;br /&gt;
[http://en.wikipedia.org/wiki/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.&lt;br /&gt;
&lt;br /&gt;
===Don't repeat yourself=== &lt;br /&gt;
[http://en.wikipedia.org/wiki/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. Also in ActiveRecord there is no need to specify attribute names since it is picked up from the database tables itself.&lt;br /&gt;
&lt;br /&gt;
==Model/View/Controller Framework==&lt;br /&gt;
All Rails applications are implemented using the [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller 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.&lt;br /&gt;
Key components of MVC architecture include:&lt;br /&gt;
# 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.&lt;br /&gt;
#Views: Views provide a '''human interface(GUI)''' for the model. Views in Rails are implemented using embedded ruby (.html.erb) files&lt;br /&gt;
#Controllers: Controllers process the '''user actions''' and help redirect to the appropriate model ,build a user response and update the View. &lt;br /&gt;
The typical user interaction on a Rails application happens as follows:&lt;br /&gt;
#The user responds to the UI (ex : clicks a button)&lt;br /&gt;
#The router routes the action to the appropriate controller and action in it&lt;br /&gt;
#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&lt;br /&gt;
#A view queries the model and uses its data to build a response .&lt;br /&gt;
#The view updates the UI with the view and waits for further user interaction.&lt;br /&gt;
&lt;br /&gt;
==CRUD==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Create,_read,_update_and_delete 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:&lt;br /&gt;
#Create or add new entries&lt;br /&gt;
#Read, retrieve, search, or view existing entries&lt;br /&gt;
#Update or edit existing entries&lt;br /&gt;
#Delete existing entries&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
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 &amp;quot;contact management&amp;quot; or &amp;quot;contact maintenance&amp;quot; (or &amp;quot;document management&amp;quot; in general, depending on the basic storage unit for the particular application).&lt;br /&gt;
&lt;br /&gt;
==Routing in Rails==&lt;br /&gt;
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: &lt;br /&gt;
resources :photos &lt;br /&gt;
can allow us to specify 7 routes for the application.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! HTTP Verb&lt;br /&gt;
! Path &lt;br /&gt;
! action &lt;br /&gt;
! used for&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos || index || display a list of all photos&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos/new || new  || return an HTML form for creating a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  POST   ||	/photos	||create||	create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	||/photos/new ||	new ||	return an HTML form for creating a new photo &lt;br /&gt;
|-&lt;br /&gt;
|  POST	|| /photos	|| create	|| create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id	|| show	|| display a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id/edit	|| edit	|| return an HTML form for editing a photo&lt;br /&gt;
|-&lt;br /&gt;
|  PUT	|| /photos/:id ||	update	|| update a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  DELETE	|| /photos/:id	|| destroy	|| delete a specific photo&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
resources :photos&lt;br /&gt;
creates seven different routes in your application, all mapping to the Photos controller:&lt;br /&gt;
&lt;br /&gt;
==Object relational Mapping==&lt;br /&gt;
Rails handles Object Relational mapping using Active Records. ActiveRecords allow a Rails program to deal with objects and itself handles the mapping of these objects to relational databases. In Active Record, &lt;br /&gt;
# Database tables correspond to Rails classes&lt;br /&gt;
# Database records (rows) correspond to Rails objects.&lt;br /&gt;
ActiveRecord also allow us to query the database in the object domain by using the Active Record Query Interface&lt;br /&gt;
&lt;br /&gt;
=Developing the Cookbook App=&lt;br /&gt;
We now will look into how to develop the cookbook application, and talk about how to get started with the same. &lt;br /&gt;
To get started, we first create a new rails project called say 'CookBookApp', which is done by simply running the command : &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails new CookBookApp&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Once this is run, a series of folders are created, like app, config, db, etc. In developing the cookbook app, we will mainly look into the app, config and db folders mainly.&lt;br /&gt;
==Scaffolding==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Scaffold_%28programming%29 Scaffolding] is a very powerful command that creates code to create, read, update and delete (CRUD) data from the database. In the Cookbook application, the first scaffold is to create categories. Each Category will then have multiple recipes, and the main functionality of our cookbook app would be done.&lt;br /&gt;
The scaffold command to create categories is done by simply giving the command :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails generate scaffold category name:string&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
As seen, this generates the model called category, a bunch of views, and a category controller.&lt;br /&gt;
It also generates a migration, to create a new table called categories (notice that rails called the table as categories, which is the plural of category automatically).&lt;br /&gt;
===DB Migration===&lt;br /&gt;
The scaffold command generates a lot of files, and also a migration in db\migrate with a name like &amp;lt;timestamp&amp;gt;_create_categories.rb. This migration contains the details of the table that has to be created called 'categories', so the next step is to run the migrations. Before we do this, however, if we start the rails server, we get an error mentioning that the table 'categories' doesn't exist. &lt;br /&gt;
Let us now run the migration using the command :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After the migration is run, we know that the categories table has been created, and now run the server.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails server&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
===Creating Categories===&lt;br /&gt;
Now, if we goto the default localhost:3000 page, we see one option : Create a new category, as shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:2.jpg]]&lt;br /&gt;
&lt;br /&gt;
We can click on it, and make a new category, lets say 'Main Course'&lt;br /&gt;
&lt;br /&gt;
[[File:3.jpg]]&lt;br /&gt;
&lt;br /&gt;
When clicking on a new category notice that the URL is localhost:3000/categories/new. So we are executing the new method of the categories controller. On clicking 'Create Category', a new category with the name 'Main Course' is created, and a flash notice saying 'Successfully Create' is shown. We can now see the category in our list of categories.&lt;br /&gt;
&lt;br /&gt;
[[File:5.jpg]]&lt;br /&gt;
&lt;br /&gt;
So, as we saw, with just a simple scaffold command and db:migrate, we have an application with the basic CRUD functionality.&lt;br /&gt;
===Creating Recipes===&lt;br /&gt;
Now, similar to creating categories, we create recipes using the scaffold command. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails generate scaffold recipe title:string description:string instructions:text&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Once this is done, we again have to run a db:migrate, so that the recipes table is in the database.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, the recipes have been created, and as seen in the recipe model, each recipe belongs to a category.&lt;br /&gt;
&lt;br /&gt;
==Relationships==&lt;br /&gt;
Relationships are always defined in the model. We first want to say that each recipe belongs to a category. This can be done in the recipe controller as shown below :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similarly, we want to add a detail mentioning that each Category can have many Recipes : a one to many relationship. To do this, we need to edit the Category model, and include the 'has_many' relationship with Recipe. The Category model now looks like :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
  has_many :recipes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We also have to add a reference in the migration, to indicate that the recipes table references the categories table. This can be done in the &amp;lt;timestamp&amp;gt;_create_recipes.rb migration, and now it would look like :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateRecipes &amp;lt; ActiveRecord::Migration&lt;br /&gt;
  def change&lt;br /&gt;
    create_table :recipes do |t|&lt;br /&gt;
      t.string :title&lt;br /&gt;
      t.string :description&lt;br /&gt;
      t.text :instructions&lt;br /&gt;
      t.references :category&lt;br /&gt;
      t.timestamps&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As before, since we have made a change to the migration, we now need to do a db:migrate.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Valdations==&lt;br /&gt;
Validations, just like Relationships, are also defined in the model. Suppose we want to make sure that each recipe entered has a valid title and instructions, and the condition being that they cannot be empty. We can add this to the Recipe model, which now looks like :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :category&lt;br /&gt;
  validates :title, :presence =&amp;gt; true&lt;br /&gt;
  validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
  validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Thus, the title, instructions and category are all validated, and it makes sure that title and instructions are not empty, and that the category it belongs also exists.&lt;br /&gt;
&lt;br /&gt;
==Routes.rb==&lt;br /&gt;
The routes.rb file is located in the config folder. routes.rb, as the name suggests is used to route to the corresponding controller's method. Suppose we have defined a new method in the Category controller. Now, if we want to route a link to the Category controller's new method from some recipe view, we have to include this in the routes.rb file. If we don't, we get an error saying 'No route matches ...'. Whenever we decide to add a new method to a controller, we have to add something similar to :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
match 'Controller_name/new_method_name' =&amp;gt; 'Controller_name#new_method_name'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Our application starts are localhost:3000\categories. Suppose we want to have the root localhost:3000 to the index method in the category controller, we can do:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to =&amp;gt; 'categories#index'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This has to be done after deleting the index.html file from the public directory.&lt;br /&gt;
&lt;br /&gt;
==Views &amp;amp; Layouts==&lt;br /&gt;
Now that the major portion of the application has been done, let us talk about Views and Layouts. &lt;br /&gt;
First, let us add the following code to application.html.erb in the app\views\layouts folder.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to &amp;quot;Show all recipes&amp;quot;, recipes_url %&amp;gt; &amp;lt;%= link_to &amp;quot;Show all categories&amp;quot;, categories_url %&amp;gt; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, when we start the rails server, we see the following page :&lt;br /&gt;
&lt;br /&gt;
[[File:6.jpg]]&lt;br /&gt;
&lt;br /&gt;
Since we want the user to choose a category for each recipe, we include the following code in our view (in the recipes\_form.html.erb in the views section of our app) :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= f.label :category %&amp;gt;&lt;br /&gt;
&amp;lt;%= select(&amp;quot;recipe&amp;quot;, &amp;quot;category_id&amp;quot;, Category.find(:all).collect{ |c| [ c.name, c.id] }) %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view now would have a drop box to choose which category and the user can write the recipe for that category. The new recipe page would now look like :&lt;br /&gt;
&lt;br /&gt;
[[File:7.jpg]]&lt;br /&gt;
&lt;br /&gt;
The CookBookApp now can have many categories, and recipes for each category. Note that we just used a few commands : scaffold, db:migrate, etc. to create this application. Since rails uses the Convention over Configuration principle, it makes it easier to write applications.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
[1] Ruby on Rails Guides: Getting Started With Rails :http://guides.rubyonrails.org/getting_started.html&lt;br /&gt;
&lt;br /&gt;
[2] CookBookApp : http://courses.ncsu.edu/csc517/common/exercises/cookbook3/&lt;br /&gt;
&lt;br /&gt;
[3] CSC 517 Fall 2011 : Engineering Online Video : http://mediasite.online.ncsu.edu/online/Viewer/?peid=5a5b4de717dc4108a1b04133575b22181d&lt;br /&gt;
&lt;br /&gt;
[4] Course Schedule, and Lecture #9 Materials : http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures&lt;br /&gt;
&lt;br /&gt;
[5] Ruby on Rails official site : http://rubyonrails.org/&lt;br /&gt;
&lt;br /&gt;
[6] Sam Ruby, Agile Web Development with Rails, ISBN:1934356549&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=54185</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4d mt</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=54185"/>
		<updated>2011-10-27T17:42:49Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: /* Convention over Configuration */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''' The CookBook Application (Lecture 9)'''&lt;br /&gt;
=Introduction=&lt;br /&gt;
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.Rails includes a simple light weight application server [http://en.wikipedia.org/wiki/WEBrick Webrick] and a build system [http://en.wikipedia.org/wiki/Rake_(software) Rake] which makes development on rails easy.&lt;br /&gt;
&lt;br /&gt;
==Ruby on Rails==&lt;br /&gt;
Ruby on Rails, is an [http://en.wikipedia.org/wiki/Web_application_framework open source web application framework] for the [http://en.wikipedia.org/wiki/Ruby_(programming_language) 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:&lt;br /&gt;
===Convention over Configuration===&lt;br /&gt;
[http://en.wikipedia.org/wiki/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.&lt;br /&gt;
&lt;br /&gt;
===Don't repeat yourself=== &lt;br /&gt;
[http://en.wikipedia.org/wiki/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. Also in ActiveRecord there is no need to specify attribute names since it is picked up from the database tables itself.&lt;br /&gt;
&lt;br /&gt;
==Model/View/Controller Framework==&lt;br /&gt;
All Rails applications are implemented using the [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller 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.&lt;br /&gt;
Key components of MVC architecture include:&lt;br /&gt;
# 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.&lt;br /&gt;
#Views: Views provide a human interface(GUI) for the model. Views in Rails are implemented using embedded ruby (.html.erb) files&lt;br /&gt;
#Controllers: Controllers process the user actions and help redirect to the appropriate model ,build a user response and update the View. &lt;br /&gt;
The typical user interaction on a Rails application happens as follows:&lt;br /&gt;
#The user responds to the UI (ex : clicks a button)&lt;br /&gt;
#The router routes the action to the appropriate controller and action in it&lt;br /&gt;
#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&lt;br /&gt;
#A view queries the model and uses its data to build a response .&lt;br /&gt;
#The view updates the UI with the view and waits for further user interaction.&lt;br /&gt;
&lt;br /&gt;
==CRUD==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Create,_read,_update_and_delete 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:&lt;br /&gt;
#Create or add new entries&lt;br /&gt;
#Read, retrieve, search, or view existing entries&lt;br /&gt;
#Update or edit existing entries&lt;br /&gt;
#Delete existing entries&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
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 &amp;quot;contact management&amp;quot; or &amp;quot;contact maintenance&amp;quot; (or &amp;quot;document management&amp;quot; in general, depending on the basic storage unit for the particular application).&lt;br /&gt;
&lt;br /&gt;
==Routing in Rails==&lt;br /&gt;
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: &lt;br /&gt;
resources :photos &lt;br /&gt;
can allow us to specify 7 routes for the application.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! HTTP Verb&lt;br /&gt;
! Path &lt;br /&gt;
! action &lt;br /&gt;
! used for&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos || index || display a list of all photos&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos/new || new  || return an HTML form for creating a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  POST   ||	/photos	||create||	create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	||/photos/new ||	new ||	return an HTML form for creating a new photo &lt;br /&gt;
|-&lt;br /&gt;
|  POST	|| /photos	|| create	|| create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id	|| show	|| display a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id/edit	|| edit	|| return an HTML form for editing a photo&lt;br /&gt;
|-&lt;br /&gt;
|  PUT	|| /photos/:id ||	update	|| update a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  DELETE	|| /photos/:id	|| destroy	|| delete a specific photo&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
resources :photos&lt;br /&gt;
creates seven different routes in your application, all mapping to the Photos controller:&lt;br /&gt;
&lt;br /&gt;
==Object relational Mapping==&lt;br /&gt;
Rails handles Object Relational mapping using Active Records. ActiveRecords allow a Rails program to deal with objects and itself handles the mapping of these objects to relational databases. In Active Record, &lt;br /&gt;
# Database tables correspond to Rails classes&lt;br /&gt;
# Database records (rows) correspond to Rails objects.&lt;br /&gt;
ActiveRecord also allow us to query the database in the object domain by using the Active Record Query Interface&lt;br /&gt;
&lt;br /&gt;
=Developing the Cookbook App=&lt;br /&gt;
We now will look into how to develop the cookbook application, and talk about how to get started with the same. &lt;br /&gt;
To get started, we first create a new rails project called say 'CookBookApp', which is done by simply running the command : &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails new CookBookApp&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Once this is run, a series of folders are created, like app, config, db, etc. In developing the cookbook app, we will mainly look into the app, config and db folders mainly.&lt;br /&gt;
==Scaffolding==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Scaffold_%28programming%29 Scaffolding] is a very powerful command that creates code to create, read, update and delete (CRUD) data from the database. In the Cookbook application, the first scaffold is to create categories. Each Category will then have multiple recipes, and the main functionality of our cookbook app would be done.&lt;br /&gt;
The scaffold command to create categories is done by simply giving the command :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails generate scaffold category name:string&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
As seen, this generates the model called category, a bunch of views, and a category controller.&lt;br /&gt;
It also generates a migration, to create a new table called categories (notice that rails called the table as categories, which is the plural of category automatically).&lt;br /&gt;
===DB Migration===&lt;br /&gt;
The scaffold command generates a lot of files, and also a migration in db\migrate with a name like &amp;lt;timestamp&amp;gt;_create_categories.rb. This migration contains the details of the table that has to be created called 'categories', so the next step is to run the migrations. Before we do this, however, if we start the rails server, we get an error mentioning that the table 'categories' doesn't exist. &lt;br /&gt;
Let us now run the migration using the command :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After the migration is run, we know that the categories table has been created, and now run the server.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails server&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
===Creating Categories===&lt;br /&gt;
Now, if we goto the default localhost:3000 page, we see one option : Create a new category, as shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:2.jpg]]&lt;br /&gt;
&lt;br /&gt;
We can click on it, and make a new category, lets say 'Main Course'&lt;br /&gt;
&lt;br /&gt;
[[File:3.jpg]]&lt;br /&gt;
&lt;br /&gt;
When clicking on a new category notice that the URL is localhost:3000/categories/new. So we are executing the new method of the categories controller. On clicking 'Create Category', a new category with the name 'Main Course' is created, and a flash notice saying 'Successfully Create' is shown. We can now see the category in our list of categories.&lt;br /&gt;
&lt;br /&gt;
[[File:5.jpg]]&lt;br /&gt;
&lt;br /&gt;
So, as we saw, with just a simple scaffold command and db:migrate, we have an application with the basic CRUD functionality.&lt;br /&gt;
===Creating Recipes===&lt;br /&gt;
Now, similar to creating categories, we create recipes using the scaffold command. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails generate scaffold recipe title:string description:string instructions:text&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Once this is done, we again have to run a db:migrate, so that the recipes table is in the database.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, the recipes have been created, and as seen in the recipe model, each recipe belongs to a category.&lt;br /&gt;
&lt;br /&gt;
==Relationships==&lt;br /&gt;
Relationships are always defined in the model. We first want to say that each recipe belongs to a category. This can be done in the recipe controller as shown below :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similarly, we want to add a detail mentioning that each Category can have many Recipes : a one to many relationship. To do this, we need to edit the Category model, and include the 'has_many' relationship with Recipe. The Category model now looks like :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
  has_many :recipes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We also have to add a reference in the migration, to indicate that the recipes table references the categories table. This can be done in the &amp;lt;timestamp&amp;gt;_create_recipes.rb migration, and now it would look like :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CreateRecipes &amp;lt; ActiveRecord::Migration&lt;br /&gt;
  def change&lt;br /&gt;
    create_table :recipes do |t|&lt;br /&gt;
      t.string :title&lt;br /&gt;
      t.string :description&lt;br /&gt;
      t.text :instructions&lt;br /&gt;
      t.references :category&lt;br /&gt;
      t.timestamps&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As before, since we have made a change to the migration, we now need to do a db:migrate.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Valdations==&lt;br /&gt;
Validations, just like Relationships, are also defined in the model. Suppose we want to make sure that each recipe entered has a valid title and instructions, and the condition being that they cannot be empty. We can add this to the Recipe model, which now looks like :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :category&lt;br /&gt;
  validates :title, :presence =&amp;gt; true&lt;br /&gt;
  validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
  validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Thus, the title, instructions and category are all validated, and it makes sure that title and instructions are not empty, and that the category it belongs also exists.&lt;br /&gt;
&lt;br /&gt;
==Routes.rb==&lt;br /&gt;
The routes.rb file is located in the config folder. routes.rb, as the name suggests is used to route to the corresponding controller's method. Suppose we have defined a new method in the Category controller. Now, if we want to route a link to the Category controller's new method from some recipe view, we have to include this in the routes.rb file. If we don't, we get an error saying 'No route matches ...'. Whenever we decide to add a new method to a controller, we have to add something similar to :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
match 'Controller_name/new_method_name' =&amp;gt; 'Controller_name#new_method_name'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Our application starts are localhost:3000\categories. Suppose we want to have the root localhost:3000 to the index method in the category controller, we can do:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to =&amp;gt; 'categories#index'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This has to be done after deleting the index.html file from the public directory.&lt;br /&gt;
&lt;br /&gt;
==Views &amp;amp; Layouts==&lt;br /&gt;
Now that the major portion of the application has been done, let us talk about Views and Layouts. &lt;br /&gt;
First, let us add the following code to application.html.erb in the app\views\layouts folder.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to &amp;quot;Show all recipes&amp;quot;, recipes_url %&amp;gt; &amp;lt;%= link_to &amp;quot;Show all categories&amp;quot;, categories_url %&amp;gt; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, when we start the rails server, we see the following page :&lt;br /&gt;
&lt;br /&gt;
[[File:6.jpg]]&lt;br /&gt;
&lt;br /&gt;
Since we want the user to choose a category for each recipe, we include the following code in our view (in the recipes\_form.html.erb in the views section of our app) :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= f.label :category %&amp;gt;&lt;br /&gt;
&amp;lt;%= select(&amp;quot;recipe&amp;quot;, &amp;quot;category_id&amp;quot;, Category.find(:all).collect{ |c| [ c.name, c.id] }) %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view now would have a drop box to choose which category and the user can write the recipe for that category. The new recipe page would now look like :&lt;br /&gt;
&lt;br /&gt;
[[File:7.jpg]]&lt;br /&gt;
&lt;br /&gt;
The CookBookApp now can have many categories, and recipes for each category. Note that we just used a few commands : scaffold, db:migrate, etc. to create this application. Since rails uses the Convention over Configuration principle, it makes it easier to write applications.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
[1] Ruby on Rails Guides: Getting Started With Rails :http://guides.rubyonrails.org/getting_started.html&lt;br /&gt;
&lt;br /&gt;
[2] CookBookApp : http://courses.ncsu.edu/csc517/common/exercises/cookbook3/&lt;br /&gt;
&lt;br /&gt;
[3] CSC 517 Fall 2011 : Engineering Online Video : http://mediasite.online.ncsu.edu/online/Viewer/?peid=5a5b4de717dc4108a1b04133575b22181d&lt;br /&gt;
&lt;br /&gt;
[4] Course Schedule, and Lecture #9 Materials : http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures&lt;br /&gt;
&lt;br /&gt;
[5] Ruby on Rails official site : http://rubyonrails.org/&lt;br /&gt;
&lt;br /&gt;
[6] Sam Ruby, Agile Web Development with Rails, ISBN:1934356549&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=52879</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4d mt</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=52879"/>
		<updated>2011-10-20T03:26:59Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''' The CookBook Application (Lecture 9)'''&lt;br /&gt;
=Introduction=&lt;br /&gt;
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.Rails includes a simple light weight application server [http://en.wikipedia.org/wiki/WEBrick Webrick] and a build system [http://en.wikipedia.org/wiki/Rake_(software) Rake] which makes development on rails easy.&lt;br /&gt;
&lt;br /&gt;
==Ruby on Rails==&lt;br /&gt;
Ruby on Rails, is an [http://en.wikipedia.org/wiki/Web_application_framework open source web application framework] for the [http://en.wikipedia.org/wiki/Ruby_(programming_language) 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:&lt;br /&gt;
===Convention over Configuration===&lt;br /&gt;
[http://en.wikipedia.org/wiki/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.&lt;br /&gt;
&lt;br /&gt;
===Don't repeat yourself=== &lt;br /&gt;
[http://en.wikipedia.org/wiki/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. Also in ActiveRecord there is no need to specify attribute names since it is picked up from the database tables itself.&lt;br /&gt;
&lt;br /&gt;
==Model/View/Controller Framework==&lt;br /&gt;
All Rails applications are implemented using the [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller 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.&lt;br /&gt;
Key components of MVC architecture include:&lt;br /&gt;
# 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.&lt;br /&gt;
#Views: Views provide a human interface(GUI) for the model. Views in Rails are implemented using embedded ruby (.html.erb) files&lt;br /&gt;
#Controllers: Controllers process the user actions and help redirect to the appropriate model ,build a user response and update the View. &lt;br /&gt;
The typical user interaction on a Rails application happens as follows:&lt;br /&gt;
#The user responds to the UI (ex : clicks a button)&lt;br /&gt;
#The router routes the action to the appropriate controller and action in it&lt;br /&gt;
#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&lt;br /&gt;
#A view queries the model and uses its data to build a response .&lt;br /&gt;
#The view updates the UI with the view and waits for further user interaction.&lt;br /&gt;
&lt;br /&gt;
==CRUD==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Create,_read,_update_and_delete 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:&lt;br /&gt;
#Create or add new entries&lt;br /&gt;
#Read, retrieve, search, or view existing entries&lt;br /&gt;
#Update or edit existing entries&lt;br /&gt;
#Delete existing entries&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
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 &amp;quot;contact management&amp;quot; or &amp;quot;contact maintenance&amp;quot; (or &amp;quot;document management&amp;quot; in general, depending on the basic storage unit for the particular application).&lt;br /&gt;
&lt;br /&gt;
==Routing in Rails==&lt;br /&gt;
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: &lt;br /&gt;
resources :photos &lt;br /&gt;
can allow us to specify 7 routes for the application.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! HTTP Verb&lt;br /&gt;
! Path &lt;br /&gt;
! action &lt;br /&gt;
! used for&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos || index || display a list of all photos&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos/new || new  || return an HTML form for creating a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  POST   ||	/photos	||create||	create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	||/photos/new ||	new ||	return an HTML form for creating a new photo &lt;br /&gt;
|-&lt;br /&gt;
|  POST	|| /photos	|| create	|| create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id	|| show	|| display a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id/edit	|| edit	|| return an HTML form for editing a photo&lt;br /&gt;
|-&lt;br /&gt;
|  PUT	|| /photos/:id ||	update	|| update a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  DELETE	|| /photos/:id	|| destroy	|| delete a specific photo&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
resources :photos&lt;br /&gt;
creates seven different routes in your application, all mapping to the Photos controller:&lt;br /&gt;
&lt;br /&gt;
==Object relational Mapping==&lt;br /&gt;
Rails handles Object Relational mapping using Active Records. ActiveRecords allow a Rails program to deal with objects and itself handles the mapping of these objects to relational databases. In Active Record, &lt;br /&gt;
# Database tables correspond to Rails classes&lt;br /&gt;
# Database records (rows) correspond to Rails objects.&lt;br /&gt;
ActiveRecord also allow us to query the database in the object domain by using the Active Record Query Interface&lt;br /&gt;
&lt;br /&gt;
=Developing the Cookbook App=&lt;br /&gt;
We now will look into how to develop the cookbook application, and talk about how to get started with the same. &lt;br /&gt;
To get started, we first create a new rails project called say 'CookBookApp', which is done by simply running the command : &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails new CookBookApp&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Once this is run, a series of folders are created, like app, config, db, etc. In developing the cookbook app, we will mainly look into the app, config and db folders mainly.&lt;br /&gt;
==Scaffolding==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Scaffold_%28programming%29 Scaffolding] is a very powerful command that creates code to create, read, update and delete (CRUD) data from the database. In the Cookbook application, the first scaffold is to create categories. Each Category will then have multiple recipes, and the main functionality of our cookbook app would be done.&lt;br /&gt;
The scaffold command to create categories is done by simply giving the command :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails generate scaffold category name:string&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
As seen, this generates the model called category, a bunch of views, and a category controller.&lt;br /&gt;
It also generates a migration, to create a new table called categories (notice that rails called the table as categories, which is the plural of category automatically).&lt;br /&gt;
===DB Migration===&lt;br /&gt;
The scaffold command generates a lot of files, and also a migration in db\migrate with a name like &amp;lt;timestamp&amp;gt;_create_categories.rb. This migration contains the details of the table that has to be created called 'categories', so the next step is to run the migrations. Before we do this, however, if we start the rails server, we get an error mentioning that the table 'categories' doesn't exist. &lt;br /&gt;
Let us now run the migration using the command :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After the migration is run, we know that the categories table has been created, and now run the server.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails server&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
===Creating Categories===&lt;br /&gt;
Now, if we goto the default localhost:3000 page, we see one option : Create a new category, as shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:2.jpg]]&lt;br /&gt;
&lt;br /&gt;
We can click on it, and make a new category, lets say 'Main Course'&lt;br /&gt;
&lt;br /&gt;
[[File:3.jpg]]&lt;br /&gt;
&lt;br /&gt;
When clicking on a new category notice that the URL is localhost:3000/categories/new. So we are executing the new method of the categories controller. On clicking 'Create Category', a new category with the name 'Main Course' is created, and a flash notice saying 'Successfully Create' is shown. We can now see the category in our list of categories.&lt;br /&gt;
&lt;br /&gt;
[[File:5.jpg]]&lt;br /&gt;
&lt;br /&gt;
So, as we saw, with just a simple scaffold command and db:migrate, we have an application with the basic CRUD functionality.&lt;br /&gt;
===Creating Recipes===&lt;br /&gt;
Now, similar to creating categories, we create recipes using the scaffold command. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails generate scaffold recipe title:string description:string instructions:text category:references&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The category:references creates a line 'belongs_to :category' in the Recipe model. The Recipe model now looks like :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Once this is done, we again have to run a db:migrate, so that the recipes table is in the database.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, the recipes have been created, and as seen in the recipe model, each recipe belongs to a category.&lt;br /&gt;
&lt;br /&gt;
==Relationships==&lt;br /&gt;
Relationships are always defined in the model. When we created recipes by scaffolding, the 'category:references' made sure that the Recipes model has the 'belongs_to' relationship with Category. Similarly, we want to add a detail mentioning that each Category can have many Recipes : a one to many relationship. To do this, we need to edit the Category model, and include the 'has_many' relationship with Recipe. The Category model now looks like :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
  has_many :recipes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Valdations==&lt;br /&gt;
Validations, just like Relationships, are also defined in the model. Suppose we want to make sure that each recipe entered has a valid title and instructions, and the condition being that they cannot be empty. We can add this to the Recipe model, which now looks like :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :category&lt;br /&gt;
  validates :title, :presence =&amp;gt; true&lt;br /&gt;
  validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
  validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Thus, the title, instructions and category are all validated, and it makes sure that title and instructions are not empty, and that the category it belongs also exists.&lt;br /&gt;
&lt;br /&gt;
==Routes.rb==&lt;br /&gt;
The routes.rb file is located in the config folder. routes.rb, as the name suggests is used to route to the corresponding controller's method. Suppose we have defined a new method in the Category controller. Now, if we want to route a link to the Category controller's new method from some recipe view, we have to include this in the routes.rb file. If we don't, we get an error saying 'No route matches ...'. Whenever we decide to add a new method to a controller, we have to add something similar to :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
match 'Controller_name/new_method_name' =&amp;gt; 'Controller_name#new_method_name'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Our application starts are localhost:3000\categories. Suppose we want to have the root localhost:3000 to the index method in the category controller, we can do:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to =&amp;gt; 'categories#index'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This has to be done after deleting the index.html file from the public directory.&lt;br /&gt;
&lt;br /&gt;
==Views &amp;amp; Layouts==&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
[1] Ruby on Rails Guides: Getting Started With Rails :http://guides.rubyonrails.org/getting_started.html&lt;br /&gt;
&lt;br /&gt;
[2] CookBookApp : http://courses.ncsu.edu/csc517/common/exercises/cookbook3/&lt;br /&gt;
&lt;br /&gt;
[3] CSC 517 Fall 2011 : Engineering Online Video : http://mediasite.online.ncsu.edu/online/Viewer/?peid=5a5b4de717dc4108a1b04133575b22181d&lt;br /&gt;
&lt;br /&gt;
[4] Course Schedule, and Lecture #9 Materials : http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures&lt;br /&gt;
&lt;br /&gt;
[5] Ruby on Rails official site : http://rubyonrails.org/&lt;br /&gt;
&lt;br /&gt;
[6] Sam Ruby, Agile Web Development with Rails, ISBN:1934356549&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=52878</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4d mt</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=52878"/>
		<updated>2011-10-20T03:26:42Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''' The CookBook Application (Lecture 9)'''&lt;br /&gt;
=Introduction=&lt;br /&gt;
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.Rails includes a simple light weight application server [http://en.wikipedia.org/wiki/WEBrick Webrick] and a build system [http://en.wikipedia.org/wiki/Rake_(software) Rake] which makes development on rails easy.&lt;br /&gt;
&lt;br /&gt;
==Ruby on Rails==&lt;br /&gt;
Ruby on Rails, is an [http://en.wikipedia.org/wiki/Web_application_framework open source web application framework] for the [http://en.wikipedia.org/wiki/Ruby_(programming_language) 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:&lt;br /&gt;
===Convention over Configuration===&lt;br /&gt;
[http://en.wikipedia.org/wiki/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.&lt;br /&gt;
&lt;br /&gt;
===Don't repeat yourself=== &lt;br /&gt;
[http://en.wikipedia.org/wiki/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. Also in ActiveRecord there is no need to specify attribute names since it is picked up from the database tables itself.&lt;br /&gt;
&lt;br /&gt;
==Model/View/Controller Framework==&lt;br /&gt;
All Rails applications are implemented using the [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller 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.&lt;br /&gt;
Key components of MVC architecture include:&lt;br /&gt;
# 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.&lt;br /&gt;
#Views: Views provide a human interface(GUI) for the model. Views in Rails are implemented using embedded ruby (.html.erb) files&lt;br /&gt;
#Controllers: Controllers process the user actions and help redirect to the appropriate model ,build a user response and update the View. &lt;br /&gt;
The typical user interaction on a Rails application happens as follows:&lt;br /&gt;
#The user responds to the UI (ex : clicks a button)&lt;br /&gt;
#The router routes the action to the appropriate controller and action in it&lt;br /&gt;
#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&lt;br /&gt;
#A view queries the model and uses its data to build a response .&lt;br /&gt;
#The view updates the UI with the view and waits for further user interaction.&lt;br /&gt;
&lt;br /&gt;
==CRUD==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Create,_read,_update_and_delete 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:&lt;br /&gt;
#Create or add new entries&lt;br /&gt;
#Read, retrieve, search, or view existing entries&lt;br /&gt;
#Update or edit existing entries&lt;br /&gt;
#Delete existing entries&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
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 &amp;quot;contact management&amp;quot; or &amp;quot;contact maintenance&amp;quot; (or &amp;quot;document management&amp;quot; in general, depending on the basic storage unit for the particular application).&lt;br /&gt;
&lt;br /&gt;
==Routing in Rails==&lt;br /&gt;
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: &lt;br /&gt;
resources :photos &lt;br /&gt;
can allow us to specify 7 routes for the application.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! HTTP Verb&lt;br /&gt;
! Path &lt;br /&gt;
! action &lt;br /&gt;
! used for&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos || index || display a list of all photos&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos/new || new  || return an HTML form for creating a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  POST   ||	/photos	||create||	create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	||/photos/new ||	new ||	return an HTML form for creating a new photo &lt;br /&gt;
|-&lt;br /&gt;
|  POST	|| /photos	|| create	|| create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id	|| show	|| display a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id/edit	|| edit	|| return an HTML form for editing a photo&lt;br /&gt;
|-&lt;br /&gt;
|  PUT	|| /photos/:id ||	update	|| update a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  DELETE	|| /photos/:id	|| destroy	|| delete a specific photo&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
resources :photos&lt;br /&gt;
creates seven different routes in your application, all mapping to the Photos controller:&lt;br /&gt;
&lt;br /&gt;
==Object relational Mapping==&lt;br /&gt;
Rails handles Object Relational mapping using Active Records. ActiveRecords allow a Rails program to deal with objects and itself handles the mapping of these objects to relational databases. In Active Record, &lt;br /&gt;
# Database tables correspond to Rails classes&lt;br /&gt;
# Database records (rows) correspond to Rails objects.&lt;br /&gt;
ActiveRecord also allow us to query the database in the object domain by using the Active Record Query Interface&lt;br /&gt;
&lt;br /&gt;
=Developing the Cookbook App=&lt;br /&gt;
We now will look into how to develop the cookbook application, and talk about how to get started with the same. &lt;br /&gt;
To get started, we first create a new rails project called say 'CookBookApp', which is done by simply running the command : &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails new CookBookApp&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Once this is run, a series of folders are created, like app, config, db, etc. In developing the cookbook app, we will mainly look into the app, config and db folders mainly.&lt;br /&gt;
==Scaffolding==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Scaffold_%28programming%29 Scaffolding] is a very powerful command that creates code to create, read, update and delete (CRUD) data from the database. In the Cookbook application, the first scaffold is to create categories. Each Category will then have multiple recipes, and the main functionality of our cookbook app would be done.&lt;br /&gt;
The scaffold command to create categories is done by simply giving the command :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails generate scaffold category name:string&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
As seen, this generates the model called category, a bunch of views, and a category controller.&lt;br /&gt;
It also generates a migration, to create a new table called categories (notice that rails called the table as categories, which is the plural of category automatically).&lt;br /&gt;
===DB Migration===&lt;br /&gt;
The scaffold command generates a lot of files, and also a migration in db\migrate with a name like &amp;lt;timestamp&amp;gt;_create_categories.rb. This migration contains the details of the table that has to be created called 'categories', so the next step is to run the migrations. Before we do this, however, if we start the rails server, we get an error mentioning that the table 'categories' doesn't exist. &lt;br /&gt;
Let us now run the migration using the command :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After the migration is run, we know that the categories table has been created, and now run the server.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails server&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
===Creating Categories===&lt;br /&gt;
Now, if we goto the default localhost:3000 page, we see one option : Create a new category, as shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:2.jpg]]&lt;br /&gt;
&lt;br /&gt;
We can click on it, and make a new category, lets say 'Main Course'&lt;br /&gt;
&lt;br /&gt;
[[File:3.jpg]]&lt;br /&gt;
&lt;br /&gt;
When clicking on a new category notice that the URL is localhost:3000/categories/new. So we are executing the new method of the categories controller. On clicking 'Create Category', a new category with the name 'Main Course' is created, and a flash notice saying 'Successfully Create' is shown. We can now see the category in our list of categories.&lt;br /&gt;
&lt;br /&gt;
[[File:5.jpg]]&lt;br /&gt;
&lt;br /&gt;
So, as we saw, with just a simple scaffold command and db:migrate, we have an application with the basic CRUD functionality.&lt;br /&gt;
===Creating Recipes===&lt;br /&gt;
Now, similar to creating categories, we create recipes using the scaffold command. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails generate scaffold recipe title:string description:string instructions:text category:references&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The category:references creates a line 'belongs_to :category' in the Recipe model. The Recipe model now looks like :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Once this is done, we again have to run a db:migrate, so that the recipes table is in the database.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, the recipes have been created, and as seen in the recipe model, each recipe belongs to a category.&lt;br /&gt;
&lt;br /&gt;
==Relationships==&lt;br /&gt;
Relationships are always defined in the model. When we created recipes by scaffolding, the 'category:references' made sure that the Recipes model has the 'belongs_to' relationship with Category. Similarly, we want to add a detail mentioning that each Category can have many Recipes : a one to many relationship. To do this, we need to edit the Category model, and include the 'has_many' relationship with Recipe. The Category model now looks like :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
  has_many :recipes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Valdations==&lt;br /&gt;
Validations, just like Relationships, are also defined in the model. Suppose we want to make sure that each recipe entered has a valid title and instructions, and the condition being that they cannot be empty. We can add this to the Recipe model, which now looks like :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :category&lt;br /&gt;
  validates :title, :presence =&amp;gt; true&lt;br /&gt;
  validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
  validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Thus, the title, instructions and category are all validated, and it makes sure that title and instructions are not empty, and that the category it belongs also exists.&lt;br /&gt;
&lt;br /&gt;
==Routes.rb==&lt;br /&gt;
The routes.rb file is located in the config folder. routes.rb, as the name suggests is used to route to the corresponding controller's method. Suppose we have defined a new method in the Category controller. Now, if we want to route a link to the Category controller's new method from some recipe view, we have to include this in the routes.rb file. If we don't, we get an error saying 'No route matches ...'. Whenever we decide to add a new method to a controller, we have to add something similar to :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
match 'Controller_name/new_method_name' =&amp;gt; 'Controller_name#new_method_name'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Our application starts are localhost:3000\categories. Suppose we want to have the root localhost:3000 to the index method in the category controller, we can do:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to =&amp;gt; 'categories#index'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This has to be done after deleting the index.html file from the public directory.&lt;br /&gt;
&lt;br /&gt;
==Views &amp;amp; Layouts==&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
[1] Ruby on Rails Guides: Getting Started With Rails :http://guides.rubyonrails.org/getting_started.html&lt;br /&gt;
&lt;br /&gt;
[2] CookBookApp : http://courses.ncsu.edu/csc517/common/exercises/cookbook3/&lt;br /&gt;
&lt;br /&gt;
[3] CSC 517 Fall 2011 : Engineering Online Video : http://mediasite.online.ncsu.edu/online/Viewer/?peid=5a5b4de717dc4108a1b04133575b22181d&lt;br /&gt;
&lt;br /&gt;
[4] Course Schedule, and Lecture #9 Materials : http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures&lt;br /&gt;
&lt;br /&gt;
[5] Ruby on Rails official site : http://rubyonrails.org/&lt;br /&gt;
&lt;br /&gt;
[6] Sam Ruby, Agile Web Development with Rails ISBN: 1934356549&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=52876</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4d mt</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=52876"/>
		<updated>2011-10-20T03:17:41Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''' The CookBook Application (Lecture 9)'''&lt;br /&gt;
=Introduction=&lt;br /&gt;
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.Rails includes a simple light weight application server [http://en.wikipedia.org/wiki/WEBrick Webrick] and a build system [http://en.wikipedia.org/wiki/Rake_(software) Rake] which makes development on rails easy.&lt;br /&gt;
&lt;br /&gt;
==Ruby on Rails==&lt;br /&gt;
Ruby on Rails, is an [http://en.wikipedia.org/wiki/Web_application_framework open source web application framework] for the [http://en.wikipedia.org/wiki/Ruby_(programming_language) 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:&lt;br /&gt;
===Convention over Configuration===&lt;br /&gt;
[http://en.wikipedia.org/wiki/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.&lt;br /&gt;
&lt;br /&gt;
===Don't repeat yourself=== &lt;br /&gt;
[http://en.wikipedia.org/wiki/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. Also in ActiveRecord there is no need to specify attribute names since it is picked up from the database tables itself.&lt;br /&gt;
&lt;br /&gt;
==Model/View/Controller Framework==&lt;br /&gt;
All Rails applications are implemented using the [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller 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.&lt;br /&gt;
Key components of MVC architecture include:&lt;br /&gt;
# 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.&lt;br /&gt;
#Views: Views provide a human interface(GUI) for the model. Views in Rails are implemented using embedded ruby (.html.erb) files&lt;br /&gt;
#Controllers: Controllers process the user actions and help redirect to the appropriate model ,build a user response and update the View. &lt;br /&gt;
The typical user interaction on a Rails application happens as follows:&lt;br /&gt;
#The user responds to the UI (ex : clicks a button)&lt;br /&gt;
#The router routes the action to the appropriate controller and action in it&lt;br /&gt;
#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&lt;br /&gt;
#A view queries the model and uses its data to build a response .&lt;br /&gt;
#The view updates the UI with the view and waits for further user interaction.&lt;br /&gt;
&lt;br /&gt;
==CRUD==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Create,_read,_update_and_delete 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:&lt;br /&gt;
#Create or add new entries&lt;br /&gt;
#Read, retrieve, search, or view existing entries&lt;br /&gt;
#Update or edit existing entries&lt;br /&gt;
#Delete existing entries&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
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 &amp;quot;contact management&amp;quot; or &amp;quot;contact maintenance&amp;quot; (or &amp;quot;document management&amp;quot; in general, depending on the basic storage unit for the particular application).&lt;br /&gt;
&lt;br /&gt;
==Routing in Rails==&lt;br /&gt;
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: &lt;br /&gt;
resources :photos &lt;br /&gt;
can allow us to specify 7 routes for the application.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! HTTP Verb&lt;br /&gt;
! Path &lt;br /&gt;
! action &lt;br /&gt;
! used for&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos || index || display a list of all photos&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos/new || new  || return an HTML form for creating a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  POST   ||	/photos	||create||	create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	||/photos/new ||	new ||	return an HTML form for creating a new photo &lt;br /&gt;
|-&lt;br /&gt;
|  POST	|| /photos	|| create	|| create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id	|| show	|| display a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id/edit	|| edit	|| return an HTML form for editing a photo&lt;br /&gt;
|-&lt;br /&gt;
|  PUT	|| /photos/:id ||	update	|| update a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  DELETE	|| /photos/:id	|| destroy	|| delete a specific photo&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
resources :photos&lt;br /&gt;
creates seven different routes in your application, all mapping to the Photos controller:&lt;br /&gt;
&lt;br /&gt;
==Object relational Mapping==&lt;br /&gt;
Rails handles Object Relational mapping using Active Records. ActiveRecords allow a Rails program to deal with objects and itself handles the mapping of these objects to relational databases. In Active Record, &lt;br /&gt;
# Database tables correspond to Rails classes&lt;br /&gt;
# Database records (rows) correspond to Rails objects.&lt;br /&gt;
ActiveRecord also allow us to query the database in the object domain by using the Active Record Query Interface&lt;br /&gt;
&lt;br /&gt;
=Developing the Cookbook App=&lt;br /&gt;
We now will look into how to develop the cookbook application, and talk about how to get started with the same. &lt;br /&gt;
To get started, we first create a new rails project called say 'CookBookApp', which is done by simply running the command : &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails new CookBookApp&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Once this is run, a series of folders are created, like app, config, db, etc. In developing the cookbook app, we will mainly look into the app, config and db folders mainly.&lt;br /&gt;
==Scaffolding==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Scaffold_%28programming%29 Scaffolding] is a very powerful command that creates code to create, read, update and delete (CRUD) data from the database. In the Cookbook application, the first scaffold is to create categories. Each Category will then have multiple recipes, and the main functionality of our cookbook app would be done.&lt;br /&gt;
The scaffold command to create categories is done by simply giving the command :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails generate scaffold category name:string&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
As seen, this generates the model called category, a bunch of views, and a category controller.&lt;br /&gt;
It also generates a migration, to create a new table called categories (notice that rails called the table as categories, which is the plural of category automatically).&lt;br /&gt;
===DB Migration===&lt;br /&gt;
The scaffold command generates a lot of files, and also a migration in db\migrate with a name like &amp;lt;timestamp&amp;gt;_create_categories.rb. This migration contains the details of the table that has to be created called 'categories', so the next step is to run the migrations. Before we do this, however, if we start the rails server, we get an error mentioning that the table 'categories' doesn't exist. &lt;br /&gt;
Let us now run the migration using the command :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After the migration is run, we know that the categories table has been created, and now run the server.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails server&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
===Creating Categories===&lt;br /&gt;
Now, if we goto the default localhost:3000 page, we see one option : Create a new category, as shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:2.jpg]]&lt;br /&gt;
&lt;br /&gt;
We can click on it, and make a new category, lets say 'Main Course'&lt;br /&gt;
&lt;br /&gt;
[[File:3.jpg]]&lt;br /&gt;
&lt;br /&gt;
When clicking on a new category notice that the URL is localhost:3000/categories/new. So we are executing the new method of the categories controller. On clicking 'Create Category', a new category with the name 'Main Course' is created, and a flash notice saying 'Successfully Create' is shown. We can now see the category in our list of categories.&lt;br /&gt;
&lt;br /&gt;
[[File:5.jpg]]&lt;br /&gt;
&lt;br /&gt;
So, as we saw, with just a simple scaffold command and db:migrate, we have an application with the basic CRUD functionality.&lt;br /&gt;
===Creating Recipes===&lt;br /&gt;
Now, similar to creating categories, we create recipes using the scaffold command. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails generate scaffold recipe title:string description:string instructions:text category:references&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The category:references creates a line 'belongs_to :category' in the Recipe model. The Recipe model now looks like :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Once this is done, we again have to run a db:migrate, so that the recipes table is in the database.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rake db:migrate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, the recipes have been created, and as seen in the recipe model, each recipe belongs to a category.&lt;br /&gt;
&lt;br /&gt;
==Relationships==&lt;br /&gt;
Relationships are always defined in the model. When we created recipes by scaffolding, the 'category:references' made sure that the Recipes model has the 'belongs_to' relationship with Category. Similarly, we want to add a detail mentioning that each Category can have many Recipes : a one to many relationship. To do this, we need to edit the Category model, and include the 'has_many' relationship with Recipe. The Category model now looks like :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;
  has_many :recipes&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Valdations==&lt;br /&gt;
Validations, just like Relationships, are also defined in the model. Suppose we want to make sure that each recipe entered has a valid title and instructions, and the condition being that they cannot be empty. We can add this to the Recipe model, which now looks like :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :category&lt;br /&gt;
  validates :title, :presence =&amp;gt; true&lt;br /&gt;
  validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
  validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Thus, the title, instructions and category are all validated, and it makes sure that title and instructions are not empty, and that the category it belongs also exists.&lt;br /&gt;
&lt;br /&gt;
==Routes.rb==&lt;br /&gt;
The routes.rb file is located in the config folder. routes.rb, as the name suggests is used to route to the corresponding controller's method. Suppose we have defined a new method in the Category controller. Now, if we want to route a link to the Category controller's new method from some recipe view, we have to include this in the routes.rb file. If we don't, we get an error saying 'No route matches ...'. Whenever we decide to add a new method to a controller, we have to add something similar to :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
match 'Controller_name/new_method_name' =&amp;gt; 'Controller_name#new_method_name'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Our application starts are localhost:3000\categories. Suppose we want to have the root localhost:3000 to the index method in the category controller, we can do:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to =&amp;gt; 'categories#index'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This has to be done after deleting the index.html file from the public directory.&lt;br /&gt;
&lt;br /&gt;
==Views &amp;amp; Layouts==&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
[1] Ruby on Rails Guides: Getting Started With Rails :http://guides.rubyonrails.org/getting_started.html&lt;br /&gt;
&lt;br /&gt;
[2] CookBookApp : http://courses.ncsu.edu/csc517/common/exercises/cookbook3/&lt;br /&gt;
&lt;br /&gt;
[3] CSC 517 Fall 2011 : Engineering Online Video : http://mediasite.online.ncsu.edu/online/Viewer/?peid=5a5b4de717dc4108a1b04133575b22181d&lt;br /&gt;
&lt;br /&gt;
[4] Course Schedule, and Lecture #9 Materials : http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures&lt;br /&gt;
&lt;br /&gt;
[5] Ruby on Rails official site : http://rubyonrails.org/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=52000</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4d mt</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=52000"/>
		<updated>2011-10-15T04:09:22Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''' The CookBook Application (Lecture 9)'''&lt;br /&gt;
=Introduction=&lt;br /&gt;
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.Rails includes a simple light weight application server [http://en.wikipedia.org/wiki/WEBrick Webrick] and a build system [http://en.wikipedia.org/wiki/Rake_(software) Rake] which makes development on rails easy.&lt;br /&gt;
&lt;br /&gt;
==Ruby on Rails==&lt;br /&gt;
Ruby on Rails, is an [http://en.wikipedia.org/wiki/Web_application_framework open source web application framework] for the [http://en.wikipedia.org/wiki/Ruby_(programming_language) 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:&lt;br /&gt;
===Convention over Configuration===&lt;br /&gt;
[http://en.wikipedia.org/wiki/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.&lt;br /&gt;
&lt;br /&gt;
===Don't repeat yourself=== &lt;br /&gt;
[http://en.wikipedia.org/wiki/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. Also in ActiveRecord there is no need to specify attribute names since it is picked up from the database tables itself.&lt;br /&gt;
&lt;br /&gt;
==Model/View/Controller Framework==&lt;br /&gt;
All Rails applications are implemented using the [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller 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.&lt;br /&gt;
Key components of MVC architecture include:&lt;br /&gt;
# 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.&lt;br /&gt;
#Views: Views provide a human interface(GUI) for the model. Views in Rails are implemented using embedded ruby (.html.erb) files&lt;br /&gt;
#Controllers: Controllers process the user actions and help redirect to the appropriate model ,build a user response and update the View. &lt;br /&gt;
The typical user interaction on a Rails application happens as follows:&lt;br /&gt;
#The user responds to the UI (ex : clicks a button)&lt;br /&gt;
#The router routes the action to the appropriate controller and action in it&lt;br /&gt;
#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&lt;br /&gt;
#A view queries the model and uses its data to build a response .&lt;br /&gt;
#The view updates the UI with the view and waits for further user interaction.&lt;br /&gt;
&lt;br /&gt;
==CRUD==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Create,_read,_update_and_delete 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:&lt;br /&gt;
#Create or add new entries&lt;br /&gt;
#Read, retrieve, search, or view existing entries&lt;br /&gt;
#Update or edit existing entries&lt;br /&gt;
#Delete existing entries&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
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 &amp;quot;contact management&amp;quot; or &amp;quot;contact maintenance&amp;quot; (or &amp;quot;document management&amp;quot; in general, depending on the basic storage unit for the particular application).&lt;br /&gt;
&lt;br /&gt;
==Routing in Rails==&lt;br /&gt;
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: &lt;br /&gt;
resources :photos &lt;br /&gt;
can allow us to specify 7 routes for the application.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! HTTP Verb&lt;br /&gt;
! Path &lt;br /&gt;
! action &lt;br /&gt;
! used for&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos || index || display a list of all photos&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos/new || new  || return an HTML form for creating a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  POST   ||	/photos	||create||	create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	||/photos/new ||	new ||	return an HTML form for creating a new photo &lt;br /&gt;
|-&lt;br /&gt;
|  POST	|| /photos	|| create	|| create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id	|| show	|| display a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id/edit	|| edit	|| return an HTML form for editing a photo&lt;br /&gt;
|-&lt;br /&gt;
|  PUT	|| /photos/:id ||	update	|| update a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  DELETE	|| /photos/:id	|| destroy	|| delete a specific photo&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
resources :photos&lt;br /&gt;
creates seven different routes in your application, all mapping to the Photos controller:&lt;br /&gt;
&lt;br /&gt;
==Object relational Mapping==&lt;br /&gt;
Rails handles Object Relational mapping using Active Records. ActiveRecords allow a Rails program to deal with objects and itself handles the mapping of these objects to relational databases. In Active Record, &lt;br /&gt;
# Database tables correspond to Rails classes&lt;br /&gt;
# Database records (rows) correspond to Rails objects.&lt;br /&gt;
ActiveRecord also allow us to query the database in the object domain by using the Active Record Query Interface&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=51999</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4d mt</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=51999"/>
		<updated>2011-10-15T04:08:58Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: /* Object relational Mapping */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''' The CookBook Application (Lecture 9)'''&lt;br /&gt;
=Introduction=&lt;br /&gt;
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.Rails includes a simple light weight application server [http://en.wikipedia.org/wiki/WEBrick Webrick] and a build system [http://en.wikipedia.org/wiki/Rake_(software) Rake] which makes development on rails easy.&lt;br /&gt;
&lt;br /&gt;
==Ruby on Rails==&lt;br /&gt;
Ruby on Rails, is an [http://en.wikipedia.org/wiki/Web_application_framework open source web application framework] for the [http://en.wikipedia.org/wiki/Ruby_(programming_language) 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:&lt;br /&gt;
===Convention over Configuration===&lt;br /&gt;
[http://en.wikipedia.org/wiki/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.&lt;br /&gt;
&lt;br /&gt;
===Don't repeat yourself=== &lt;br /&gt;
[http://en.wikipedia.org/wiki/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. Also in ActiveRecord there is no need to specify attribute names since it is picked up from the database tables itself.&lt;br /&gt;
&lt;br /&gt;
==Model/View/Controller Framework==&lt;br /&gt;
All Rails applications are implemented using the [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller 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.&lt;br /&gt;
Key components of MVC architecture include:&lt;br /&gt;
# 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.&lt;br /&gt;
#Views: Views provide a human interface(GUI) for the model. Views in Rails are implemented using embedded ruby (.html.erb) files&lt;br /&gt;
#Controllers: Controllers process the user actions and help redirect to the appropriate model ,build a user response and update the View. &lt;br /&gt;
The typical user interaction on a Rails application happens as follows:&lt;br /&gt;
#The user responds to the UI (ex : clicks a button)&lt;br /&gt;
#The router routes the action to the appropriate controller and action in it&lt;br /&gt;
#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&lt;br /&gt;
#A view queries the model and uses its data to build a response .&lt;br /&gt;
#The view updates the UI with the view and waits for further user interaction.&lt;br /&gt;
&lt;br /&gt;
==CRUD==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Create,_read,_update_and_delete 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:&lt;br /&gt;
#Create or add new entries&lt;br /&gt;
#Read, retrieve, search, or view existing entries&lt;br /&gt;
#Update or edit existing entries&lt;br /&gt;
#Delete existing entries&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
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 &amp;quot;contact management&amp;quot; or &amp;quot;contact maintenance&amp;quot; (or &amp;quot;document management&amp;quot; in general, depending on the basic storage unit for the particular application).&lt;br /&gt;
&lt;br /&gt;
==Routing in Rails==&lt;br /&gt;
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: &lt;br /&gt;
resources :photos &lt;br /&gt;
can allow us to specify 7 routes for the application.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! HTTP Verb&lt;br /&gt;
! Path &lt;br /&gt;
! action &lt;br /&gt;
! used for&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos || index || display a list of all photos&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos/new || new  || return an HTML form for creating a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  POST   ||	/photos	||create||	create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	||/photos/new ||	new ||	return an HTML form for creating a new photo &lt;br /&gt;
|-&lt;br /&gt;
|  POST	|| /photos	|| create	|| create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id	|| show	|| display a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id/edit	|| edit	|| return an HTML form for editing a photo&lt;br /&gt;
|-&lt;br /&gt;
|  PUT	|| /photos/:id ||	update	|| update a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  DELETE	|| /photos/:id	|| destroy	|| delete a specific photo&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
resources :photos&lt;br /&gt;
creates seven different routes in your application, all mapping to the Photos controller:&lt;br /&gt;
&lt;br /&gt;
==Object relational Mapping==&lt;br /&gt;
Rails handles Object Relational mapping using Active Records. ActiveRecords allow a Rails program to deal with objects and itself handles the mapping of these objects to relational databases. In Active Record, &lt;br /&gt;
# Database tables correspond to Rails classes&lt;br /&gt;
# Database records (rows) correspond to Rails objects.&lt;br /&gt;
ActiveRecord also allow us to query the database in the object domain by using the Active Record Query Interface&lt;br /&gt;
&lt;br /&gt;
='''References'''=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=51998</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4d mt</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=51998"/>
		<updated>2011-10-15T04:06:27Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: /* Don't repeat yourself */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''' The CookBook Application (Lecture 9)'''&lt;br /&gt;
=Introduction=&lt;br /&gt;
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.Rails includes a simple light weight application server [http://en.wikipedia.org/wiki/WEBrick Webrick] and a build system [http://en.wikipedia.org/wiki/Rake_(software) Rake] which makes development on rails easy.&lt;br /&gt;
&lt;br /&gt;
==Ruby on Rails==&lt;br /&gt;
Ruby on Rails, is an [http://en.wikipedia.org/wiki/Web_application_framework open source web application framework] for the [http://en.wikipedia.org/wiki/Ruby_(programming_language) 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:&lt;br /&gt;
===Convention over Configuration===&lt;br /&gt;
[http://en.wikipedia.org/wiki/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.&lt;br /&gt;
&lt;br /&gt;
===Don't repeat yourself=== &lt;br /&gt;
[http://en.wikipedia.org/wiki/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. Also in ActiveRecord there is no need to specify attribute names since it is picked up from the database tables itself.&lt;br /&gt;
&lt;br /&gt;
==Model/View/Controller Framework==&lt;br /&gt;
All Rails applications are implemented using the [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller 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.&lt;br /&gt;
Key components of MVC architecture include:&lt;br /&gt;
# 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.&lt;br /&gt;
#Views: Views provide a human interface(GUI) for the model. Views in Rails are implemented using embedded ruby (.html.erb) files&lt;br /&gt;
#Controllers: Controllers process the user actions and help redirect to the appropriate model ,build a user response and update the View. &lt;br /&gt;
The typical user interaction on a Rails application happens as follows:&lt;br /&gt;
#The user responds to the UI (ex : clicks a button)&lt;br /&gt;
#The router routes the action to the appropriate controller and action in it&lt;br /&gt;
#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&lt;br /&gt;
#A view queries the model and uses its data to build a response .&lt;br /&gt;
#The view updates the UI with the view and waits for further user interaction.&lt;br /&gt;
&lt;br /&gt;
==CRUD==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Create,_read,_update_and_delete 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:&lt;br /&gt;
#Create or add new entries&lt;br /&gt;
#Read, retrieve, search, or view existing entries&lt;br /&gt;
#Update or edit existing entries&lt;br /&gt;
#Delete existing entries&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
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 &amp;quot;contact management&amp;quot; or &amp;quot;contact maintenance&amp;quot; (or &amp;quot;document management&amp;quot; in general, depending on the basic storage unit for the particular application).&lt;br /&gt;
&lt;br /&gt;
==Routing in Rails==&lt;br /&gt;
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: &lt;br /&gt;
resources :photos &lt;br /&gt;
can allow us to specify 7 routes for the application.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! HTTP Verb&lt;br /&gt;
! Path &lt;br /&gt;
! action &lt;br /&gt;
! used for&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos || index || display a list of all photos&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos/new || new  || return an HTML form for creating a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  POST   ||	/photos	||create||	create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	||/photos/new ||	new ||	return an HTML form for creating a new photo &lt;br /&gt;
|-&lt;br /&gt;
|  POST	|| /photos	|| create	|| create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id	|| show	|| display a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id/edit	|| edit	|| return an HTML form for editing a photo&lt;br /&gt;
|-&lt;br /&gt;
|  PUT	|| /photos/:id ||	update	|| update a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  DELETE	|| /photos/:id	|| destroy	|| delete a specific photo&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
resources :photos&lt;br /&gt;
creates seven different routes in your application, all mapping to the Photos controller:&lt;br /&gt;
&lt;br /&gt;
==Object relational Mapping==&lt;br /&gt;
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, &lt;br /&gt;
# Database tables correspond to Rails classes&lt;br /&gt;
# Database records (rows) correspond to Rails objects.&lt;br /&gt;
ActiveRecord also allow us to query the database in the object domain by using the Active Record Query Interface&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
='''References'''=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=51997</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4d mt</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=51997"/>
		<updated>2011-10-15T04:04:20Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: /* Convention over Configuration */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''' The CookBook Application (Lecture 9)'''&lt;br /&gt;
=Introduction=&lt;br /&gt;
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.Rails includes a simple light weight application server [http://en.wikipedia.org/wiki/WEBrick Webrick] and a build system [http://en.wikipedia.org/wiki/Rake_(software) Rake] which makes development on rails easy.&lt;br /&gt;
&lt;br /&gt;
==Ruby on Rails==&lt;br /&gt;
Ruby on Rails, is an [http://en.wikipedia.org/wiki/Web_application_framework open source web application framework] for the [http://en.wikipedia.org/wiki/Ruby_(programming_language) 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:&lt;br /&gt;
===Convention over Configuration===&lt;br /&gt;
[http://en.wikipedia.org/wiki/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.&lt;br /&gt;
&lt;br /&gt;
===Don't repeat yourself=== &lt;br /&gt;
[http://en.wikipedia.org/wiki/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&lt;br /&gt;
&lt;br /&gt;
==Model/View/Controller Framework==&lt;br /&gt;
All Rails applications are implemented using the [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller 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.&lt;br /&gt;
Key components of MVC architecture include:&lt;br /&gt;
# 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.&lt;br /&gt;
#Views: Views provide a human interface(GUI) for the model. Views in Rails are implemented using embedded ruby (.html.erb) files&lt;br /&gt;
#Controllers: Controllers process the user actions and help redirect to the appropriate model ,build a user response and update the View. &lt;br /&gt;
The typical user interaction on a Rails application happens as follows:&lt;br /&gt;
#The user responds to the UI (ex : clicks a button)&lt;br /&gt;
#The router routes the action to the appropriate controller and action in it&lt;br /&gt;
#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&lt;br /&gt;
#A view queries the model and uses its data to build a response .&lt;br /&gt;
#The view updates the UI with the view and waits for further user interaction.&lt;br /&gt;
&lt;br /&gt;
==CRUD==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Create,_read,_update_and_delete 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:&lt;br /&gt;
#Create or add new entries&lt;br /&gt;
#Read, retrieve, search, or view existing entries&lt;br /&gt;
#Update or edit existing entries&lt;br /&gt;
#Delete existing entries&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
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 &amp;quot;contact management&amp;quot; or &amp;quot;contact maintenance&amp;quot; (or &amp;quot;document management&amp;quot; in general, depending on the basic storage unit for the particular application).&lt;br /&gt;
&lt;br /&gt;
==Routing in Rails==&lt;br /&gt;
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: &lt;br /&gt;
resources :photos &lt;br /&gt;
can allow us to specify 7 routes for the application.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! HTTP Verb&lt;br /&gt;
! Path &lt;br /&gt;
! action &lt;br /&gt;
! used for&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos || index || display a list of all photos&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos/new || new  || return an HTML form for creating a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  POST   ||	/photos	||create||	create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	||/photos/new ||	new ||	return an HTML form for creating a new photo &lt;br /&gt;
|-&lt;br /&gt;
|  POST	|| /photos	|| create	|| create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id	|| show	|| display a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id/edit	|| edit	|| return an HTML form for editing a photo&lt;br /&gt;
|-&lt;br /&gt;
|  PUT	|| /photos/:id ||	update	|| update a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  DELETE	|| /photos/:id	|| destroy	|| delete a specific photo&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
resources :photos&lt;br /&gt;
creates seven different routes in your application, all mapping to the Photos controller:&lt;br /&gt;
&lt;br /&gt;
==Object relational Mapping==&lt;br /&gt;
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, &lt;br /&gt;
# Database tables correspond to Rails classes&lt;br /&gt;
# Database records (rows) correspond to Rails objects.&lt;br /&gt;
ActiveRecord also allow us to query the database in the object domain by using the Active Record Query Interface&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
='''References'''=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=51996</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4d mt</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=51996"/>
		<updated>2011-10-15T04:03:58Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''' The CookBook Application (Lecture 9)'''&lt;br /&gt;
=Introduction=&lt;br /&gt;
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.Rails includes a simple light weight application server [http://en.wikipedia.org/wiki/WEBrick Webrick] and a build system [http://en.wikipedia.org/wiki/Rake_(software) Rake] which makes development on rails easy.&lt;br /&gt;
&lt;br /&gt;
==Ruby on Rails==&lt;br /&gt;
Ruby on Rails, is an [http://en.wikipedia.org/wiki/Web_application_framework open source web application framework] for the [http://en.wikipedia.org/wiki/Ruby_(programming_language) 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:&lt;br /&gt;
===Convention over Configuration===&lt;br /&gt;
[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.&lt;br /&gt;
&lt;br /&gt;
===Don't repeat yourself=== &lt;br /&gt;
[http://en.wikipedia.org/wiki/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&lt;br /&gt;
&lt;br /&gt;
==Model/View/Controller Framework==&lt;br /&gt;
All Rails applications are implemented using the [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller 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.&lt;br /&gt;
Key components of MVC architecture include:&lt;br /&gt;
# 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.&lt;br /&gt;
#Views: Views provide a human interface(GUI) for the model. Views in Rails are implemented using embedded ruby (.html.erb) files&lt;br /&gt;
#Controllers: Controllers process the user actions and help redirect to the appropriate model ,build a user response and update the View. &lt;br /&gt;
The typical user interaction on a Rails application happens as follows:&lt;br /&gt;
#The user responds to the UI (ex : clicks a button)&lt;br /&gt;
#The router routes the action to the appropriate controller and action in it&lt;br /&gt;
#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&lt;br /&gt;
#A view queries the model and uses its data to build a response .&lt;br /&gt;
#The view updates the UI with the view and waits for further user interaction.&lt;br /&gt;
&lt;br /&gt;
==CRUD==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Create,_read,_update_and_delete 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:&lt;br /&gt;
#Create or add new entries&lt;br /&gt;
#Read, retrieve, search, or view existing entries&lt;br /&gt;
#Update or edit existing entries&lt;br /&gt;
#Delete existing entries&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
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 &amp;quot;contact management&amp;quot; or &amp;quot;contact maintenance&amp;quot; (or &amp;quot;document management&amp;quot; in general, depending on the basic storage unit for the particular application).&lt;br /&gt;
&lt;br /&gt;
==Routing in Rails==&lt;br /&gt;
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: &lt;br /&gt;
resources :photos &lt;br /&gt;
can allow us to specify 7 routes for the application.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! HTTP Verb&lt;br /&gt;
! Path &lt;br /&gt;
! action &lt;br /&gt;
! used for&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos || index || display a list of all photos&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos/new || new  || return an HTML form for creating a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  POST   ||	/photos	||create||	create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	||/photos/new ||	new ||	return an HTML form for creating a new photo &lt;br /&gt;
|-&lt;br /&gt;
|  POST	|| /photos	|| create	|| create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id	|| show	|| display a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id/edit	|| edit	|| return an HTML form for editing a photo&lt;br /&gt;
|-&lt;br /&gt;
|  PUT	|| /photos/:id ||	update	|| update a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  DELETE	|| /photos/:id	|| destroy	|| delete a specific photo&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
resources :photos&lt;br /&gt;
creates seven different routes in your application, all mapping to the Photos controller:&lt;br /&gt;
&lt;br /&gt;
==Object relational Mapping==&lt;br /&gt;
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, &lt;br /&gt;
# Database tables correspond to Rails classes&lt;br /&gt;
# Database records (rows) correspond to Rails objects.&lt;br /&gt;
ActiveRecord also allow us to query the database in the object domain by using the Active Record Query Interface&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
='''References'''=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=51995</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4d mt</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=51995"/>
		<updated>2011-10-15T04:03:03Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''' The CookBook Application (Lecture 9)'''&lt;br /&gt;
=Introduction=&lt;br /&gt;
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.Rails includes a simple light weight application server [http://en.wikipedia.org/wiki/WEBrick Webrick] and a build system [http://en.wikipedia.org/wiki/Rake_(software) Rake] which makes development on rails easy.&lt;br /&gt;
&lt;br /&gt;
==Ruby on Rails==&lt;br /&gt;
Ruby on Rails, is an [http://en.wikipedia.org/wiki/Web_application_framework open source web application framework] for the [http://en.wikipedia.org/wiki/Ruby_(programming_language) 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:&lt;br /&gt;
===Convention over Configuration===&lt;br /&gt;
[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.&lt;br /&gt;
&lt;br /&gt;
===Don't repeat yourself=== &lt;br /&gt;
[http://en.wikipedia.org/wiki/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&lt;br /&gt;
&lt;br /&gt;
==Model/View/Controller Framework==&lt;br /&gt;
All Rails applications are implemented using the [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller 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.&lt;br /&gt;
Key components of MVC architecture include:&lt;br /&gt;
# 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.&lt;br /&gt;
#Views: Views provide a human interface(GUI) for the model. Views in Rails are implemented using embedded ruby (.html.erb) files&lt;br /&gt;
#Controllers: Controllers process the user actions and help redirect to the appropriate model ,build a user response and update the View. &lt;br /&gt;
The typical user interaction on a Rails application happens as follows:&lt;br /&gt;
#The user responds to the UI (ex : clicks a button)&lt;br /&gt;
#The router routes the action to the appropriate controller and action in it&lt;br /&gt;
#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&lt;br /&gt;
#A view queries the model and uses its data to build a response .&lt;br /&gt;
#The view updates the UI with the view and waits for further user interaction.&lt;br /&gt;
&lt;br /&gt;
==CRUD==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Create,_read,_update_and_delete 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:&lt;br /&gt;
#Create or add new entries&lt;br /&gt;
#Read, retrieve, search, or view existing entries&lt;br /&gt;
#Update or edit existing entries&lt;br /&gt;
#Delete existing entries&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
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 &amp;quot;contact management&amp;quot; or &amp;quot;contact maintenance&amp;quot; (or &amp;quot;document management&amp;quot; in general, depending on the basic storage unit for the particular application).&lt;br /&gt;
&lt;br /&gt;
==Routing in Rails==&lt;br /&gt;
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: &lt;br /&gt;
resources :photos &lt;br /&gt;
can allow us to specify 7 routes for the application.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! HTTP Verb&lt;br /&gt;
! Path &lt;br /&gt;
! action &lt;br /&gt;
! used for&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos || index || display a list of all photos&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos/new || new  || return an HTML form for creating a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  POST   ||	/photos	||create||	create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	||/photos/new ||	new ||	return an HTML form for creating a new photo &lt;br /&gt;
|-&lt;br /&gt;
|  POST	|| /photos	|| create	|| create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id	|| show	|| display a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id/edit	|| edit	|| return an HTML form for editing a photo&lt;br /&gt;
|-&lt;br /&gt;
|  PUT	|| /photos/:id ||	update	|| update a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  DELETE	|| /photos/:id	|| destroy	|| delete a specific photo&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
resources :photos&lt;br /&gt;
creates seven different routes in your application, all mapping to the Photos controller:&lt;br /&gt;
&lt;br /&gt;
==Object relational Mapping==&lt;br /&gt;
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, &lt;br /&gt;
# Database tables correspond to Rails classes&lt;br /&gt;
# Database records (rows) correspond to Rails objects.&lt;br /&gt;
ActiveRecord also allow us to query the database in the object domain by using the Active Record Query Interface&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=51991</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4d mt</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=51991"/>
		<updated>2011-10-15T03:56:37Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''' The CookBook Application (Lecture 9)'''&lt;br /&gt;
=Introduction=&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
==Ruby on Rails==&lt;br /&gt;
Ruby on Rails, is an [http://en.wikipedia.org/wiki/Web_application_framework open source web application framework] for the [http://en.wikipedia.org/wiki/Ruby_(programming_language) 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:&lt;br /&gt;
===Convention over Configuration===&lt;br /&gt;
[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.&lt;br /&gt;
&lt;br /&gt;
===Don't repeat yourself=== &lt;br /&gt;
[http://en.wikipedia.org/wiki/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&lt;br /&gt;
&lt;br /&gt;
==Model/View/Controller Framework==&lt;br /&gt;
All Rails applications are implemented using the [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller 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.&lt;br /&gt;
Key components of MVC architecture include:&lt;br /&gt;
# 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.&lt;br /&gt;
#Views: Views provide a human interface(GUI) for the model. Views in Rails are implemented using embedded ruby (.html.erb) files&lt;br /&gt;
#Controllers: Controllers process the user actions and help redirect to the appropriate model ,build a user response and update the View. &lt;br /&gt;
The typical user interaction on a Rails application happens as follows:&lt;br /&gt;
#The user responds to the UI (ex : clicks a button)&lt;br /&gt;
#The router routes the action to the appropriate controller and action in it&lt;br /&gt;
#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&lt;br /&gt;
#A view queries the model and uses its data to build a response .&lt;br /&gt;
#The view updates the UI with the view and waits for further user interaction.&lt;br /&gt;
&lt;br /&gt;
==CRUD==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Create,_read,_update_and_delete 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:&lt;br /&gt;
#Create or add new entries&lt;br /&gt;
#Read, retrieve, search, or view existing entries&lt;br /&gt;
#Update or edit existing entries&lt;br /&gt;
#Delete existing entries&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
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 &amp;quot;contact management&amp;quot; or &amp;quot;contact maintenance&amp;quot; (or &amp;quot;document management&amp;quot; in general, depending on the basic storage unit for the particular application).&lt;br /&gt;
&lt;br /&gt;
==Routing in Rails==&lt;br /&gt;
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: &lt;br /&gt;
resources :photos &lt;br /&gt;
can allow us to specify 7 routes for the application.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! HTTP Verb&lt;br /&gt;
! Path &lt;br /&gt;
! action &lt;br /&gt;
! used for&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos || index || display a list of all photos&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos/new || new  || return an HTML form for creating a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  POST   ||	/photos	||create||	create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	||/photos/new ||	new ||	return an HTML form for creating a new photo &lt;br /&gt;
|-&lt;br /&gt;
|  POST	|| /photos	|| create	|| create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id	|| show	|| display a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id/edit	|| edit	|| return an HTML form for editing a photo&lt;br /&gt;
|-&lt;br /&gt;
|  PUT	|| /photos/:id ||	update	|| update a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  DELETE	|| /photos/:id	|| destroy	|| delete a specific photo&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
resources :photos&lt;br /&gt;
creates seven different routes in your application, all mapping to the Photos controller:&lt;br /&gt;
&lt;br /&gt;
==Object relational Mapping==&lt;br /&gt;
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, &lt;br /&gt;
# Database tables correspond to Rails classes&lt;br /&gt;
# Database records (rows) correspond to Rails objects.&lt;br /&gt;
ActiveRecord also allow us to query the database in the object domain by using the Active Record Query Interface&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=51989</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4d mt</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=51989"/>
		<updated>2011-10-15T03:56:11Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''' The CookBook Application (Lecture 9)'''&lt;br /&gt;
=Introduction=&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
==Ruby on Rails==&lt;br /&gt;
Ruby on Rails, is an [http://en.wikipedia.org/wiki/Web_application_framework open source web application framework] for the [http://en.wikipedia.org/wiki/Ruby_(programming_language) 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:&lt;br /&gt;
===Convention over Configuration===&lt;br /&gt;
	[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.&lt;br /&gt;
&lt;br /&gt;
===Don't repeat yourself=== &lt;br /&gt;
	[http://en.wikipedia.org/wiki/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&lt;br /&gt;
&lt;br /&gt;
==Model/View/Controller Framework==&lt;br /&gt;
All Rails applications are implemented using the [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller 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.&lt;br /&gt;
Key components of MVC architecture include:&lt;br /&gt;
# 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.&lt;br /&gt;
#Views: Views provide a human interface(GUI) for the model. Views in Rails are implemented using embedded ruby (.html.erb) files&lt;br /&gt;
#Controllers: Controllers process the user actions and help redirect to the appropriate model ,build a user response and update the View. &lt;br /&gt;
The typical user interaction on a Rails application happens as follows:&lt;br /&gt;
#The user responds to the UI (ex : clicks a button)&lt;br /&gt;
#The router routes the action to the appropriate controller and action in it&lt;br /&gt;
#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&lt;br /&gt;
#A view queries the model and uses its data to build a response .&lt;br /&gt;
#The view updates the UI with the view and waits for further user interaction.&lt;br /&gt;
&lt;br /&gt;
==CRUD==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Create,_read,_update_and_delete 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:&lt;br /&gt;
#Create or add new entries&lt;br /&gt;
#Read, retrieve, search, or view existing entries&lt;br /&gt;
#Update or edit existing entries&lt;br /&gt;
#Delete existing entries&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
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 &amp;quot;contact management&amp;quot; or &amp;quot;contact maintenance&amp;quot; (or &amp;quot;document management&amp;quot; in general, depending on the basic storage unit for the particular application).&lt;br /&gt;
&lt;br /&gt;
==Routing in Rails==&lt;br /&gt;
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: &lt;br /&gt;
resources :photos &lt;br /&gt;
can allow us to specify 7 routes for the application.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! HTTP Verb&lt;br /&gt;
! Path &lt;br /&gt;
! action &lt;br /&gt;
! used for&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos || index || display a list of all photos&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos/new || new  || return an HTML form for creating a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  POST   ||	/photos	||create||	create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	||/photos/new ||	new ||	return an HTML form for creating a new photo &lt;br /&gt;
|-&lt;br /&gt;
|  POST	|| /photos	|| create	|| create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id	|| show	|| display a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id/edit	|| edit	|| return an HTML form for editing a photo&lt;br /&gt;
|-&lt;br /&gt;
|  PUT	|| /photos/:id ||	update	|| update a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  DELETE	|| /photos/:id	|| destroy	|| delete a specific photo&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
resources :photos&lt;br /&gt;
creates seven different routes in your application, all mapping to the Photos controller:&lt;br /&gt;
&lt;br /&gt;
==Object relational Mapping==&lt;br /&gt;
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, &lt;br /&gt;
# Database tables correspond to Rails classes&lt;br /&gt;
# Database records (rows) correspond to Rails objects.&lt;br /&gt;
ActiveRecord also allow us to query the database in the object domain by using the Active Record Query Interface&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=51985</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4d mt</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=51985"/>
		<updated>2011-10-15T03:53:35Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: /* Routing in Rails */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''' The CookBook Application (Lecture 9)'''&lt;br /&gt;
==Introduction==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
Ruby on Rails, is an [http://en.wikipedia.org/wiki/Web_application_framework open source web application framework] for the [http://en.wikipedia.org/wiki/Ruby_(programming_language) 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:&lt;br /&gt;
====Convention over Configuration====&lt;br /&gt;
	[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.&lt;br /&gt;
&lt;br /&gt;
====Don't repeat yourself==== &lt;br /&gt;
	[http://en.wikipedia.org/wiki/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&lt;br /&gt;
&lt;br /&gt;
===Model/View/Controller Framework===&lt;br /&gt;
All Rails applications are implemented using the [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller 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.&lt;br /&gt;
Key components of MVC architecture include:&lt;br /&gt;
# 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.&lt;br /&gt;
#Views: Views provide a human interface(GUI) for the model. Views in Rails are implemented using embedded ruby (.html.erb) files&lt;br /&gt;
#Controllers: Controllers process the user actions and help redirect to the appropriate model ,build a user response and update the View. &lt;br /&gt;
The typical user interaction on a Rails application happens as follows:&lt;br /&gt;
#The user responds to the UI (ex : clicks a button)&lt;br /&gt;
#The router routes the action to the appropriate controller and action in it&lt;br /&gt;
#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&lt;br /&gt;
#A view queries the model and uses its data to build a response .&lt;br /&gt;
#The view updates the UI with the view and waits for further user interaction.&lt;br /&gt;
&lt;br /&gt;
===CRUD===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Create,_read,_update_and_delete 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:&lt;br /&gt;
#Create or add new entries&lt;br /&gt;
#Read, retrieve, search, or view existing entries&lt;br /&gt;
#Update or edit existing entries&lt;br /&gt;
#Delete existing entries&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
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 &amp;quot;contact management&amp;quot; or &amp;quot;contact maintenance&amp;quot; (or &amp;quot;document management&amp;quot; in general, depending on the basic storage unit for the particular application).&lt;br /&gt;
&lt;br /&gt;
===Routing in Rails===&lt;br /&gt;
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: &lt;br /&gt;
resources :photos &lt;br /&gt;
can allow us to specify 7 routes for the application.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! HTTP Verb&lt;br /&gt;
! Path &lt;br /&gt;
! action &lt;br /&gt;
! used for&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos || index || display a list of all photos&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos/new || new  || return an HTML form for creating a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  POST   ||	/photos	||create||	create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	||/photos/new ||	new ||	return an HTML form for creating a new photo &lt;br /&gt;
|-&lt;br /&gt;
|  POST	|| /photos	|| create	|| create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id	|| show	|| display a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id/edit	|| edit	|| return an HTML form for editing a photo&lt;br /&gt;
|-&lt;br /&gt;
|  PUT	|| /photos/:id ||	update	|| update a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  DELETE	|| /photos/:id	|| destroy	|| delete a specific photo&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
resources :photos&lt;br /&gt;
creates seven different routes in your application, all mapping to the Photos controller:&lt;br /&gt;
&lt;br /&gt;
===Object relational Mapping===&lt;br /&gt;
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, &lt;br /&gt;
# Database tables correspond to Rails classes&lt;br /&gt;
# Database records (rows) correspond to Rails objects.&lt;br /&gt;
ActiveRecord also allow us to query the database in the object domain by using the Active Record Query Interface&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=51983</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4d mt</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=51983"/>
		<updated>2011-10-15T03:52:14Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: /* Routing in Rails */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''' The CookBook Application (Lecture 9)'''&lt;br /&gt;
==Introduction==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Ruby on Rails==&lt;br /&gt;
Ruby on Rails, is an [http://en.wikipedia.org/wiki/Web_application_framework open source web application framework] for the [http://en.wikipedia.org/wiki/Ruby_(programming_language) 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:&lt;br /&gt;
===Convention over Configuration===&lt;br /&gt;
	[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.&lt;br /&gt;
&lt;br /&gt;
===Don't repeat yourself=== &lt;br /&gt;
	[http://en.wikipedia.org/wiki/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&lt;br /&gt;
&lt;br /&gt;
==Model/View/Controller Framework ==&lt;br /&gt;
All Rails applications are implemented using the [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller 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.&lt;br /&gt;
Key components of MVC architecture include:&lt;br /&gt;
# 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.&lt;br /&gt;
#Views: Views provide a human interface(GUI) for the model. Views in Rails are implemented using embedded ruby (.html.erb) files&lt;br /&gt;
#Controllers: Controllers process the user actions and help redirect to the appropriate model ,build a user response and update the View. &lt;br /&gt;
The typical user interaction on a Rails application happens as follows:&lt;br /&gt;
#The user responds to the UI (ex : clicks a button)&lt;br /&gt;
#The router routes the action to the appropriate controller and action in it&lt;br /&gt;
#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&lt;br /&gt;
#A view queries the model and uses its data to build a response .&lt;br /&gt;
#The view updates the UI with the view and waits for further user interaction.&lt;br /&gt;
&lt;br /&gt;
==CRUD==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Create,_read,_update_and_delete 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:&lt;br /&gt;
#Create or add new entries&lt;br /&gt;
#Read, retrieve, search, or view existing entries&lt;br /&gt;
#Update or edit existing entries&lt;br /&gt;
#Delete existing entries&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
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 &amp;quot;contact management&amp;quot; or &amp;quot;contact maintenance&amp;quot; (or &amp;quot;document management&amp;quot; in general, depending on the basic storage unit for the particular application).&lt;br /&gt;
&lt;br /&gt;
==Routing in Rails==&lt;br /&gt;
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: &lt;br /&gt;
resources :photos &lt;br /&gt;
can allow us to specify 7 routes for the application.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! HTTP Verb&lt;br /&gt;
! Path &lt;br /&gt;
! action &lt;br /&gt;
! used for&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos || index || display a list of all photos&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos/new || new  || return an HTML form for creating a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  POST   ||	/photos	||create||	create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	||/photos/new ||	new ||	return an HTML form for creating a new photo &lt;br /&gt;
|-&lt;br /&gt;
|  POST	|| /photos	|| create	|| create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id	|| show	|| display a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id/edit	|| edit	|| return an HTML form for editing a photo&lt;br /&gt;
|-&lt;br /&gt;
|  PUT	|| /photos/:id ||	update	|| update a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  DELETE	|| /photos/:id	|| destroy	|| delete a specific photo&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
resources :photos&lt;br /&gt;
creates seven different routes in your application, all mapping to the Photos controller:&lt;br /&gt;
&lt;br /&gt;
==Object relational Mapping==&lt;br /&gt;
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, &lt;br /&gt;
# Database tables correspond to Rails classes&lt;br /&gt;
# Database records (rows) correspond to Rails objects.&lt;br /&gt;
ActiveRecord also allow us to query the database in the object domain by using the Active Record Query INterface&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=51982</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4d mt</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=51982"/>
		<updated>2011-10-15T03:51:31Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: /* Routing in Rails */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''' The CookBook Application (Lecture 9)'''&lt;br /&gt;
==Introduction==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Ruby on Rails==&lt;br /&gt;
Ruby on Rails, is an [http://en.wikipedia.org/wiki/Web_application_framework open source web application framework] for the [http://en.wikipedia.org/wiki/Ruby_(programming_language) 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:&lt;br /&gt;
===Convention over Configuration===&lt;br /&gt;
	[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.&lt;br /&gt;
&lt;br /&gt;
===Don't repeat yourself=== &lt;br /&gt;
	[http://en.wikipedia.org/wiki/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&lt;br /&gt;
&lt;br /&gt;
==Model/View/Controller Framework ==&lt;br /&gt;
All Rails applications are implemented using the [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller 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.&lt;br /&gt;
Key components of MVC architecture include:&lt;br /&gt;
# 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.&lt;br /&gt;
#Views: Views provide a human interface(GUI) for the model. Views in Rails are implemented using embedded ruby (.html.erb) files&lt;br /&gt;
#Controllers: Controllers process the user actions and help redirect to the appropriate model ,build a user response and update the View. &lt;br /&gt;
The typical user interaction on a Rails application happens as follows:&lt;br /&gt;
#The user responds to the UI (ex : clicks a button)&lt;br /&gt;
#The router routes the action to the appropriate controller and action in it&lt;br /&gt;
#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&lt;br /&gt;
#A view queries the model and uses its data to build a response .&lt;br /&gt;
#The view updates the UI with the view and waits for further user interaction.&lt;br /&gt;
&lt;br /&gt;
==CRUD==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Create,_read,_update_and_delete 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:&lt;br /&gt;
#Create or add new entries&lt;br /&gt;
#Read, retrieve, search, or view existing entries&lt;br /&gt;
#Update or edit existing entries&lt;br /&gt;
#Delete existing entries&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
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 &amp;quot;contact management&amp;quot; or &amp;quot;contact maintenance&amp;quot; (or &amp;quot;document management&amp;quot; in general, depending on the basic storage unit for the particular application).&lt;br /&gt;
&lt;br /&gt;
==Routing in Rails==&lt;br /&gt;
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: &lt;br /&gt;
resources :photos &lt;br /&gt;
can allow us to specify 7 routes for the application.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! HTTP Verb&lt;br /&gt;
! Path &lt;br /&gt;
! action &lt;br /&gt;
! used for&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos || index || display a list of all photos&lt;br /&gt;
|-&lt;br /&gt;
|  GET   || /photos/new || new  || return an HTML form for creating a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  POST   ||	/photos	||create||	create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	||/photos/new ||	new ||	return an HTML form for creating a new photo &lt;br /&gt;
|-&lt;br /&gt;
|  POST	|| /photos	|| create	|| create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id	|| show	|| display a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|| /photos/:id/edit	|| edit	|| return an HTML form for editing a photo&lt;br /&gt;
|-&lt;br /&gt;
|  PUT	|| /photos/:id ||	update	|| update a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  DELETE	|| /photos/:id	|| destroy	|| delete a specific photo|}&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
resources :photos&lt;br /&gt;
creates seven different routes in your application, all mapping to the Photos controller:&lt;br /&gt;
&lt;br /&gt;
==Object relational Mapping==&lt;br /&gt;
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, &lt;br /&gt;
# Database tables correspond to Rails classes&lt;br /&gt;
# Database records (rows) correspond to Rails objects.&lt;br /&gt;
ActiveRecord also allow us to query the database in the object domain by using the Active Record Query INterface&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=51978</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4d mt</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=51978"/>
		<updated>2011-10-15T03:49:07Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: /* [Model/View/Controller Framework http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller ] */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''' The CookBook Application (Lecture 9)'''&lt;br /&gt;
==Introduction==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Ruby on Rails==&lt;br /&gt;
Ruby on Rails, is an [http://en.wikipedia.org/wiki/Web_application_framework open source web application framework] for the [http://en.wikipedia.org/wiki/Ruby_(programming_language) 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:&lt;br /&gt;
===Convention over Configuration===&lt;br /&gt;
	[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.&lt;br /&gt;
&lt;br /&gt;
===Don't repeat yourself=== &lt;br /&gt;
	[http://en.wikipedia.org/wiki/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&lt;br /&gt;
&lt;br /&gt;
==Model/View/Controller Framework ==&lt;br /&gt;
All Rails applications are implemented using the [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller 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.&lt;br /&gt;
Key components of MVC architecture include:&lt;br /&gt;
# 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.&lt;br /&gt;
#Views: Views provide a human interface(GUI) for the model. Views in Rails are implemented using embedded ruby (.html.erb) files&lt;br /&gt;
#Controllers: Controllers process the user actions and help redirect to the appropriate model ,build a user response and update the View. &lt;br /&gt;
The typical user interaction on a Rails application happens as follows:&lt;br /&gt;
#The user responds to the UI (ex : clicks a button)&lt;br /&gt;
#The router routes the action to the appropriate controller and action in it&lt;br /&gt;
#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&lt;br /&gt;
#A view queries the model and uses its data to build a response .&lt;br /&gt;
#The view updates the UI with the view and waits for further user interaction.&lt;br /&gt;
&lt;br /&gt;
==CRUD==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Create,_read,_update_and_delete 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:&lt;br /&gt;
#Create or add new entries&lt;br /&gt;
#Read, retrieve, search, or view existing entries&lt;br /&gt;
#Update or edit existing entries&lt;br /&gt;
#Delete existing entries&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
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 &amp;quot;contact management&amp;quot; or &amp;quot;contact maintenance&amp;quot; (or &amp;quot;document management&amp;quot; in general, depending on the basic storage unit for the particular application).&lt;br /&gt;
&lt;br /&gt;
==Routing in Rails==&lt;br /&gt;
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: &lt;br /&gt;
resources :photos &lt;br /&gt;
can allow us to specify 7 routes for the application.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! HTTP Verb&lt;br /&gt;
! Path &lt;br /&gt;
! action &lt;br /&gt;
! used for&lt;br /&gt;
|-&lt;br /&gt;
|  GET   | /photos | index | display a list of all photos&lt;br /&gt;
|-&lt;br /&gt;
|  GET   | /photos/new | new  | return an HTML form for creating a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  POST   |	/photos	|create|	create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|/photos/new|	new|	return an HTML form for creating a new photo| &lt;br /&gt;
|-&lt;br /&gt;
|  POST	|/photos	|create	|create a new photo|&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|/photos/:id	|show	|display a specific photo|&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|/photos/:id/edit	|edit	|return an HTML form for editing a photo&lt;br /&gt;
|-&lt;br /&gt;
|  PUT	|/photos/:id|	update	|update a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  DELETE	|/photos/:id	|destroy	|delete a specific photo|}&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
resources :photos&lt;br /&gt;
creates seven different routes in your application, all mapping to the Photos controller:&lt;br /&gt;
&lt;br /&gt;
==Object relational Mapping==&lt;br /&gt;
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, &lt;br /&gt;
# Database tables correspond to Rails classes&lt;br /&gt;
# Database records (rows) correspond to Rails objects.&lt;br /&gt;
ActiveRecord also allow us to query the database in the object domain by using the Active Record Query INterface&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=51977</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4d mt</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=51977"/>
		<updated>2011-10-15T03:46:46Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: /* Model/View/Controller Framework */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''' The CookBook Application (Lecture 9)'''&lt;br /&gt;
==Introduction==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Ruby on Rails==&lt;br /&gt;
Ruby on Rails, is an [http://en.wikipedia.org/wiki/Web_application_framework open source web application framework] for the [http://en.wikipedia.org/wiki/Ruby_(programming_language) 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:&lt;br /&gt;
===Convention over Configuration===&lt;br /&gt;
	[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.&lt;br /&gt;
&lt;br /&gt;
===Don't repeat yourself=== &lt;br /&gt;
	[http://en.wikipedia.org/wiki/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&lt;br /&gt;
&lt;br /&gt;
==[Model/View/Controller Framework http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller ]==&lt;br /&gt;
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.&lt;br /&gt;
Key components of MVC architecture include:&lt;br /&gt;
# 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.&lt;br /&gt;
#Views: Views provide a human interface(GUI) for the model. Views in Rails are implemented using embedded ruby (.html.erb) files&lt;br /&gt;
#Controllers: Controllers process the user actions and help redirect to the appropriate model ,build a user response and update the View. &lt;br /&gt;
The typical user interaction on a Rails application happens as follows:&lt;br /&gt;
#The user responds to the UI (ex : clicks a button)&lt;br /&gt;
#The router routes the action to the appropriate controller and action in it&lt;br /&gt;
#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&lt;br /&gt;
#A view queries the model and uses its data to build a response .&lt;br /&gt;
#The view updates the UI with the view and waits for further user interaction.&lt;br /&gt;
&lt;br /&gt;
==CRUD==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Create,_read,_update_and_delete 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:&lt;br /&gt;
#Create or add new entries&lt;br /&gt;
#Read, retrieve, search, or view existing entries&lt;br /&gt;
#Update or edit existing entries&lt;br /&gt;
#Delete existing entries&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
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 &amp;quot;contact management&amp;quot; or &amp;quot;contact maintenance&amp;quot; (or &amp;quot;document management&amp;quot; in general, depending on the basic storage unit for the particular application).&lt;br /&gt;
&lt;br /&gt;
==Routing in Rails==&lt;br /&gt;
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: &lt;br /&gt;
resources :photos &lt;br /&gt;
can allow us to specify 7 routes for the application.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! HTTP Verb&lt;br /&gt;
! Path &lt;br /&gt;
! action &lt;br /&gt;
! used for&lt;br /&gt;
|-&lt;br /&gt;
|  GET   | /photos | index | display a list of all photos&lt;br /&gt;
|-&lt;br /&gt;
|  GET   | /photos/new | new  | return an HTML form for creating a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  POST   |	/photos	|create|	create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|/photos/new|	new|	return an HTML form for creating a new photo| &lt;br /&gt;
|-&lt;br /&gt;
|  POST	|/photos	|create	|create a new photo|&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|/photos/:id	|show	|display a specific photo|&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|/photos/:id/edit	|edit	|return an HTML form for editing a photo&lt;br /&gt;
|-&lt;br /&gt;
|  PUT	|/photos/:id|	update	|update a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  DELETE	|/photos/:id	|destroy	|delete a specific photo|}&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
resources :photos&lt;br /&gt;
creates seven different routes in your application, all mapping to the Photos controller:&lt;br /&gt;
&lt;br /&gt;
==Object relational Mapping==&lt;br /&gt;
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, &lt;br /&gt;
# Database tables correspond to Rails classes&lt;br /&gt;
# Database records (rows) correspond to Rails objects.&lt;br /&gt;
ActiveRecord also allow us to query the database in the object domain by using the Active Record Query INterface&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=51976</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4d mt</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=51976"/>
		<updated>2011-10-15T03:46:30Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: /* CRUD */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''' The CookBook Application (Lecture 9)'''&lt;br /&gt;
==Introduction==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Ruby on Rails==&lt;br /&gt;
Ruby on Rails, is an [http://en.wikipedia.org/wiki/Web_application_framework open source web application framework] for the [http://en.wikipedia.org/wiki/Ruby_(programming_language) 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:&lt;br /&gt;
===Convention over Configuration===&lt;br /&gt;
	[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.&lt;br /&gt;
&lt;br /&gt;
===Don't repeat yourself=== &lt;br /&gt;
	[http://en.wikipedia.org/wiki/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&lt;br /&gt;
&lt;br /&gt;
==[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model/View/Controller Framework]==&lt;br /&gt;
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.&lt;br /&gt;
Key components of MVC architecture include:&lt;br /&gt;
# 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.&lt;br /&gt;
#Views: Views provide a human interface(GUI) for the model. Views in Rails are implemented using embedded ruby (.html.erb) files&lt;br /&gt;
#Controllers: Controllers process the user actions and help redirect to the appropriate model ,build a user response and update the View. &lt;br /&gt;
The typical user interaction on a Rails application happens as follows:&lt;br /&gt;
#The user responds to the UI (ex : clicks a button)&lt;br /&gt;
#The router routes the action to the appropriate controller and action in it&lt;br /&gt;
#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&lt;br /&gt;
#A view queries the model and uses its data to build a response .&lt;br /&gt;
#The view updates the UI with the view and waits for further user interaction.&lt;br /&gt;
&lt;br /&gt;
==CRUD==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Create,_read,_update_and_delete 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:&lt;br /&gt;
#Create or add new entries&lt;br /&gt;
#Read, retrieve, search, or view existing entries&lt;br /&gt;
#Update or edit existing entries&lt;br /&gt;
#Delete existing entries&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
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 &amp;quot;contact management&amp;quot; or &amp;quot;contact maintenance&amp;quot; (or &amp;quot;document management&amp;quot; in general, depending on the basic storage unit for the particular application).&lt;br /&gt;
&lt;br /&gt;
==Routing in Rails==&lt;br /&gt;
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: &lt;br /&gt;
resources :photos &lt;br /&gt;
can allow us to specify 7 routes for the application.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! HTTP Verb&lt;br /&gt;
! Path &lt;br /&gt;
! action &lt;br /&gt;
! used for&lt;br /&gt;
|-&lt;br /&gt;
|  GET   | /photos | index | display a list of all photos&lt;br /&gt;
|-&lt;br /&gt;
|  GET   | /photos/new | new  | return an HTML form for creating a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  POST   |	/photos	|create|	create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|/photos/new|	new|	return an HTML form for creating a new photo| &lt;br /&gt;
|-&lt;br /&gt;
|  POST	|/photos	|create	|create a new photo|&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|/photos/:id	|show	|display a specific photo|&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|/photos/:id/edit	|edit	|return an HTML form for editing a photo&lt;br /&gt;
|-&lt;br /&gt;
|  PUT	|/photos/:id|	update	|update a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  DELETE	|/photos/:id	|destroy	|delete a specific photo|}&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
resources :photos&lt;br /&gt;
creates seven different routes in your application, all mapping to the Photos controller:&lt;br /&gt;
&lt;br /&gt;
==Object relational Mapping==&lt;br /&gt;
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, &lt;br /&gt;
# Database tables correspond to Rails classes&lt;br /&gt;
# Database records (rows) correspond to Rails objects.&lt;br /&gt;
ActiveRecord also allow us to query the database in the object domain by using the Active Record Query INterface&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=51975</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4d mt</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=51975"/>
		<updated>2011-10-15T03:31:15Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: /* Routing in Rails */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''' The CookBook Application (Lecture 9)'''&lt;br /&gt;
==Introduction==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Ruby on Rails==&lt;br /&gt;
Ruby on Rails, is an [http://en.wikipedia.org/wiki/Web_application_framework open source web application framework] for the [http://en.wikipedia.org/wiki/Ruby_(programming_language) 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:&lt;br /&gt;
===Convention over Configuration===&lt;br /&gt;
	[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.&lt;br /&gt;
&lt;br /&gt;
===Don't repeat yourself=== &lt;br /&gt;
	[http://en.wikipedia.org/wiki/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&lt;br /&gt;
&lt;br /&gt;
==[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model/View/Controller Framework]==&lt;br /&gt;
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.&lt;br /&gt;
Key components of MVC architecture include:&lt;br /&gt;
# 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.&lt;br /&gt;
#Views: Views provide a human interface(GUI) for the model. Views in Rails are implemented using embedded ruby (.html.erb) files&lt;br /&gt;
#Controllers: Controllers process the user actions and help redirect to the appropriate model ,build a user response and update the View. &lt;br /&gt;
The typical user interaction on a Rails application happens as follows:&lt;br /&gt;
#The user responds to the UI (ex : clicks a button)&lt;br /&gt;
#The router routes the action to the appropriate controller and action in it&lt;br /&gt;
#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&lt;br /&gt;
#A view queries the model and uses its data to build a response .&lt;br /&gt;
#The view updates the UI with the view and waits for further user interaction.&lt;br /&gt;
&lt;br /&gt;
==[http://en.wikipedia.org/wiki/Create,_read,_update_and_delete CRUD]==&lt;br /&gt;
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:&lt;br /&gt;
#Create or add new entries&lt;br /&gt;
#Read, retrieve, search, or view existing entries&lt;br /&gt;
#Update or edit existing entries&lt;br /&gt;
#Delete existing entries&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
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 &amp;quot;contact management&amp;quot; or &amp;quot;contact maintenance&amp;quot; (or &amp;quot;document management&amp;quot; in general, depending on the basic storage unit for the particular application).&lt;br /&gt;
&lt;br /&gt;
==Routing in Rails==&lt;br /&gt;
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: &lt;br /&gt;
resources :photos &lt;br /&gt;
can allow us to specify 7 routes for the application.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! HTTP Verb&lt;br /&gt;
! Path &lt;br /&gt;
! action &lt;br /&gt;
! used for&lt;br /&gt;
|-&lt;br /&gt;
|  GET   | /photos | index | display a list of all photos&lt;br /&gt;
|-&lt;br /&gt;
|  GET   | /photos/new | new  | return an HTML form for creating a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  POST   |	/photos	|create|	create a new photo&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|/photos/new|	new|	return an HTML form for creating a new photo| &lt;br /&gt;
|-&lt;br /&gt;
|  POST	|/photos	|create	|create a new photo|&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|/photos/:id	|show	|display a specific photo|&lt;br /&gt;
|-&lt;br /&gt;
|  GET	|/photos/:id/edit	|edit	|return an HTML form for editing a photo&lt;br /&gt;
|-&lt;br /&gt;
|  PUT	|/photos/:id|	update	|update a specific photo&lt;br /&gt;
|-&lt;br /&gt;
|  DELETE	|/photos/:id	|destroy	|delete a specific photo|}&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
resources :photos&lt;br /&gt;
creates seven different routes in your application, all mapping to the Photos controller:&lt;br /&gt;
&lt;br /&gt;
==Object relational Mapping==&lt;br /&gt;
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, &lt;br /&gt;
# Database tables correspond to Rails classes&lt;br /&gt;
# Database records (rows) correspond to Rails objects.&lt;br /&gt;
ActiveRecord also allow us to query the database in the object domain by using the Active Record Query INterface&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=51974</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4d mt</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=51974"/>
		<updated>2011-10-15T03:29:34Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: /* Ruby on Rails */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''' The CookBook Application (Lecture 9)'''&lt;br /&gt;
==Introduction==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Ruby on Rails==&lt;br /&gt;
Ruby on Rails, is an [http://en.wikipedia.org/wiki/Web_application_framework open source web application framework] for the [http://en.wikipedia.org/wiki/Ruby_(programming_language) 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:&lt;br /&gt;
===Convention over Configuration===&lt;br /&gt;
	[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.&lt;br /&gt;
&lt;br /&gt;
===Don't repeat yourself=== &lt;br /&gt;
	[http://en.wikipedia.org/wiki/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&lt;br /&gt;
&lt;br /&gt;
==[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model/View/Controller Framework]==&lt;br /&gt;
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.&lt;br /&gt;
Key components of MVC architecture include:&lt;br /&gt;
# 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.&lt;br /&gt;
#Views: Views provide a human interface(GUI) for the model. Views in Rails are implemented using embedded ruby (.html.erb) files&lt;br /&gt;
#Controllers: Controllers process the user actions and help redirect to the appropriate model ,build a user response and update the View. &lt;br /&gt;
The typical user interaction on a Rails application happens as follows:&lt;br /&gt;
#The user responds to the UI (ex : clicks a button)&lt;br /&gt;
#The router routes the action to the appropriate controller and action in it&lt;br /&gt;
#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&lt;br /&gt;
#A view queries the model and uses its data to build a response .&lt;br /&gt;
#The view updates the UI with the view and waits for further user interaction.&lt;br /&gt;
&lt;br /&gt;
==[http://en.wikipedia.org/wiki/Create,_read,_update_and_delete CRUD]==&lt;br /&gt;
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:&lt;br /&gt;
#Create or add new entries&lt;br /&gt;
#Read, retrieve, search, or view existing entries&lt;br /&gt;
#Update or edit existing entries&lt;br /&gt;
#Delete existing entries&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
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 &amp;quot;contact management&amp;quot; or &amp;quot;contact maintenance&amp;quot; (or &amp;quot;document management&amp;quot; in general, depending on the basic storage unit for the particular application).&lt;br /&gt;
&lt;br /&gt;
==Routing in Rails==&lt;br /&gt;
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: &lt;br /&gt;
resources :photos &lt;br /&gt;
can allow us to specify 7 routes for the application.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! HTTP Verb&lt;br /&gt;
! Path &lt;br /&gt;
! action &lt;br /&gt;
! used for&lt;br /&gt;
|-&lt;br /&gt;
|  GET   | /photos | index | display a list of all photos&lt;br /&gt;
|  GET   | /photos/new | new  | return an HTML form for creating a new photo&lt;br /&gt;
|  POST   |	/photos	|create|	create a new photo&lt;br /&gt;
|  GET	|/photos/new|	new|	return an HTML form for creating a new photo| &lt;br /&gt;
|  POST	|/photos	|create	|create a new photo|&lt;br /&gt;
|  GET	|/photos/:id	|show	|display a specific photo|&lt;br /&gt;
|  GET	|/photos/:id/edit	|edit	|return an HTML form for editing a photo&lt;br /&gt;
|  PUT	|/photos/:id|	update	|update a specific photo&lt;br /&gt;
|  DELETE	|/photos/:id	|destroy	|delete a specific photo|}&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
resources :photos&lt;br /&gt;
creates seven different routes in your application, all mapping to the Photos controller:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Object relational Mapping==&lt;br /&gt;
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, &lt;br /&gt;
# Database tables correspond to Rails classes&lt;br /&gt;
# Database records (rows) correspond to Rails objects.&lt;br /&gt;
ActiveRecord also allow us to query the database in the object domain by using the Active Record Query INterface&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=51973</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4d mt</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=51973"/>
		<updated>2011-10-15T03:24:51Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: /* Ruby on Rails */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''' The CookBook Application (Lecture 9)'''&lt;br /&gt;
==Introduction==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Ruby on Rails==&lt;br /&gt;
Ruby on Rails, is an [http://en.wikipedia.org/wiki/Web_application_framework open source web application framework] for the [http://en.wikipedia.org/wiki/Ruby_(programming_language) 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:&lt;br /&gt;
===[http://en.wikipedia.org/wiki/Convention_over_configuration Convention over Configuration]===&lt;br /&gt;
	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.&lt;br /&gt;
&lt;br /&gt;
===[http://en.wikipedia.org/wiki/Don't_repeat_yourself Don't repeat yourself]=== &lt;br /&gt;
	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&lt;br /&gt;
&lt;br /&gt;
==[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model/View/Controller Frame work]==&lt;br /&gt;
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.&lt;br /&gt;
Key components of MVC architecture include:&lt;br /&gt;
# 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.&lt;br /&gt;
#Views: Views provide a human interface(GUI) for the model. Views in Rails are implemented using embedded ruby (.html.erb) files&lt;br /&gt;
#Controllers: Controllers process the user actions and help redirect to the appropriate model ,build a user response and update the View. &lt;br /&gt;
The typical user interaction on a Rails application happens as follows:&lt;br /&gt;
#The user responds to the UI (ex : clicks a button)&lt;br /&gt;
#The router routes the action to the appropriate controller and action in it&lt;br /&gt;
#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&lt;br /&gt;
#A view queries the model and uses its data to build a response .&lt;br /&gt;
#The view updates the UI with the view and waits for further user interaction.&lt;br /&gt;
&lt;br /&gt;
==[ http://en.wikipedia.org/wiki/Create,_read,_update_and_delete CRUD]==&lt;br /&gt;
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:&lt;br /&gt;
#Create or add new entries&lt;br /&gt;
#Read, retrieve, search, or view existing entries&lt;br /&gt;
#Update or edit existing entries&lt;br /&gt;
#Delete existing entries&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
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 &amp;quot;contact management&amp;quot; or &amp;quot;contact maintenance&amp;quot; (or &amp;quot;document management&amp;quot; in general, depending on the basic storage unit for the particular application).&lt;br /&gt;
&lt;br /&gt;
==Routing in Rails==&lt;br /&gt;
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: &lt;br /&gt;
resources :photos &lt;br /&gt;
can allow us to specify 7 routes for the application.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! HTTP Verb&lt;br /&gt;
! Path &lt;br /&gt;
! action &lt;br /&gt;
! used for&lt;br /&gt;
|-&lt;br /&gt;
|  GET   | /photos | index | display a list of all photos&lt;br /&gt;
|  GET   | /photos/new | new  | return an HTML form for creating a new photo&lt;br /&gt;
|  POST   |	/photos	|create|	create a new photo&lt;br /&gt;
|  GET	|/photos/new|	new|	return an HTML form for creating a new photo| &lt;br /&gt;
|  POST	|/photos	|create	|create a new photo|&lt;br /&gt;
|  GET	|/photos/:id	|show	|display a specific photo|&lt;br /&gt;
|  GET	|/photos/:id/edit	|edit	|return an HTML form for editing a photo&lt;br /&gt;
|  PUT	|/photos/:id|	update	|update a specific photo&lt;br /&gt;
|  DELETE	|/photos/:id	|destroy	|delete a specific photo|}&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
resources :photos&lt;br /&gt;
creates seven different routes in your application, all mapping to the Photos controller:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Object relational Mapping==&lt;br /&gt;
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, &lt;br /&gt;
# Database tables correspond to Rails classes&lt;br /&gt;
# Database records (rows) correspond to Rails objects.&lt;br /&gt;
ActiveRecord also allow us to query the database in the object domain by using the Active Record Query INterface&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=51972</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4d mt</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=51972"/>
		<updated>2011-10-15T03:22:54Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: /* Ruby on Rails */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''' The CookBook Application (Lecture 9)'''&lt;br /&gt;
==Introduction==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Ruby on Rails==&lt;br /&gt;
Ruby on Rails, is an [http://en.wikipedia.org/wiki/Web_application_framework open source web application framework] for the [http://en.wikipedia.org/wiki/Ruby_(programming_language) 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:&lt;br /&gt;
===[http://en.wikipedia.org/wiki/Convention_over_configuration Convention over Configuration]===&lt;br /&gt;
	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.&lt;br /&gt;
&lt;br /&gt;
===[http://en.wikipedia.org/wiki/Don't_repeat_yourself Don't repeat yourself]=== &lt;br /&gt;
	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&lt;br /&gt;
&lt;br /&gt;
==[Model/View/Controller Frame work  http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller  ]==&lt;br /&gt;
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.&lt;br /&gt;
Key components of MVC architecture include:&lt;br /&gt;
# 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.&lt;br /&gt;
#Views: Views provide a human interface(GUI) for the model. Views in Rails are implemented using embedded ruby (.html.erb) files&lt;br /&gt;
#Controllers: Controllers process the user actions and help redirect to the appropriate model ,build a user response and update the View. &lt;br /&gt;
The typical user interaction on a Rails application happens as follows:&lt;br /&gt;
#The user responds to the UI (ex : clicks a button)&lt;br /&gt;
#The router routes the action to the appropriate controller and action in it&lt;br /&gt;
#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&lt;br /&gt;
#A view queries the model and uses its data to build a response .&lt;br /&gt;
#The view updates the UI with the view and waits for further user interaction.&lt;br /&gt;
&lt;br /&gt;
==[CRUD  http://en.wikipedia.org/wiki/Create,_read,_update_and_delete ]==&lt;br /&gt;
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:&lt;br /&gt;
#Create or add new entries&lt;br /&gt;
#Read, retrieve, search, or view existing entries&lt;br /&gt;
#Update or edit existing entries&lt;br /&gt;
#Delete existing entries&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
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 &amp;quot;contact management&amp;quot; or &amp;quot;contact maintenance&amp;quot; (or &amp;quot;document management&amp;quot; in general, depending on the basic storage unit for the particular application).&lt;br /&gt;
&lt;br /&gt;
==Routing in Rails==&lt;br /&gt;
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: &lt;br /&gt;
resources :photos &lt;br /&gt;
can allow us to specify 7 routes for the application.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! HTTP Verb&lt;br /&gt;
! Path &lt;br /&gt;
! action &lt;br /&gt;
! used for&lt;br /&gt;
|-&lt;br /&gt;
|  GET   | /photos | index | display a list of all photos&lt;br /&gt;
|  GET   | /photos/new | new  | return an HTML form for creating a new photo&lt;br /&gt;
|  POST   |	/photos	|create|	create a new photo&lt;br /&gt;
|  GET	|/photos/new|	new|	return an HTML form for creating a new photo| &lt;br /&gt;
|  POST	|/photos	|create	|create a new photo|&lt;br /&gt;
|  GET	|/photos/:id	|show	|display a specific photo|&lt;br /&gt;
|  GET	|/photos/:id/edit	|edit	|return an HTML form for editing a photo&lt;br /&gt;
|  PUT	|/photos/:id|	update	|update a specific photo&lt;br /&gt;
|  DELETE	|/photos/:id	|destroy	|delete a specific photo|}&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
resources :photos&lt;br /&gt;
creates seven different routes in your application, all mapping to the Photos controller:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Object relational Mapping==&lt;br /&gt;
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, &lt;br /&gt;
# Database tables correspond to Rails classes&lt;br /&gt;
# Database records (rows) correspond to Rails objects.&lt;br /&gt;
ActiveRecord also allow us to query the database in the object domain by using the Active Record Query INterface&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=51971</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4d mt</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=51971"/>
		<updated>2011-10-15T03:21:26Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: /* [Convention over Configuration  http://en.wikipedia.org/wiki/Convention_over_configuration  ] */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''' The CookBook Application (Lecture 9)'''&lt;br /&gt;
==Introduction==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Ruby on Rails==&lt;br /&gt;
Ruby on Rails, is an [http://en.wikipedia.org/wiki/Web_application_framework open source 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:&lt;br /&gt;
===[Convention over Configuration  http://en.wikipedia.org/wiki/Convention_over_configuration]===&lt;br /&gt;
	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.&lt;br /&gt;
&lt;br /&gt;
===[Don't repeat yourself  http://en.wikipedia.org/wiki/Don't_repeat_yourself  ]=== &lt;br /&gt;
	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&lt;br /&gt;
&lt;br /&gt;
==[Model/View/Controller Frame work  http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller  ]==&lt;br /&gt;
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.&lt;br /&gt;
Key components of MVC architecture include:&lt;br /&gt;
# 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.&lt;br /&gt;
#Views: Views provide a human interface(GUI) for the model. Views in Rails are implemented using embedded ruby (.html.erb) files&lt;br /&gt;
#Controllers: Controllers process the user actions and help redirect to the appropriate model ,build a user response and update the View. &lt;br /&gt;
The typical user interaction on a Rails application happens as follows:&lt;br /&gt;
#The user responds to the UI (ex : clicks a button)&lt;br /&gt;
#The router routes the action to the appropriate controller and action in it&lt;br /&gt;
#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&lt;br /&gt;
#A view queries the model and uses its data to build a response .&lt;br /&gt;
#The view updates the UI with the view and waits for further user interaction.&lt;br /&gt;
&lt;br /&gt;
==[CRUD  http://en.wikipedia.org/wiki/Create,_read,_update_and_delete ]==&lt;br /&gt;
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:&lt;br /&gt;
#Create or add new entries&lt;br /&gt;
#Read, retrieve, search, or view existing entries&lt;br /&gt;
#Update or edit existing entries&lt;br /&gt;
#Delete existing entries&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
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 &amp;quot;contact management&amp;quot; or &amp;quot;contact maintenance&amp;quot; (or &amp;quot;document management&amp;quot; in general, depending on the basic storage unit for the particular application).&lt;br /&gt;
&lt;br /&gt;
==Routing in Rails==&lt;br /&gt;
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: &lt;br /&gt;
resources :photos &lt;br /&gt;
can allow us to specify 7 routes for the application.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! HTTP Verb&lt;br /&gt;
! Path &lt;br /&gt;
! action &lt;br /&gt;
! used for&lt;br /&gt;
|-&lt;br /&gt;
|  GET   | /photos | index | display a list of all photos&lt;br /&gt;
|  GET   | /photos/new | new  | return an HTML form for creating a new photo&lt;br /&gt;
|  POST   |	/photos	|create|	create a new photo&lt;br /&gt;
|  GET	|/photos/new|	new|	return an HTML form for creating a new photo| &lt;br /&gt;
|  POST	|/photos	|create	|create a new photo|&lt;br /&gt;
|  GET	|/photos/:id	|show	|display a specific photo|&lt;br /&gt;
|  GET	|/photos/:id/edit	|edit	|return an HTML form for editing a photo&lt;br /&gt;
|  PUT	|/photos/:id|	update	|update a specific photo&lt;br /&gt;
|  DELETE	|/photos/:id	|destroy	|delete a specific photo|}&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
resources :photos&lt;br /&gt;
creates seven different routes in your application, all mapping to the Photos controller:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Object relational Mapping==&lt;br /&gt;
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, &lt;br /&gt;
# Database tables correspond to Rails classes&lt;br /&gt;
# Database records (rows) correspond to Rails objects.&lt;br /&gt;
ActiveRecord also allow us to query the database in the object domain by using the Active Record Query INterface&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=51970</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4d mt</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=51970"/>
		<updated>2011-10-15T03:21:00Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''' The CookBook Application (Lecture 9)'''&lt;br /&gt;
==Introduction==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Ruby on Rails==&lt;br /&gt;
Ruby on Rails, is an [http://en.wikipedia.org/wiki/Web_application_framework open source 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:&lt;br /&gt;
===[Convention over Configuration  http://en.wikipedia.org/wiki/Convention_over_configuration  ]===&lt;br /&gt;
	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.&lt;br /&gt;
===[Don't repeat yourself  http://en.wikipedia.org/wiki/Don't_repeat_yourself  ]=== &lt;br /&gt;
	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&lt;br /&gt;
&lt;br /&gt;
==[Model/View/Controller Frame work  http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller  ]==&lt;br /&gt;
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.&lt;br /&gt;
Key components of MVC architecture include:&lt;br /&gt;
# 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.&lt;br /&gt;
#Views: Views provide a human interface(GUI) for the model. Views in Rails are implemented using embedded ruby (.html.erb) files&lt;br /&gt;
#Controllers: Controllers process the user actions and help redirect to the appropriate model ,build a user response and update the View. &lt;br /&gt;
The typical user interaction on a Rails application happens as follows:&lt;br /&gt;
#The user responds to the UI (ex : clicks a button)&lt;br /&gt;
#The router routes the action to the appropriate controller and action in it&lt;br /&gt;
#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&lt;br /&gt;
#A view queries the model and uses its data to build a response .&lt;br /&gt;
#The view updates the UI with the view and waits for further user interaction.&lt;br /&gt;
&lt;br /&gt;
==[CRUD  http://en.wikipedia.org/wiki/Create,_read,_update_and_delete ]==&lt;br /&gt;
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:&lt;br /&gt;
#Create or add new entries&lt;br /&gt;
#Read, retrieve, search, or view existing entries&lt;br /&gt;
#Update or edit existing entries&lt;br /&gt;
#Delete existing entries&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
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 &amp;quot;contact management&amp;quot; or &amp;quot;contact maintenance&amp;quot; (or &amp;quot;document management&amp;quot; in general, depending on the basic storage unit for the particular application).&lt;br /&gt;
&lt;br /&gt;
==Routing in Rails==&lt;br /&gt;
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: &lt;br /&gt;
resources :photos &lt;br /&gt;
can allow us to specify 7 routes for the application.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! HTTP Verb&lt;br /&gt;
! Path &lt;br /&gt;
! action &lt;br /&gt;
! used for&lt;br /&gt;
|-&lt;br /&gt;
|  GET   | /photos | index | display a list of all photos&lt;br /&gt;
|  GET   | /photos/new | new  | return an HTML form for creating a new photo&lt;br /&gt;
|  POST   |	/photos	|create|	create a new photo&lt;br /&gt;
|  GET	|/photos/new|	new|	return an HTML form for creating a new photo| &lt;br /&gt;
|  POST	|/photos	|create	|create a new photo|&lt;br /&gt;
|  GET	|/photos/:id	|show	|display a specific photo|&lt;br /&gt;
|  GET	|/photos/:id/edit	|edit	|return an HTML form for editing a photo&lt;br /&gt;
|  PUT	|/photos/:id|	update	|update a specific photo&lt;br /&gt;
|  DELETE	|/photos/:id	|destroy	|delete a specific photo|}&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
resources :photos&lt;br /&gt;
creates seven different routes in your application, all mapping to the Photos controller:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Object relational Mapping==&lt;br /&gt;
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, &lt;br /&gt;
# Database tables correspond to Rails classes&lt;br /&gt;
# Database records (rows) correspond to Rails objects.&lt;br /&gt;
ActiveRecord also allow us to query the database in the object domain by using the Active Record Query INterface&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=51969</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4d mt</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4d_mt&amp;diff=51969"/>
		<updated>2011-10-15T03:17:46Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: Created page with &amp;quot;''' 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...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''' The CookBook Application (Lecture 9)'''&lt;br /&gt;
==Introduction==&lt;br /&gt;
 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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Ruby on Rails==&lt;br /&gt;
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:&lt;br /&gt;
===[ http://en.wikipedia.org/wiki/Convention_over_configuration  Convention over Configuration]===&lt;br /&gt;
	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.&lt;br /&gt;
===[ http://en.wikipedia.org/wiki/Don't_repeat_yourself  Don't repeat yourself]=== &lt;br /&gt;
	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&lt;br /&gt;
&lt;br /&gt;
==[ http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller  Model/View/Controller Frame work]==&lt;br /&gt;
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.&lt;br /&gt;
Key components of MVC architecture include:&lt;br /&gt;
# 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.&lt;br /&gt;
#Views: Views provide a human interface(GUI) for the model. Views in Rails are implemented using embedded ruby (.html.erb) files&lt;br /&gt;
#Controllers: Controllers process the user actions and help redirect to the appropriate model ,build a user response and update the View. &lt;br /&gt;
The typical user interaction on a Rails application happens as follows:&lt;br /&gt;
#The user responds to the UI (ex : clicks a button)&lt;br /&gt;
#The router routes the action to the appropriate controller and action in it&lt;br /&gt;
#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&lt;br /&gt;
#A view queries the model and uses its data to build a response .&lt;br /&gt;
#The view updates the UI with the view and waits for further user interaction.&lt;br /&gt;
&lt;br /&gt;
==[ http://en.wikipedia.org/wiki/Create,_read,_update_and_delete CRUD]==&lt;br /&gt;
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:&lt;br /&gt;
#Create or add new entries&lt;br /&gt;
#Read, retrieve, search, or view existing entries&lt;br /&gt;
#Update or edit existing entries&lt;br /&gt;
#Delete existing entries&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
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 &amp;quot;contact management&amp;quot; or &amp;quot;contact maintenance&amp;quot; (or &amp;quot;document management&amp;quot; in general, depending on the basic storage unit for the particular application).&lt;br /&gt;
&lt;br /&gt;
==Routing in Rails==&lt;br /&gt;
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: &lt;br /&gt;
resources :photos &lt;br /&gt;
can allow us to specify 7 routes for the application.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! HTTP Verb&lt;br /&gt;
! Path &lt;br /&gt;
! action &lt;br /&gt;
! used for&lt;br /&gt;
|-&lt;br /&gt;
| GET | /photos | index | display a list of all photos&lt;br /&gt;
 | GET | /photos/new | new  | return an HTML form for creating a new photo&lt;br /&gt;
|POST|	/photos	|create|	create a new photo&lt;br /&gt;
|GET	|/photos/new|	new|	return an HTML form for creating a new photo| &lt;br /&gt;
|POST	|/photos	|create	|create a new photo|&lt;br /&gt;
|GET	|/photos/:id	|show	|display a specific photo|&lt;br /&gt;
|GET	|/photos/:id/edit	|edit	|return an HTML form for editing a photo&lt;br /&gt;
|PUT	|/photos/:id|	update	|update a specific photo&lt;br /&gt;
|DELETE	|/photos/:id	|destroy	|delete a specific photo|}&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
resources :photos&lt;br /&gt;
creates seven different routes in your application, all mapping to the Photos controller:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Object relational Mapping==&lt;br /&gt;
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, &lt;br /&gt;
# Database tables correspond to Rails classes&lt;br /&gt;
# Database records (rows) correspond to Rails objects.&lt;br /&gt;
ActiveRecord also allow us to query the database in the object domain by using the Active Record Query INterface&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011&amp;diff=51967</id>
		<title>CSC/ECE 517 Fall 2011</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011&amp;diff=51967"/>
		<updated>2011-10-15T03:17:11Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Link title]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a ms]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a cs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a ri]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a lj]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1b sa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1b ds]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1b tj]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1c cm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1c sj]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1c ka]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1d sr]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e vs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e aa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a sc]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e dm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e an]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e sa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e lm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1g vn]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1f rs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1f sv]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1g jn]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1h ps]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e sm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1i zf]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1g rn]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1i cl]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1d ss]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1i lj]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1h hs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1d gs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2b ns]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2b jp]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2a av]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2f jm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2e ad]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2e kt]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2e gp]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2b qu]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2c bs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2c rs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2a ca]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2b rv]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2c ds]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2b sa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2f vh]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2e ps]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch3 3a oe]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch3 3h rr]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch3 3h ss]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch3 4b js]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4i aa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4i sd]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4d mt]]&lt;br /&gt;
&lt;br /&gt;
*[[trial]]&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_lm&amp;diff=50803</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1e lm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_lm&amp;diff=50803"/>
		<updated>2011-09-25T22:37:26Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: /* Examples */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
This wiki article discusses two things viz the effectiveness &amp;amp; durability of object oriented languages (like Java, Python, etc) as compared to block structured languages(like C, Pascal, Algol etc) and how block structure can be leveraged in an object oriented language.&lt;br /&gt;
&lt;br /&gt;
==Block structured languages==&lt;br /&gt;
&lt;br /&gt;
===Definition===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Block_(programming) block] is a piece of code consisting of one or more [http://en.wikipedia.org/wiki/Declaration_(computer_science) declarations] and [http://en.wikipedia.org/wiki/Statement_(programming) statements]. A block structured programming language is the one that would allow usage of such blocks &amp;amp; including [http://en.wikipedia.org/wiki/Nesting_(computing) nesting] of such blocks.&lt;br /&gt;
The purpose of blocks is to allow a group of statements to be treated as one.For instance, you might want to have a block of statements which does matrix multiplication.Blocks also narrow the lexical scope of the variables, procedures &amp;amp; functions declared in a block so that they don’t conflict with others(which might be used for a totally different purpose)having the same name elsewhere in the program.For instance, you might want to have a local variable “count” to count the sum of some variables whereas you might have another local variable with the same name “count” to count the number of elements in an array.&lt;br /&gt;
&lt;br /&gt;
===Basic features===&lt;br /&gt;
*Tasks are accomplished using functions. The primary focus is on functions. It involves creating a list of instructions and grouping them into blocks.&lt;br /&gt;
*The functions share global data. The data is stored in data structures which act as information containers. These are global in scope and are passed around between functions using parameters.&lt;br /&gt;
*It uses a top down approach&amp;lt;ref&amp;gt;[http://www.robdavispe.com/free2/software-qa-testing-test-tester-2229.html Davis, Rob. &amp;quot;What Is the Difference Between Top Down and Bottom up Design?&amp;quot; N.p., 2001. Web.]&amp;lt;/ref&amp;gt; of program design. Here the system is broken down into sub-systems which are recursively refined into additional sub- system levels until we reach base level of granularity which can be implemented by a single function.&lt;br /&gt;
*Functions are implemented as black-boxes with an input and output specification.&lt;br /&gt;
&lt;br /&gt;
===Examples===&lt;br /&gt;
Some of the examples of these languages include Algol, C &amp;amp; Pascal.&lt;br /&gt;
*E.g. Pascal program multiply two numbers.&lt;br /&gt;
Here we can see that the block is delimited by &amp;quot;Begin&amp;quot; &amp;amp; &amp;quot;End&amp;quot; keywords&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
Program Multiplication_Of_Two_Numbers;&lt;br /&gt;
&lt;br /&gt;
Var Product, Multiplier, Multiplicand : Real; &lt;br /&gt;
&lt;br /&gt;
Begin&lt;br /&gt;
    Writeln('Program to multiply 2 numbers:');&lt;br /&gt;
    Writeln;&lt;br /&gt;
    Writeln;&lt;br /&gt;
    Write('Enter the Multilicand: ');&lt;br /&gt;
    Readln(Multiplicand);&lt;br /&gt;
    Writeln;&lt;br /&gt;
    Write('Enter the Multiplier: ');&lt;br /&gt;
    Readln(Multiplier);&lt;br /&gt;
   &lt;br /&gt;
    Product := Multiplicand * Multiplier;&lt;br /&gt;
    Writeln;&lt;br /&gt;
    Writeln('Product of ',Multiplicand,'and ',Multiplier,'equals '+&lt;br /&gt;
           +'to ',Product);&lt;br /&gt;
&lt;br /&gt;
End.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*E.g. C program to read a file line by line.&lt;br /&gt;
Here we can see that the blocks are delimited by the flower braces. We can also see the usage of global variable by the function print_file().&lt;br /&gt;
Also, the print_file returns an integer which represents success. By using return a called block can return a value to the scope of the calling block.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
FILE *ifp;                                                                 /*File pointer which points to the file*/&lt;br /&gt;
&lt;br /&gt;
int print_file(char *filename)&lt;br /&gt;
{&lt;br /&gt;
      char line[80];                                                       /*Assuming each line is 80 character in length*/&lt;br /&gt;
      ifp = fopen(*filename, &amp;quot;r&amp;quot;);                                         /*opens the file for &amp;quot;reading&amp;quot; mode only*/&lt;br /&gt;
      if(!ifp)&lt;br /&gt;
           return -1;&lt;br /&gt;
      while(fgets(line,1,sizeof(line),fp)                                  /*Loop till no more lines to read*/&lt;br /&gt;
      {&lt;br /&gt;
            printf(&amp;quot;%s&amp;quot;, line);                                            /*Prints this line on to the standard output*/&lt;br /&gt;
      }&lt;br /&gt;
      fclose(fp);                                                          /*Closes the file*/&lt;br /&gt;
      return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void main()&lt;br /&gt;
{&lt;br /&gt;
     char *name;&lt;br /&gt;
     int success;&lt;br /&gt;
&lt;br /&gt;
     printf(&amp;quot;Enter the name of the file to be opened: &amp;quot;);&lt;br /&gt;
     scanf(&amp;quot;%s&amp;quot;, name);          &lt;br /&gt;
     &lt;br /&gt;
     success = print_file(name);                                                     /*Function call made to print_file() to print the file*/&lt;br /&gt;
     if(success == -1)&lt;br /&gt;
           printf(&amp;quot;\n Error opening file&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object Oriented Languages:==&lt;br /&gt;
&lt;br /&gt;
===Definition===&lt;br /&gt;
Object oriented languages are built on the object oriented concept. This is the programming paradigm that bundles the data and the methods that access and manage this data into single entity called an '''''Object'''''. This bundling of data and methods allows the object to act as a single entity and have a fluid interface through which it can interact with the outside world. The objects are generally modeled on real world objects.&lt;br /&gt;
&lt;br /&gt;
===Fundamental features of Object Oriented languages===&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_%28computer_science%29 Object]/[http://en.wikipedia.org/wiki/Class_%28computer_programming%29 Class]: Class consists of tightly coupled data fields and set of operations that act on the data. The entire set of data and operations represent a user-defined data type. An object is an instance of a class. Thus a class acts as a collection of objects of similar type. Multiple objects may be created out of a class. The objects may share the operations of the class but have their own set of data fields. An object can be defined by its current state (current value of its data members) and its behavior (the methods that can be called on the object).  &lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Abstraction_(computer_science) Data Abstraction]/[http://en.wikipedia.org/wiki/Encapsulation_%28object-oriented_programming%29 Encapsulation]: The wrapping of data and functions into a single unit (class) is called encapsulation. Encapsulation protects an object from accidental modifications by an external entity since data fields can be accessed only through predetermined interfaces. Encapsulation is implemented using language constructs like private, public, protected, etc. Abstraction involves representing only the essential features of the object and hiding the background details. Classes provide abstraction by hiding the data attributes and providing functions that provide a controlled view of the object to an external entity.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Inheritance_%28object-oriented_programming%29 Inheritance]:  Inheritance is the ability of a class to inherit partly or completely the features of another class. It allows the hierarchical classification of objects.For example, the bird ''Peacock'' is a part of the class ''Flying bird'' which again is part of the class ''Bird''. Inheritance allows the reusability of code. Pre-existing classes can be reused by extending them and adding new functionality to create sub classes without modifying the original classes. The sub class will then have the combined features of both classes. This allows a programmer to tailor a new class from an existing class without introducing any undesirable side effects in the base classes.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Interface_%28Java%29 Interface]: Interface is an abstract type containing no data but only a list of method definitions. A class that has all the methods defined in the interface is said to have implemented the method interface. Interfaces allow classes to define how they will communicate with an external entity.  By implementing an interface, a class promises an external entity using the interface that it will provide the functionality specified by the interface.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]/[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding]: This is the ability of a variable, method or an object to take more than one forms. This enables an objects of different types to respond to same method or property call but in its own type specific way. For example consider overloading the + operator, so that when called on strings, it concatenates the strings while for numbers it adds them up. Polymorphism also allows objects with different internal structures to share the same external interface. This helps in dynamic binding. Dynamic binding means that the code executed for a given function call is not known until actual runtime. For example, in a dynamically typed language a variable can hold an object irrespective of its data type. Hence a function call associated with a reference (variable) depends on the dynamic type of that reference (variable) i.e. the current object referenced by the variable.&lt;br /&gt;
&lt;br /&gt;
===Examples===&lt;br /&gt;
The below languages implement different degrees of OO  features&amp;lt;ref&amp;gt;[http://www.jvoegele.com/software/langcomp.html Voegele, Jason. &amp;quot;Programming Language Comparison.&amp;quot; Web.]&amp;lt;/ref&amp;gt;&lt;br /&gt;
*Ruby, Scala, Smalltalk, Eiffel are said to be purely OO languages. Here even primitives are implemented as OO objects.&lt;br /&gt;
*Java, C++, C#, VB.NET, Python are OO languages but have some degree of procedural elements&lt;br /&gt;
*Visual Basic, PHP, Perl are basically procedural languages with some OO features.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
E.g. C++ program demonstrating the use of a class .&lt;br /&gt;
The class CRectangle represents a rectangle and has data members x,y which represent the length and breadth (ie: properties) of the rectangle. The functions set_values() and area() allow us to access these data members in a controlled manner. The class object has been instantiated in function main&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
&lt;br /&gt;
class CRectangle {&lt;br /&gt;
    int x, y;&lt;br /&gt;
  public:&lt;br /&gt;
    void set_values (int,int);&lt;br /&gt;
    int area () ;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
void CRectangle::set_values (int a, int b) {&lt;br /&gt;
  x = a;&lt;br /&gt;
  y = b;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void CRectangle::area() {&lt;br /&gt;
  return x*y;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main () {&lt;br /&gt;
  CRectangle rect;&lt;br /&gt;
  rect.set_values (3,4);&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;area: &amp;quot; &amp;lt;&amp;lt; rect.area();&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
 &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Need for Object Oriented Languages==&lt;br /&gt;
Block structured languages follow a task-centric approach rather than a data-centric approach. This resulted in certain flaws:&lt;br /&gt;
#Structured programming follows a hierarchy of structures. However in large programs data is generally in the form of a network of structures. This resulted in a lot of [http://en.wiktionary.org/wiki/tramp_data tramp data] being passed around.&amp;lt;ref&amp;gt; [http://en.wikipedia.org/wiki/Structured_programming#Object-oriented_comparison Object Oriented Comparison. Web. 8 June 2011.] &amp;lt;/ref&amp;gt;&lt;br /&gt;
#As the program size increased the amount of data being passed around increased proportionately. Ensuring parameters are correctly passed to the correct subprogram becomes a major issue.&lt;br /&gt;
#In case of large programs even a small change resulted in massive ripple-effect with changing multiple sub modules to propagate the new data through the program's hierarchy.&lt;br /&gt;
#Structured program shared a lot of global data.&amp;lt;ref&amp;gt;[http://c2.com/cgi/wiki?GlobalVariablesAreBad Global Variables Are Bad]&amp;lt;/ref&amp;gt; The access to the data was uncontrolled and unpredictable. There was no control over who had access to what.&lt;br /&gt;
#Structured program suffered from memory leaks.&amp;lt;ref&amp;gt;[http://www.cprogramming.com/debugging/valgrind.html Using Valgrind to Find Memory Leaks and Invalid Memory Use]&amp;lt;/ref&amp;gt; This was because anyone could create global structures and there was no responsibility on the creator to deallocate the memory.&lt;br /&gt;
The main motivating factor for object-oriented approach is to remove these flaws of block structured languages. OO languages treat data as a critical element and do not allow it to flow freely around the system. The data is tied to the functions with operate on and prevent it from accidental modification from external functions.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Object-oriented languages over Block structured languages==&lt;br /&gt;
#Object oriented approach follows a data centered approach. It is possible to map objects in the problem domain to those in the program. This helps the programmer visualize the program in terms of real world objects and which leads to a much better design. Also it allows more details of a model to be captured in implementable form.&amp;lt;ref&amp;gt;Balaguruswamy, E. Object Oriented Programming with C++. New Delhi: Tata Mcgraw-Hill, 2008. Print.&amp;lt;/ref&amp;gt;&lt;br /&gt;
#The principle of data hiding helps in building secure programs. The data fields of an object can be accessed only through the interface defined by its publicly accessible methods. Such controlled access prevents any inadvertent change to object data by external modules.&amp;lt;ref&amp;gt;[http://www.developer.com/design/article.php/3387591/Hiding-Data-within-Object-Oriented-Programming.htm Weisfeld, Matt. &amp;quot;Hiding Data within Object-Oriented Programming.&amp;quot; N.p., 29 July 2004. Web.]&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Mapping real world entities into objects help reduce and easily manage software complexity . Also it helps when systems need to be upgraded to larger systems.&amp;lt;ref&amp;gt;[http://www.artima.com/interfacedesign/SigObjects.html &amp;quot;Interface Design by Bill Venners.&amp;quot; Web.]&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Objects act as black boxes with internal structure and well defined interfaces. Hence adding new functionality becomes easier, since we can easily isolate the objects/classes that need to be modified or extended. The low coupling between objects allows an object to be modified without any effect on the other objects accessing it so long as the interface through which they interact remain untouched.&lt;br /&gt;
#Most modern object oriented languages provides a load of class libraries and frameworks. These are much easier to use as compared to library functions present in block structured languages. The frameworks help in rapid software development and easier testing and debugging thus leading to increased productivity.&amp;lt;ref&amp;gt;[http://www.cs.bgsu.edu/maner/domains/RAD.htm Maner, Walter. RAPID APPLICATION DEVELOPMENT. N.p., 15 Mar. 1997. Web.]&amp;lt;/ref&amp;gt; &amp;lt;ref&amp;gt;[http://www.chillarege.com/authwork/TestingBestPractice.pdf Chillarege, Ram. &amp;quot;Software Testing Best Practices.&amp;quot; N.p., 26 Apr. 1999. Web.]&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Message passing techniques (ex: interfaces in JAVA) help separate implementation of an object from its externally visible interface. Multiple classes of objects may implement the same interface. In such cases an object can be replaced by another object without affecting external entities dependent on that object so long as the interface is untouched. This results in low coupling.&lt;br /&gt;
#Object oriented design provides much better memory management. Recent OO languages sport automated garbage handling. Even for languages without garbage collection&amp;lt;ref&amp;gt;[http://javarevisited.blogspot.com/2011/04/garbage-collection-in-java.html Paul, Javin. &amp;quot;Garbage Collection in Java.&amp;quot; How Garbage Collection Works In Java. N.p., 11 Apr. 2011. Web. ]&amp;lt;/ref&amp;gt;, memory deallocation of data members can be handled in the destructors of the objects. But in case of block structured languages (like C), it is the programmers responsibility to take care of releasing the dynamically allocated memory. Also the data structures tend to be global in scope and persist beyond the scope of the functions calling them. Hence it becomes difficult to fix responsibility on functions to deallocate them. This leads to memory leaks.&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Object-Oriented Languages==&lt;br /&gt;
Although object oriented languages sport some very advantageous features as compared to block structured languages, they do have some drawbacks. &lt;br /&gt;
#Economy of language features: The addition of OO features to a language and the additional syntax needed to implement these features makes it bulky. The complexity of these languages make them daunting to learn and master.&amp;lt;ref name=&amp;quot;badEnggProperties&amp;quot;&amp;gt;[http://lucacardelli.name/Papers/BadPropertiesOfOO.html Bad Engineering Properties of Object-Oriented Languages by Luca Cardelli]&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Economy of execution: OO style is intrinsically less efficient as compared to block structured style. In procedural languages, type checking occurs at compile time. Hence no tests are needed at runtime for pointer dereferencing for method calls.In OO style each routine is supposed to be a virtual method. This introduces indirections in the form of method tables during for dereferencing.&lt;br /&gt;
#Also many OO languages include automatic garbage collection (e.g. Java) &amp;amp; hence it might slow down the performance.&amp;lt;ref&amp;gt;[http://www.jelovic.com/articles/why_java_is_slow.htm Why Java is slow Jelovic, Dejan. &amp;quot;Why Java Is Slow.&amp;quot; Why Java Will Always Be Slower Than C++. N.p., 18 Dec. 2009. Web.]&amp;lt;/ref&amp;gt;. This along with the overhead of creation and destruction of objects degrades performance in resource intensive systems such as embedded systems and real time systems.&amp;lt;ref&amp;gt;[http://csjournals.com/IJCSC/PDF1-2/9..pdf Object Oriented Vs Procedural Programming in Embedded System ]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Usage of block-structures in Object Oriented languages==&lt;br /&gt;
OO languages have evolved from block structured languages. Though OO languages use the concepts of class and objects,inheritance,etc they inherently still rely on a blocks to define the structure of  the code. The key areas where OO languages assimilate the idea of block structure are as follows:&lt;br /&gt;
*In classes the member functions or methods are implemented using blocks. Each method can be viewed as a block that accepts parameters from an external entity that calls it, performs some processing (that may or may not require the use of data fields of the object) and returns control back with or without a return value.  &lt;br /&gt;
*Blocks are also useful in defining control structures like loops, conditional blocks(e.g. if...else), etc.&lt;br /&gt;
E.g. Java program demonstrating the usage of a while loop .The while loop encloses a group of statements within curly braces signifying a block that may be iterated upon till while condition holds true&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
package hashmap;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.LinkedHashMap;&lt;br /&gt;
import java.util.Map;&lt;br /&gt;
import java.util.Set;&lt;br /&gt;
&lt;br /&gt;
public class ExampleHashMap&lt;br /&gt;
{&lt;br /&gt;
	public static void main(String args[])&lt;br /&gt;
	{&lt;br /&gt;
		LinkedHashMap h = new LinkedHashMap();                          //Creates a LinkedHashMap. Linked HashMaps defines the &lt;br /&gt;
		h.put(&amp;quot;A&amp;quot;,100);                                                 //iteration ordering, which is normally the &lt;br /&gt;
		h.put(&amp;quot;B&amp;quot;,200);                                                 //order in which keys were inserted into the map.&lt;br /&gt;
		h.put(&amp;quot;C&amp;quot;,300);&lt;br /&gt;
		h.put(&amp;quot;D&amp;quot;,400);&lt;br /&gt;
		h.put(&amp;quot;E&amp;quot;,500);&lt;br /&gt;
		&lt;br /&gt;
		Set s = h.entrySet();                                           //Returns a set containing key value pairs&lt;br /&gt;
		Iterator i = s.iterator();                                      &lt;br /&gt;
		&lt;br /&gt;
		while(i.hasNext())&lt;br /&gt;
		{&lt;br /&gt;
			Map.Entry me = (Map.Entry)i.next();                     //Each element in the set has to be casted to Map.Entry &lt;br /&gt;
			System.out.println(me.getKey()+&amp;quot; : &amp;quot;+me.getValue());    //to access the key/value &lt;br /&gt;
		}&lt;br /&gt;
       }&lt;br /&gt;
}&lt;br /&gt;
		&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
*OO languages generally provide inbuilt language support for exception handling. Most OO languages implement exception handling in terms of blocks&lt;br /&gt;
E.g.: Try...catch  blocks in Java&lt;br /&gt;
Here the catch and finally blocks are used to delimit a group of statements that can be called to handle exceptions at runtime. Also, we can see usage of conditional statements if...else in this case.&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
try{&lt;br /&gt;
    System.out.println(&amp;quot;Inside try block of testMethod!&amp;quot;);&lt;br /&gt;
    int p=1000, q=0, i=0;&lt;br /&gt;
    if(p&amp;gt;q){                                               //We can clearly see here that a Divide by Zero exception will occur.&lt;br /&gt;
         i=p/q;&lt;br /&gt;
    }&lt;br /&gt;
    else if(q&amp;gt;p){&lt;br /&gt;
         i=q/p;&lt;br /&gt;
    }&lt;br /&gt;
    else{                                                 //In this case p equals q&lt;br /&gt;
         i=p/q;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
catch(Exception e){&lt;br /&gt;
    System.out.println(&amp;quot;Inside catch block of testMethod!&amp;quot;);&lt;br /&gt;
    System.out.println(“Caution: your trying to Divide a number by Zero...”);&lt;br /&gt;
}&lt;br /&gt;
finally{                                                  &lt;br /&gt;
    System.out.println(&amp;quot;Inside finally block of testMethod!&amp;quot;);&lt;br /&gt;
    System.out.println(“This block executes everytime an exception is caught...”);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
*Certain OO languages like JAVA allow creation of anonymous classes which are basically blocks which don’t have a name but can be instantiated without actually making a separate class.&lt;br /&gt;
E.g.&lt;br /&gt;
Here ActionListener is an interface which has been implemented as anonymous block in the below JAVA example&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
import java.awt.Button;&lt;br /&gt;
&lt;br /&gt;
class Submit()&lt;br /&gt;
{&lt;br /&gt;
       public Submit()&lt;br /&gt;
       {&lt;br /&gt;
              Button button1 = new Button(&amp;quot;Submit&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
              button1.addActionListener(new ActionListener(){&lt;br /&gt;
                     public void actionPerformed (ActionEvent event){&lt;br /&gt;
                            System.out.println(&amp;quot;actionPerfomed is implemented as anonymous class here &amp;amp; is added as a listener to button1&amp;quot;);&lt;br /&gt;
                     }  &lt;br /&gt;
              });&lt;br /&gt;
       }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
*In a pure OO language like Ruby, blocks can also be used to defines closures(blocks, procs &amp;amp; lambdas)&amp;lt;ref&amp;gt;[http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas Sosinski, Robert. &amp;quot;Understanding Ruby Blocks, Procs and Lambdas.&amp;quot; N.p., 21 Dec. 2008. Web. 8 Sept. 2011.]&amp;lt;/ref&amp;gt;&lt;br /&gt;
E.g.&lt;br /&gt;
Given below is a method which yields control to a block in Ruby &amp;amp; also passes parameter to it at the same time.&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
def block_demo&lt;br /&gt;
    yield(&amp;quot;Smith&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
block_demo { |name| puts &amp;quot; Hello #{name} &amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The Output will be&lt;br /&gt;
 Hello Smith &lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Block structured languages had their uses in earlier days when code size was small and the main focus was on efficiency(in terms of space &amp;amp; memory) of programs. However, these programs could not scale in size as maintenance and code complexity became an overhead.&lt;br /&gt;
&lt;br /&gt;
Object Oriented concepts like encapsulation, inheritance(which helps in code re-usability), interfaces &amp;amp; polymorphism allowed better visualization of the software problem and its design to handle this complexity. Moreover, class libraries and frameworks developed in these languages helped abstract the code complexity and allowed rapid program development. This led to a gradual transition from block oriented to object oriented languages for application development.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
#[http://golang.org/ The Go Programming Language by Google for multicore and networked machines]&lt;br /&gt;
#[http://download.oracle.com/javase/tutorial/java/concepts/ Object Oriented Concepts (Java Tutorials)] &lt;br /&gt;
#[http://www.daimi.au.dk/publications/PB/230/PB-230.pdf  Block Structure and Object Oriented Languages by Ole Lehrmann Madsen]&lt;br /&gt;
#[http://portal.acm.org/citation.cfm?id=356671 A Tutorial on Algol 68, by Andrew S. Tanenbaum, in Computing Surveys, Vol. 8, No. 2, June 1976, with Corrigenda (Vol. 9, No. 3, September 1977) ] &lt;br /&gt;
#[http://ruby-lang.org Official Ruby Webpage]&lt;br /&gt;
#[http://www.pascal-programming.info/index.php Pascal programming tutorials]&lt;br /&gt;
#[http://steve-yegge.blogspot.com/2008/05/dynamic-languages-strike-back.html Dynamic Languages Strike Back]&lt;br /&gt;
#[http://amath.colorado.edu/computing/mmm/funcproc.html Functional vs Procedural languages]&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_lm&amp;diff=50781</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1e lm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_lm&amp;diff=50781"/>
		<updated>2011-09-25T22:30:52Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: /* Examples */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
This wiki article discusses two things viz the effectiveness &amp;amp; durability of object oriented languages (like Java, Python, etc) as compared to block structured languages(like C, Pascal, Algol etc) and how block structure can be leveraged in an object oriented language.&lt;br /&gt;
&lt;br /&gt;
==Block structured languages==&lt;br /&gt;
&lt;br /&gt;
===Definition===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Block_(programming) block] is a piece of code consisting of one or more [http://en.wikipedia.org/wiki/Declaration_(computer_science) declarations] and [http://en.wikipedia.org/wiki/Statement_(programming) statements]. A block structured programming language is the one that would allow usage of such blocks &amp;amp; including [http://en.wikipedia.org/wiki/Nesting_(computing) nesting] of such blocks.&lt;br /&gt;
The purpose of blocks is to allow a group of statements to be treated as one.For instance, you might want to have a block of statements which does matrix multiplication.Blocks also narrow the lexical scope of the variables, procedures &amp;amp; functions declared in a block so that they don’t conflict with others(which might be used for a totally different purpose)having the same name elsewhere in the program.For instance, you might want to have a local variable “count” to count the sum of some variables whereas you might have another local variable with the same name “count” to count the number of elements in an array.&lt;br /&gt;
&lt;br /&gt;
===Basic features===&lt;br /&gt;
*Tasks are accomplished using functions. The primary focus is on functions. It involves creating a list of instructions and grouping them into blocks.&lt;br /&gt;
*The functions share global data. The data is stored in data structures which act as information containers. These are global in scope and are passed around between functions using parameters.&lt;br /&gt;
*It uses a top down approach of program design. Here the system is broken down into sub-systems which are recursively refined into additional sub- system levels until we reach base level of granularity which can be implemented by a single function.&lt;br /&gt;
*Functions are implemented as black-boxes with an input and output specification.&lt;br /&gt;
&lt;br /&gt;
===Examples===&lt;br /&gt;
Some of the examples of these languages include Algol, C &amp;amp; Pascal.&lt;br /&gt;
*E.g. Pascal program multiply two numbers.&lt;br /&gt;
Here we can see that the block is delimited by &amp;quot;Begin&amp;quot; &amp;amp; &amp;quot;End&amp;quot; keywords&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
Program Multiplication_Of_Two_Numbers;&lt;br /&gt;
&lt;br /&gt;
Var Product, Multiplier, Multiplicand : Real; &lt;br /&gt;
&lt;br /&gt;
Begin&lt;br /&gt;
    Writeln('Program to multiply 2 numbers:');&lt;br /&gt;
    Writeln;&lt;br /&gt;
    Writeln;&lt;br /&gt;
    Write('Enter the Multilicand: ');&lt;br /&gt;
    Readln(Multiplicand);&lt;br /&gt;
    Writeln;&lt;br /&gt;
    Write('Enter the Multiplier: ');&lt;br /&gt;
    Readln(Multiplier);&lt;br /&gt;
   &lt;br /&gt;
    Product := Multiplicand * Multiplier;&lt;br /&gt;
    Writeln;&lt;br /&gt;
    Writeln('Product of ',Multiplicand,'and ',Multiplier,'equals '+&lt;br /&gt;
           +'to ',Product);&lt;br /&gt;
&lt;br /&gt;
End.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*E.g. C program to read a file line by line.&lt;br /&gt;
Here we can see that the blocks are delimited by the flower braces. We can also see the usage of global variable by the function print_file().&lt;br /&gt;
Also, we can see that in our case we explictly tell the main() not to expect any return value from the function print_file() with the help of keyword &amp;quot;void&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
FILE *ifp;                                                                 /*File pointer which points to the file*/&lt;br /&gt;
&lt;br /&gt;
void print_file(char *filename)&lt;br /&gt;
{&lt;br /&gt;
      char line[80];                                                       /*Assuming each line is 80 character in length*/&lt;br /&gt;
      ifp = fopen(*filename, &amp;quot;r&amp;quot;);                                         /*opens the file for &amp;quot;reading&amp;quot; mode only*/&lt;br /&gt;
&lt;br /&gt;
      while(fgets(line,1,sizeof(line),fp)                                  /*Loop till no more lines to read*/&lt;br /&gt;
      {&lt;br /&gt;
            printf(&amp;quot;%s&amp;quot;, line);                                            /*Prints this line on to the standard output*/&lt;br /&gt;
      }&lt;br /&gt;
      fclose(fp);                                                          /*Closes the file*/&lt;br /&gt;
       &lt;br /&gt;
}&lt;br /&gt;
void main()&lt;br /&gt;
{&lt;br /&gt;
     char *name;&lt;br /&gt;
&lt;br /&gt;
     printf(&amp;quot;Enter the name of the file to be opened: &amp;quot;);&lt;br /&gt;
     scanf(&amp;quot;%s&amp;quot;, name);          &lt;br /&gt;
&lt;br /&gt;
     print_file(name);                                                     /*Function call made to print_file() to print the file*/&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object Oriented Languages:==&lt;br /&gt;
&lt;br /&gt;
===Definition===&lt;br /&gt;
Object oriented languages are built on the object oriented concept. This is the programming paradigm that bundles the data and the methods that access and manage this data into single entity called an '''''Object'''''. This bundling of data and methods allows the object to act as a single entity and have a fluid interface through which it can interact with the outside world. The objects are generally modeled on real world objects.&lt;br /&gt;
&lt;br /&gt;
===Fundamental features of Object Oriented languages===&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_%28computer_science%29 Object]/[http://en.wikipedia.org/wiki/Class_%28computer_programming%29 Class]: Class consists of tightly coupled data fields and set of operations that act on the data. The entire set of data and operations represent a user-defined data type. An object is an instance of a class. Thus a class acts as a collection of objects of similar type. Multiple objects may be created out of a class. The objects may share the operations of the class but have their own set of data fields. An object can be defined by its current state (current value of its data members) and its behavior (the methods that can be called on the object).  &lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Abstraction_(computer_science) Data Abstraction]/[http://en.wikipedia.org/wiki/Encapsulation_%28object-oriented_programming%29 Encapsulation]: The wrapping of data and functions into a single unit (class) is called encapsulation. Encapsulation protects an object from accidental modifications by an external entity since data fields can be accessed only through predetermined interfaces. Encapsulation is implemented using language constructs like private, public, protected, etc. Abstraction involves representing only the essential features of the object and hiding the background details. Classes provide abstraction by hiding the data attributes and providing functions that provide a controlled view of the object to an external entity.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Inheritance_%28object-oriented_programming%29 Inheritance]:  Inheritance is the ability of a class to inherit partly or completely the features of another class. It allows the hierarchical classification of objects.For example, the bird ''Peacock'' is a part of the class ''Flying bird'' which again is part of the class ''Bird''. Inheritance allows the reusability of code. Pre-existing classes can be reused by extending them and adding new functionality to create sub classes without modifying the original classes. The sub class will then have the combined features of both classes. This allows a programmer to tailor a new class from an existing class without introducing any undesirable side effects in the base classes.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Interface_%28Java%29 Interface]: Interface is an abstract type containing no data but only a list of method definitions. A class that has all the methods defined in the interface is said to have implemented the method interface. Interfaces allow classes to define how they will communicate with an external entity.  By implementing an interface, a class promises an external entity using the interface that it will provide the functionality specified by the interface.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]/[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding]: This is the ability of a variable, method or an object to take more than one forms. This enables an objects of different types to respond to same method or property call but in its own type specific way. For example consider overloading the + operator, so that when called on strings, it concatenates the strings while for numbers it adds them up. Polymorphism also allows objects with different internal structures to share the same external interface. This helps in dynamic binding. Dynamic binding means that the code executed for a given function call is not known until actual runtime. For example, in a dynamically typed language a variable can hold an object irrespective of its data type. Hence a function call associated with a reference (variable) depends on the dynamic type of that reference (variable) i.e. the current object referenced by the variable.&lt;br /&gt;
&lt;br /&gt;
===Examples===&lt;br /&gt;
The below languages implement different degrees of OO  features&amp;lt;ref&amp;gt;[http://www.jvoegele.com/software/langcomp.html Voegele, Jason. &amp;quot;Programming Language Comparison.&amp;quot; Web.]&amp;lt;/ref&amp;gt;&lt;br /&gt;
*Ruby, Scala, Smalltalk, Eiffel are said to be purely OO languages. Here even primitives are implemented as OO objects.&lt;br /&gt;
*Java, C++, C#, VB.NET, Python are OO languages but have some degree of procedural elements&lt;br /&gt;
*Visual Basic, PHP, Perl are basically procedural languages with some OO features.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
E.g. C++ program demonstrating the use of a class .&lt;br /&gt;
The class CRectangle represents a rectangle and has data members x,y which represent the length and breadth (ie: properties) of the rectangle. The functions set_values() and area() allow us to access these data members in a controlled manner. The class object has been instantiated in function main&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
&lt;br /&gt;
class CRectangle {&lt;br /&gt;
    int x, y;&lt;br /&gt;
  public:&lt;br /&gt;
    void set_values (int,int);&lt;br /&gt;
    int area () ;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
void CRectangle::set_values (int a, int b) {&lt;br /&gt;
  x = a;&lt;br /&gt;
  y = b;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void CRectangle::area() {&lt;br /&gt;
  return x*y;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main () {&lt;br /&gt;
  CRectangle rect;&lt;br /&gt;
  rect.set_values (3,4);&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;area: &amp;quot; &amp;lt;&amp;lt; rect.area();&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
 &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Need for Object Oriented Languages==&lt;br /&gt;
Block structured languages follow a task-centric approach rather than a data-centric approach. This resulted in certain flaws:&lt;br /&gt;
#Structured programming follows a hierarchy of structures. However in large programs data is generally in the form of a network of structures. This resulted in a lot of [http://en.wiktionary.org/wiki/tramp_data tramp data] being passed around.&amp;lt;ref&amp;gt; [http://en.wikipedia.org/wiki/Structured_programming#Object-oriented_comparison Object Oriented Comparison. Web. 8 June 2011.] &amp;lt;/ref&amp;gt;&lt;br /&gt;
#As the program size increased the amount of data being passed around increased proportionately. Ensuring parameters are correctly passed to the correct subprogram becomes a major issue.&lt;br /&gt;
#In case of large programs even a small change resulted in massive ripple-effect with changing multiple sub modules to propagate the new data through the program's hierarchy.&lt;br /&gt;
#Structured program shared a lot of global data.&amp;lt;ref&amp;gt;[http://c2.com/cgi/wiki?GlobalVariablesAreBad Global Variables Are Bad]&amp;lt;/ref&amp;gt; The access to the data was uncontrolled and unpredictable. There was no control over who had access to what.&lt;br /&gt;
#Structured program suffered from memory leaks.&amp;lt;ref&amp;gt;[http://www.cprogramming.com/debugging/valgrind.html Using Valgrind to Find Memory Leaks and Invalid Memory Use]&amp;lt;/ref&amp;gt; This was because anyone could create global structures and there was no responsibility on the creator to deallocate the memory.&lt;br /&gt;
The main motivating factor for object-oriented approach is to remove these flaws of block structured languages. OO languages treat data as a critical element and do not allow it to flow freely around the system. The data is tied to the functions with operate on and prevent it from accidental modification from external functions.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Object-oriented languages over Block structured languages==&lt;br /&gt;
#Object oriented approach follows a data centered approach. It is possible to map objects in the problem domain to those in the program. This helps the programmer visualize the program in terms of real world objects and which leads to a much better design. Also it allows more details of a model to be captured in implementable form.&amp;lt;ref&amp;gt;Balaguruswamy, E. Object Oriented Programming with C++. New Delhi: Tata Mcgraw-Hill, 2008. Print.&amp;lt;/ref&amp;gt;&lt;br /&gt;
#The principle of data hiding helps in building secure programs. The data fields of an object can be accessed only through the interface defined by its publicly accessible methods. Such controlled access prevents any inadvertent change to object data by external modules.&amp;lt;ref&amp;gt;[http://www.developer.com/design/article.php/3387591/Hiding-Data-within-Object-Oriented-Programming.htm Weisfeld, Matt. &amp;quot;Hiding Data within Object-Oriented Programming.&amp;quot; N.p., 29 July 2004. Web.]&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Mapping real world entities into objects help reduce and easily manage software complexity . Also it helps when systems need to be upgraded to larger systems.&amp;lt;ref&amp;gt;[http://www.artima.com/interfacedesign/SigObjects.html &amp;quot;Interface Design by Bill Venners.&amp;quot; Web.]&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Objects act as black boxes with internal structure and well defined interfaces. Hence adding new functionality becomes easier, since we can easily isolate the objects/classes that need to be modified or extended. The low coupling between objects allows an object to be modified without any effect on the other objects accessing it so long as the interface through which they interact remain untouched.&lt;br /&gt;
#Most modern object oriented languages provides a load of class libraries and frameworks. These are much easier to use as compared to library functions present in block structured languages. The frameworks help in rapid software development and easier testing and debugging thus leading to increased productivity.&amp;lt;ref&amp;gt;[http://www.cs.bgsu.edu/maner/domains/RAD.htm Maner, Walter. RAPID APPLICATION DEVELOPMENT. N.p., 15 Mar. 1997. Web.]&amp;lt;/ref&amp;gt; &amp;lt;ref&amp;gt;[http://www.chillarege.com/authwork/TestingBestPractice.pdf Chillarege, Ram. &amp;quot;Software Testing Best Practices.&amp;quot; N.p., 26 Apr. 1999. Web.]&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Message passing techniques (ex: interfaces in JAVA) help separate implementation of an object from its externally visible interface. Multiple classes of objects may implement the same interface. In such cases an object can be replaced by another object without affecting external entities dependent on that object so long as the interface is untouched. This results in low coupling.&lt;br /&gt;
#Object oriented design provides much better memory management. Recent OO languages sport automated garbage handling. Even for languages without garbage collection&amp;lt;ref&amp;gt;[http://javarevisited.blogspot.com/2011/04/garbage-collection-in-java.html Paul, Javin. &amp;quot;Garbage Collection in Java.&amp;quot; How Garbage Collection Works In Java. N.p., 11 Apr. 2011. Web. ]&amp;lt;/ref&amp;gt;, memory deallocation of data members can be handled in the destructors of the objects. But in case of block structured languages (like C), it is the programmers responsibility to take care of releasing the dynamically allocated memory. Also the data structures tend to be global in scope and persist beyond the scope of the functions calling them. Hence it becomes difficult to fix responsibility on functions to deallocate them. This leads to memory leaks.&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Object-Oriented Languages==&lt;br /&gt;
Although object oriented languages sport some very advantageous features as compared to block structured languages, they do have some drawbacks. &lt;br /&gt;
#Economy of language features: The addition of OO features to a language and the additional syntax needed to implement these features makes it bulky. The complexity of these languages make them daunting to learn and master.&amp;lt;ref name=&amp;quot;badEnggProperties&amp;quot;&amp;gt;[http://lucacardelli.name/Papers/BadPropertiesOfOO.html Bad Engineering Properties of Object-Oriented Languages by Luca Cardelli]&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Economy of execution: OO style is intrinsically less efficient as compared to block structured style. In procedural languages, type checking occurs at compile time. Hence no tests are needed at runtime for pointer dereferencing for method calls.In OO style each routine is supposed to be a virtual method. This introduces indirections in the form of method tables during for dereferencing.&lt;br /&gt;
#Also many OO languages include automatic garbage collection (e.g. Java) &amp;amp; hence it might slow down the performance.&amp;lt;ref&amp;gt;[http://www.jelovic.com/articles/why_java_is_slow.htm Why Java is slow Jelovic, Dejan. &amp;quot;Why Java Is Slow.&amp;quot; Why Java Will Always Be Slower Than C++. N.p., 18 Dec. 2009. Web.]&amp;lt;/ref&amp;gt;. This along with the overhead of creation and destruction of objects degrades performance in resource intensive systems such as embedded systems and real time systems.&amp;lt;ref&amp;gt;[http://csjournals.com/IJCSC/PDF1-2/9..pdf Object Oriented Vs Procedural Programming in Embedded System ]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Usage of block-structures in Object Oriented languages==&lt;br /&gt;
OO languages have evolved from block structured languages. Though OO languages use the concepts of class and objects,inheritance,etc they inherently still rely on a blocks to define the structure of  the code. The key areas where OO languages assimilate the idea of block structure are as follows:&lt;br /&gt;
*In classes the member functions or methods are implemented using blocks. Each method can be viewed as a block that accepts parameters from an external entity that calls it, performs some processing (that may or may not require the use of data fields of the object) and returns control back with or without a return value.  &lt;br /&gt;
*Blocks are also useful in defining control structures like loops, conditional blocks(e.g. if...else), etc.&lt;br /&gt;
E.g. Java program demonstrating the usage of a while loop .The while loop encloses a group of statements within curly braces signifying a block that may be iterated upon till while condition holds true&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
package hashmap;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.LinkedHashMap;&lt;br /&gt;
import java.util.Map;&lt;br /&gt;
import java.util.Set;&lt;br /&gt;
&lt;br /&gt;
public class ExampleHashMap&lt;br /&gt;
{&lt;br /&gt;
	public static void main(String args[])&lt;br /&gt;
	{&lt;br /&gt;
		LinkedHashMap h = new LinkedHashMap();                          //Creates a LinkedHashMap. Linked HashMaps defines the &lt;br /&gt;
		h.put(&amp;quot;A&amp;quot;,100);                                                 //iteration ordering, which is normally the &lt;br /&gt;
		h.put(&amp;quot;B&amp;quot;,200);                                                 //order in which keys were inserted into the map.&lt;br /&gt;
		h.put(&amp;quot;C&amp;quot;,300);&lt;br /&gt;
		h.put(&amp;quot;D&amp;quot;,400);&lt;br /&gt;
		h.put(&amp;quot;E&amp;quot;,500);&lt;br /&gt;
		&lt;br /&gt;
		Set s = h.entrySet();                                           //Returns a set containing key value pairs&lt;br /&gt;
		Iterator i = s.iterator();                                      &lt;br /&gt;
		&lt;br /&gt;
		while(i.hasNext())&lt;br /&gt;
		{&lt;br /&gt;
			Map.Entry me = (Map.Entry)i.next();                     //Each element in the set has to be casted to Map.Entry &lt;br /&gt;
			System.out.println(me.getKey()+&amp;quot; : &amp;quot;+me.getValue());    //to access the key/value &lt;br /&gt;
		}&lt;br /&gt;
       }&lt;br /&gt;
}&lt;br /&gt;
		&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
*OO languages generally provide inbuilt language support for exception handling. Most OO languages implement exception handling in terms of blocks&lt;br /&gt;
E.g.: Try...catch  blocks in Java&lt;br /&gt;
Here the catch and finally blocks are used to delimit a group of statements that can be called to handle exceptions at runtime. Also, we can see usage of conditional statements if...else in this case.&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
try{&lt;br /&gt;
    System.out.println(&amp;quot;Inside try block of testMethod!&amp;quot;);&lt;br /&gt;
    int p=1000, q=0, i=0;&lt;br /&gt;
    if(p&amp;gt;q){                                               //We can clearly see here that a Divide by Zero exception will occur.&lt;br /&gt;
         i=p/q;&lt;br /&gt;
    }&lt;br /&gt;
    else if(q&amp;gt;p){&lt;br /&gt;
         i=q/p;&lt;br /&gt;
    }&lt;br /&gt;
    else{                                                 //In this case p equals q&lt;br /&gt;
         i=p/q;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
catch(Exception e){&lt;br /&gt;
    System.out.println(&amp;quot;Inside catch block of testMethod!&amp;quot;);&lt;br /&gt;
    System.out.println(“Caution: your trying to Divide a number by Zero...”);&lt;br /&gt;
}&lt;br /&gt;
finally{                                                  &lt;br /&gt;
    System.out.println(&amp;quot;Inside finally block of testMethod!&amp;quot;);&lt;br /&gt;
    System.out.println(“This block executes everytime an exception is caught...”);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
*Certain OO languages like JAVA allow creation of anonymous classes which are basically blocks which don’t have a name but can be instantiated without actually making a separate class.&lt;br /&gt;
E.g.&lt;br /&gt;
Here ActionListener is an interface which has been implemented as anonymous block in the below JAVA example&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
import java.awt.Button;&lt;br /&gt;
&lt;br /&gt;
class Submit()&lt;br /&gt;
{&lt;br /&gt;
       public Submit()&lt;br /&gt;
       {&lt;br /&gt;
              Button button1 = new Button(&amp;quot;Submit&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
              button1.addActionListener(new ActionListener(){&lt;br /&gt;
                     public void actionPerformed (ActionEvent event){&lt;br /&gt;
                            System.out.println(&amp;quot;actionPerfomed is implemented as anonymous class here &amp;amp; is added as a listener to button1&amp;quot;);&lt;br /&gt;
                     }  &lt;br /&gt;
              });&lt;br /&gt;
       }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
*In a pure OO language like Ruby, blocks can also be used to defines closures(blocks, procs &amp;amp; lambdas)&amp;lt;ref&amp;gt;[http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas Sosinski, Robert. &amp;quot;Understanding Ruby Blocks, Procs and Lambdas.&amp;quot; N.p., 21 Dec. 2008. Web. 8 Sept. 2011.]&amp;lt;/ref&amp;gt;&lt;br /&gt;
E.g.&lt;br /&gt;
Given below is a method which yields control to a block in Ruby &amp;amp; also passes parameter to it at the same time.&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
def block_demo&lt;br /&gt;
    yield(&amp;quot;Smith&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
block_demo { |name| puts &amp;quot; Hello #{name} &amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The Output will be&lt;br /&gt;
 Hello Smith &lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Block structured languages had their uses in earlier days when code size was small and the main focus was on efficiency(in terms of space &amp;amp; memory) of programs. However, these programs could not scale in size as maintenance and code complexity became an overhead.&lt;br /&gt;
&lt;br /&gt;
Object Oriented concepts like encapsulation, inheritance(which helps in code re-usability), interfaces &amp;amp; polymorphism allowed better visualization of the software problem and its design to handle this complexity. Moreover, class libraries and frameworks developed in these languages helped abstract the code complexity and allowed rapid program development. This led to a gradual transition from block oriented to object oriented languages for application development.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
#[http://golang.org/ The Go Programming Language by Google for multicore and networked machines]&lt;br /&gt;
#[http://download.oracle.com/javase/tutorial/java/concepts/ Object Oriented Concepts (Java Tutorials)] &lt;br /&gt;
#[http://www.daimi.au.dk/publications/PB/230/PB-230.pdf  Block Structure and Object Oriented Languages by Ole Lehrmann Madsen]&lt;br /&gt;
#[http://portal.acm.org/citation.cfm?id=356671 A Tutorial on Algol 68, by Andrew S. Tanenbaum, in Computing Surveys, Vol. 8, No. 2, June 1976, with Corrigenda (Vol. 9, No. 3, September 1977) ] &lt;br /&gt;
#[http://ruby-lang.org Official Ruby Webpage]&lt;br /&gt;
#[http://www.pascal-programming.info/index.php Pascal programming tutorials]&lt;br /&gt;
#[http://steve-yegge.blogspot.com/2008/05/dynamic-languages-strike-back.html Dynamic Languages Strike Back]&lt;br /&gt;
#[http://amath.colorado.edu/computing/mmm/funcproc.html Functional vs Procedural languages]&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_lm&amp;diff=50778</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1e lm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_lm&amp;diff=50778"/>
		<updated>2011-09-25T22:30:08Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: /* Examples */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
This wiki article discusses two things viz the effectiveness &amp;amp; durability of object oriented languages (like Java, Python, etc) as compared to block structured languages(like C, Pascal, Algol etc) and how block structure can be leveraged in an object oriented language.&lt;br /&gt;
&lt;br /&gt;
==Block structured languages==&lt;br /&gt;
&lt;br /&gt;
===Definition===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Block_(programming) block] is a piece of code consisting of one or more [http://en.wikipedia.org/wiki/Declaration_(computer_science) declarations] and [http://en.wikipedia.org/wiki/Statement_(programming) statements]. A block structured programming language is the one that would allow usage of such blocks &amp;amp; including [http://en.wikipedia.org/wiki/Nesting_(computing) nesting] of such blocks.&lt;br /&gt;
The purpose of blocks is to allow a group of statements to be treated as one.For instance, you might want to have a block of statements which does matrix multiplication.Blocks also narrow the lexical scope of the variables, procedures &amp;amp; functions declared in a block so that they don’t conflict with others(which might be used for a totally different purpose)having the same name elsewhere in the program.For instance, you might want to have a local variable “count” to count the sum of some variables whereas you might have another local variable with the same name “count” to count the number of elements in an array.&lt;br /&gt;
&lt;br /&gt;
===Basic features===&lt;br /&gt;
*Tasks are accomplished using functions. The primary focus is on functions. It involves creating a list of instructions and grouping them into blocks.&lt;br /&gt;
*The functions share global data. The data is stored in data structures which act as information containers. These are global in scope and are passed around between functions using parameters.&lt;br /&gt;
*It uses a top down approach of program design. Here the system is broken down into sub-systems which are recursively refined into additional sub- system levels until we reach base level of granularity which can be implemented by a single function.&lt;br /&gt;
*Functions are implemented as black-boxes with an input and output specification.&lt;br /&gt;
&lt;br /&gt;
===Examples===&lt;br /&gt;
Some of the examples of these languages include Algol, C &amp;amp; Pascal.&lt;br /&gt;
*E.g. Pascal program multiply two numbers.&lt;br /&gt;
Here we can see that the block is delimited by &amp;quot;Begin&amp;quot; &amp;amp; &amp;quot;End&amp;quot; keywords&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
Program Multiplication_Of_Two_Numbers;&lt;br /&gt;
&lt;br /&gt;
Var Product, Multiplier, Multiplicand : Real; &lt;br /&gt;
&lt;br /&gt;
Begin&lt;br /&gt;
    Writeln('Program to multiply 2 numbers:');&lt;br /&gt;
    Writeln;&lt;br /&gt;
    Writeln;&lt;br /&gt;
    Write('Enter the Multilicand: ');&lt;br /&gt;
    Readln(Multiplicand);&lt;br /&gt;
    Writeln;&lt;br /&gt;
    Write('Enter the Multiplier: ');&lt;br /&gt;
    Readln(Multiplier);&lt;br /&gt;
   &lt;br /&gt;
    Product := Multiplicand * Multiplier;&lt;br /&gt;
    Writeln;&lt;br /&gt;
    Writeln('Product of ',Multiplicand,'and ',Multiplier,'equals '+&lt;br /&gt;
           +'to ',Product);&lt;br /&gt;
&lt;br /&gt;
End.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*E.g. C program to read a file line by line.&lt;br /&gt;
Here we can see that the blocks are delimited by the flower braces. We can also see the usage of global variable by the function print_file().&lt;br /&gt;
Also, we can see that in our case we explictly tell the main() not to expect any return value from the function print_file() with the help of keyword &amp;quot;void&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
FILE *ifp;                                                                 /*File pointer which points to the file*/&lt;br /&gt;
&lt;br /&gt;
void print_file(char *filename)&lt;br /&gt;
{&lt;br /&gt;
      char line[80];                                                       /*Assuming each line is 80 character in length*/&lt;br /&gt;
      ifp = fopen(*filename, &amp;quot;r&amp;quot;);                                         /*opens the file for &amp;quot;reading&amp;quot; mode only*/&lt;br /&gt;
&lt;br /&gt;
      while(fgets(line,1,sizeof(line),fp)                                  /*Loop till no more lines to read*/&lt;br /&gt;
      {&lt;br /&gt;
            printf(&amp;quot;%s&amp;quot;, line);                                            /*Prints this line on to the standard output*/&lt;br /&gt;
      }&lt;br /&gt;
      fclose(fp);                                                          /*Closes the file*/&lt;br /&gt;
       &lt;br /&gt;
}&lt;br /&gt;
void main()&lt;br /&gt;
{&lt;br /&gt;
     char *name;&lt;br /&gt;
&lt;br /&gt;
     printf(&amp;quot;Enter the name of the file to be opened: &amp;quot;);&lt;br /&gt;
     scanf(&amp;quot;%s&amp;quot;, name);          &lt;br /&gt;
&lt;br /&gt;
     print_file(name);                                                     /*Function call made to print_file() to print the file*/&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object Oriented Languages:==&lt;br /&gt;
&lt;br /&gt;
===Definition===&lt;br /&gt;
Object oriented languages are built on the object oriented concept. This is the programming paradigm that bundles the data and the methods that access and manage this data into single entity called an '''''Object'''''. This bundling of data and methods allows the object to act as a single entity and have a fluid interface through which it can interact with the outside world. The objects are generally modeled on real world objects.&lt;br /&gt;
&lt;br /&gt;
===Fundamental features of Object Oriented languages===&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_%28computer_science%29 Object]/[http://en.wikipedia.org/wiki/Class_%28computer_programming%29 Class]: Class consists of tightly coupled data fields and set of operations that act on the data. The entire set of data and operations represent a user-defined data type. An object is an instance of a class. Thus a class acts as a collection of objects of similar type. Multiple objects may be created out of a class. The objects may share the operations of the class but have their own set of data fields. An object can be defined by its current state (current value of its data members) and its behavior (the methods that can be called on the object).  &lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Abstraction_(computer_science) Data Abstraction]/[http://en.wikipedia.org/wiki/Encapsulation_%28object-oriented_programming%29 Encapsulation]: The wrapping of data and functions into a single unit (class) is called encapsulation. Encapsulation protects an object from accidental modifications by an external entity since data fields can be accessed only through predetermined interfaces. Encapsulation is implemented using language constructs like private, public, protected, etc. Abstraction involves representing only the essential features of the object and hiding the background details. Classes provide abstraction by hiding the data attributes and providing functions that provide a controlled view of the object to an external entity.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Inheritance_%28object-oriented_programming%29 Inheritance]:  Inheritance is the ability of a class to inherit partly or completely the features of another class. It allows the hierarchical classification of objects.For example, the bird ''Peacock'' is a part of the class ''Flying bird'' which again is part of the class ''Bird''. Inheritance allows the reusability of code. Pre-existing classes can be reused by extending them and adding new functionality to create sub classes without modifying the original classes. The sub class will then have the combined features of both classes. This allows a programmer to tailor a new class from an existing class without introducing any undesirable side effects in the base classes.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Interface_%28Java%29 Interface]: Interface is an abstract type containing no data but only a list of method definitions. A class that has all the methods defined in the interface is said to have implemented the method interface. Interfaces allow classes to define how they will communicate with an external entity.  By implementing an interface, a class promises an external entity using the interface that it will provide the functionality specified by the interface.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]/[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding]: This is the ability of a variable, method or an object to take more than one forms. This enables an objects of different types to respond to same method or property call but in its own type specific way. For example consider overloading the + operator, so that when called on strings, it concatenates the strings while for numbers it adds them up. Polymorphism also allows objects with different internal structures to share the same external interface. This helps in dynamic binding. Dynamic binding means that the code executed for a given function call is not known until actual runtime. For example, in a dynamically typed language a variable can hold an object irrespective of its data type. Hence a function call associated with a reference (variable) depends on the dynamic type of that reference (variable) i.e. the current object referenced by the variable.&lt;br /&gt;
&lt;br /&gt;
===Examples===&lt;br /&gt;
The below languages implement different degrees of OO  features&amp;lt;ref&amp;gt;[http://www.jvoegele.com/software/langcomp.html Voegele, Jason. &amp;quot;Programming Language Comparison.&amp;quot; Web.]&amp;lt;/ref&amp;gt;&lt;br /&gt;
*Ruby, Scala, Smalltalk, Eiffel are said to be purely OO languages. Here even primitives are implemented as OO objects.&lt;br /&gt;
*Java, C++, C#, VB.NET, Python are OO languages but have some degree of procedural elements&lt;br /&gt;
*Visual Basic, PHP, Perl are basically procedural languages with some OO features.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
E.g. C++ program demonstrating the use of a class .&lt;br /&gt;
The class CRectangle represents a rectangle and has data members x,y which represent the length and breadth (ie: properties) of the rectangle. The functions set_values() and area() allow us to access these data members in a controlled manner. The class object has been instatiated in function main&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
&lt;br /&gt;
class CRectangle {&lt;br /&gt;
    int x, y;&lt;br /&gt;
  public:&lt;br /&gt;
    void set_values (int,int);&lt;br /&gt;
    int area () ;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
void CRectangle::set_values (int a, int b) {&lt;br /&gt;
  x = a;&lt;br /&gt;
  y = b;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void CRectangle::area() {&lt;br /&gt;
  return x*y;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main () {&lt;br /&gt;
  CRectangle rect;&lt;br /&gt;
  rect.set_values (3,4);&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;area: &amp;quot; &amp;lt;&amp;lt; rect.area();&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
 &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Need for Object Oriented Languages==&lt;br /&gt;
Block structured languages follow a task-centric approach rather than a data-centric approach. This resulted in certain flaws:&lt;br /&gt;
#Structured programming follows a hierarchy of structures. However in large programs data is generally in the form of a network of structures. This resulted in a lot of [http://en.wiktionary.org/wiki/tramp_data tramp data] being passed around.&amp;lt;ref&amp;gt; [http://en.wikipedia.org/wiki/Structured_programming#Object-oriented_comparison Object Oriented Comparison. Web. 8 June 2011.] &amp;lt;/ref&amp;gt;&lt;br /&gt;
#As the program size increased the amount of data being passed around increased proportionately. Ensuring parameters are correctly passed to the correct subprogram becomes a major issue.&lt;br /&gt;
#In case of large programs even a small change resulted in massive ripple-effect with changing multiple sub modules to propagate the new data through the program's hierarchy.&lt;br /&gt;
#Structured program shared a lot of global data. The acces to the data was uncontrolled and unpredictable. There was no control over who had access to what.&lt;br /&gt;
#Structured program suffered from memory leaks.&amp;lt;ref&amp;gt;[http://www.cprogramming.com/debugging/valgrind.html Using Valgrind to Find Memory Leaks and Invalid Memory Use]&amp;lt;/ref&amp;gt; This was because anyone could create global structures and there was no responsibility on the creator to deallocate the memory.&lt;br /&gt;
The main motivating factor for object-oriented approach is to remove these flaws of block structured languages. OO languages treat data as a critical element and do not allow it to flow freely around the system. The data is tied to the functions with operate on and prevent it from accidental modification from external functions.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Object-oriented languages over Block structured languages==&lt;br /&gt;
#Object oriented approach follows a data centered approach. It is possible to map objects in the problem domain to those in the program. This helps the programmer visualize the program in terms of real world objects and which leads to a much better design. Also it allows more details of a model to be captured in implementable form.&amp;lt;ref&amp;gt;Balaguruswamy, E. Object Oriented Programming with C++. New Delhi: Tata Mcgraw-Hill, 2008. Print.&amp;lt;/ref&amp;gt;&lt;br /&gt;
#The principle of data hiding helps in building secure programs. The data fields of an object can be accessed only through the interface defined by its publicly accessible methods. Such controlled access prevents any inadvertent change to object data by external modules.&amp;lt;ref&amp;gt;[http://www.developer.com/design/article.php/3387591/Hiding-Data-within-Object-Oriented-Programming.htm Weisfeld, Matt. &amp;quot;Hiding Data within Object-Oriented Programming.&amp;quot; N.p., 29 July 2004. Web.]&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Mapping real world entities into objects help reduce and easily manage software complexity . Also it helps when systems need to be upgraded to larger systems.&amp;lt;ref&amp;gt;[http://www.artima.com/interfacedesign/SigObjects.html &amp;quot;Interface Design by Bill Venners.&amp;quot; Web.]&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Objects act as black boxes with internal structure and well defined interfaces. Hence adding new functionality becomes easier, since we can easily isolate the objects/classes that need to be modified or extended. The low coupling between objects allows an object to be modified without any effect on the other objects accessing it so long as the interface through which they interact remain untouched.&lt;br /&gt;
#Most modern object oriented languages provides a load of class libraries and frameworks. These are much easier to use as compared to library functions present in block structured languages. The frameworks help in rapid software development and easier testing and debugging thus leading to increased productivity.&amp;lt;ref&amp;gt;[http://www.cs.bgsu.edu/maner/domains/RAD.htm Maner, Walter. RAPID APPLICATION DEVELOPMENT. N.p., 15 Mar. 1997. Web.]&amp;lt;/ref&amp;gt; &amp;lt;ref&amp;gt;[http://www.chillarege.com/authwork/TestingBestPractice.pdf Chillarege, Ram. &amp;quot;Software Testing Best Practices.&amp;quot; N.p., 26 Apr. 1999. Web.]&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Message passing techniques (ex: interfaces in JAVA) help separate implementation of an object from its externally visible interface. Multiple classes of objects may implement the same interface. In such cases an object can be replaced by another object without affecting external entities dependent on that object so long as the interface is untouched. This results in low coupling.&lt;br /&gt;
#Object oriented design provides much better memory management. Recent OO languages sport automated garbage handling. Even for languages without garbage collection&amp;lt;ref&amp;gt;[http://javarevisited.blogspot.com/2011/04/garbage-collection-in-java.html Paul, Javin. &amp;quot;Garbage Collection in Java.&amp;quot; How Garbage Collection Works In Java. N.p., 11 Apr. 2011. Web. ]&amp;lt;/ref&amp;gt;, memory deallocation of data members can be handled in the destructors of the objects. But in case of block structured languages (like C), it is the programmers responsibility to take care of releasing the dynamically allocated memory. Also the data structures tend to be global in scope and persist beyond the scope of the functions calling them. Hence it becomes difficult to fix responsibility on functions to deallocate them. This leads to memory leaks.&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Object-Oriented Languages==&lt;br /&gt;
Although object oriented languages sport some very advantageous features as compared to block structured languages, they do have some drawbacks. &lt;br /&gt;
#Economy of language features: The addition of OO features to a language and the additional syntax needed to implement these features makes it bulky. The complexity of these languages make them daunting to learn and master.&amp;lt;ref name=&amp;quot;badEnggProperties&amp;quot;&amp;gt;[http://lucacardelli.name/Papers/BadPropertiesOfOO.html Bad Engineering Properties of Object-Oriented Languages by Luca Cardelli]&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Economy of execution: OO style is intrinsically less efficient as compared to block structured style. In procedural languages, type checking occurs at compile time. Hence no tests are needed at runtime for pointer dereferencing for method calls.In OO style each routine is supposed to be a virtual method. This introduces indirections in the form of method tables during for dereferencing.&lt;br /&gt;
#Also many OO languages include automatic garbage collection (e.g. Java) &amp;amp; hence it might slow down the performance.&amp;lt;ref&amp;gt;[http://www.jelovic.com/articles/why_java_is_slow.htm Why Java is slow Jelovic, Dejan. &amp;quot;Why Java Is Slow.&amp;quot; Why Java Will Always Be Slower Than C++. N.p., 18 Dec. 2009. Web.]&amp;lt;/ref&amp;gt;. This along with the overhead of creation and destruction of objects degrades performance in resource intensive systems such as embedded systems and real time systems.&amp;lt;ref&amp;gt;[http://csjournals.com/IJCSC/PDF1-2/9..pdf Object Oriented Vs Procedural Programming in Embedded System ]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Usage of block-structures in Object Oriented languages==&lt;br /&gt;
OO languages have evolved from block structured languages. Though OO languages use the concepts of class and objects,inheritance,etc they inherently still rely on a blocks to define the structure of  the code. The key areas where OO languages assimilate the idea of block structure are as follows:&lt;br /&gt;
*In classes the member functions or methods are implemented using blocks. Each method can be viewed as a block that accepts parameters from an external entity that calls it, performs some processing (that may or may not require the use of data fields of the object) and returns control back with or without a return value.  &lt;br /&gt;
*Blocks are also useful in defining control structures like loops, conditional blocks(e.g. if...else), etc.&lt;br /&gt;
E.g. Java program demonstrating the usage of a while loop .The while loop encloses a group of statements within curly braces signifying a block that may be iterated upon till while condition holds true&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
package hashmap;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.LinkedHashMap;&lt;br /&gt;
import java.util.Map;&lt;br /&gt;
import java.util.Set;&lt;br /&gt;
&lt;br /&gt;
public class ExampleHashMap&lt;br /&gt;
{&lt;br /&gt;
	public static void main(String args[])&lt;br /&gt;
	{&lt;br /&gt;
		LinkedHashMap h = new LinkedHashMap();                          //Creates a LinkedHashMap. Linked HashMaps defines the &lt;br /&gt;
		h.put(&amp;quot;A&amp;quot;,100);                                                 //iteration ordering, which is normally the &lt;br /&gt;
		h.put(&amp;quot;B&amp;quot;,200);                                                 //order in which keys were inserted into the map.&lt;br /&gt;
		h.put(&amp;quot;C&amp;quot;,300);&lt;br /&gt;
		h.put(&amp;quot;D&amp;quot;,400);&lt;br /&gt;
		h.put(&amp;quot;E&amp;quot;,500);&lt;br /&gt;
		&lt;br /&gt;
		Set s = h.entrySet();                                           //Returns a set containing key value pairs&lt;br /&gt;
		Iterator i = s.iterator();                                      &lt;br /&gt;
		&lt;br /&gt;
		while(i.hasNext())&lt;br /&gt;
		{&lt;br /&gt;
			Map.Entry me = (Map.Entry)i.next();                     //Each element in the set has to be casted to Map.Entry &lt;br /&gt;
			System.out.println(me.getKey()+&amp;quot; : &amp;quot;+me.getValue());    //to access the key/value &lt;br /&gt;
		}&lt;br /&gt;
       }&lt;br /&gt;
}&lt;br /&gt;
		&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
*OO languages generally provide inbuilt language support for exception handling. Most OO languages implement exception handling in terms of blocks&lt;br /&gt;
E.g.: Try...catch  blocks in Java&lt;br /&gt;
Here the catch and finally blocks are used to delimit a group of statements that can be called to handle exceptions at runtime. Also, we can see usage of conditional statements if...else in this case.&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
try{&lt;br /&gt;
    System.out.println(&amp;quot;Inside try block of testMethod!&amp;quot;);&lt;br /&gt;
    int p=1000, q=0, i=0;&lt;br /&gt;
    if(p&amp;gt;q){                                               //We can clearly see here that a Divide by Zero exception will occur.&lt;br /&gt;
         i=p/q;&lt;br /&gt;
    }&lt;br /&gt;
    else if(q&amp;gt;p){&lt;br /&gt;
         i=q/p;&lt;br /&gt;
    }&lt;br /&gt;
    else{                                                 //In this case p equals q&lt;br /&gt;
         i=p/q;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
catch(Exception e){&lt;br /&gt;
    System.out.println(&amp;quot;Inside catch block of testMethod!&amp;quot;);&lt;br /&gt;
    System.out.println(“Caution: your trying to Divide a number by Zero...”);&lt;br /&gt;
}&lt;br /&gt;
finally{                                                  &lt;br /&gt;
    System.out.println(&amp;quot;Inside finally block of testMethod!&amp;quot;);&lt;br /&gt;
    System.out.println(“This block executes everytime an exception is caught...”);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
*Certain OO languages like JAVA allow creation of anonymous classes which are basically blocks which don’t have a name but can be instantiated without actually making a separate class.&lt;br /&gt;
E.g.&lt;br /&gt;
Here ActionListener is an interface which has been implemented as anonymous block in the below JAVA example&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
import java.awt.Button;&lt;br /&gt;
&lt;br /&gt;
class Submit()&lt;br /&gt;
{&lt;br /&gt;
       public Submit()&lt;br /&gt;
       {&lt;br /&gt;
              Button button1 = new Button(&amp;quot;Submit&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
              button1.addActionListener(new ActionListener(){&lt;br /&gt;
                     public void actionPerformed (ActionEvent event){&lt;br /&gt;
                            System.out.println(&amp;quot;actionPerfomed is implemented as anonymous class here &amp;amp; is added as a listener to button1&amp;quot;);&lt;br /&gt;
                     }  &lt;br /&gt;
              });&lt;br /&gt;
       }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
*In a pure OO language like Ruby, blocks can also be used to defines closures(blocks, procs &amp;amp; lambdas)&amp;lt;ref&amp;gt;[http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas Sosinski, Robert. &amp;quot;Understanding Ruby Blocks, Procs and Lambdas.&amp;quot; N.p., 21 Dec. 2008. Web. 8 Sept. 2011.]&amp;lt;/ref&amp;gt;&lt;br /&gt;
E.g.&lt;br /&gt;
Given below is a method which yields control to a block in Ruby &amp;amp; also passes parameter to it at the same time.&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
def block_demo&lt;br /&gt;
    yield(&amp;quot;Smith&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
block_demo { |name| puts &amp;quot; Hello #{name} &amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The Output will be&lt;br /&gt;
 Hello Smith &lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Block structured languages had their uses in earlier days when code size was small and the main focus was on efficiency(in terms of space &amp;amp; memory) of programs. However, these programs could not scale in size as maintenance and code complexity became an overhead.&lt;br /&gt;
&lt;br /&gt;
Object Oriented concepts like encapsulation, inheritance(which helps in code re-usability), interfaces &amp;amp; polymorphism allowed better visualization of the software problem and its design to handle this complexity. Moreover, class libraries and frameworks developed in these languages helped abstract the code complexity and allowed rapid program development. This led to a gradual transition from block oriented to object oriented languages for application development.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
#[http://golang.org/ The Go Programming Language by Google for multicore and networked machines]&lt;br /&gt;
#[http://download.oracle.com/javase/tutorial/java/concepts/ Object Oriented Concepts (Java Tutorials)] &lt;br /&gt;
#[http://www.daimi.au.dk/publications/PB/230/PB-230.pdf  Block Structure and Object Oriented Languages by Ole Lehrmann Madsen]&lt;br /&gt;
#[http://portal.acm.org/citation.cfm?id=356671 A Tutorial on Algol 68, by Andrew S. Tanenbaum, in Computing Surveys, Vol. 8, No. 2, June 1976, with Corrigenda (Vol. 9, No. 3, September 1977) ] &lt;br /&gt;
#[http://ruby-lang.org Official Ruby Webpage]&lt;br /&gt;
#[http://www.pascal-programming.info/index.php Pascal programming tutorials]&lt;br /&gt;
#[http://steve-yegge.blogspot.com/2008/05/dynamic-languages-strike-back.html Dynamic Languages Strike Back]&lt;br /&gt;
#[http://amath.colorado.edu/computing/mmm/funcproc.html Functional vs Procedural languages]&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_lm&amp;diff=50776</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1e lm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_lm&amp;diff=50776"/>
		<updated>2011-09-25T22:29:34Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: /* Examples */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
This wiki article discusses two things viz the effectiveness &amp;amp; durability of object oriented languages (like Java, Python, etc) as compared to block structured languages(like C, Pascal, Algol etc) and how block structure can be leveraged in an object oriented language.&lt;br /&gt;
&lt;br /&gt;
==Block structured languages==&lt;br /&gt;
&lt;br /&gt;
===Definition===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Block_(programming) block] is a piece of code consisting of one or more [http://en.wikipedia.org/wiki/Declaration_(computer_science) declarations] and [http://en.wikipedia.org/wiki/Statement_(programming) statements]. A block structured programming language is the one that would allow usage of such blocks &amp;amp; including [http://en.wikipedia.org/wiki/Nesting_(computing) nesting] of such blocks.&lt;br /&gt;
The purpose of blocks is to allow a group of statements to be treated as one.For instance, you might want to have a block of statements which does matrix multiplication.Blocks also narrow the lexical scope of the variables, procedures &amp;amp; functions declared in a block so that they don’t conflict with others(which might be used for a totally different purpose)having the same name elsewhere in the program.For instance, you might want to have a local variable “count” to count the sum of some variables whereas you might have another local variable with the same name “count” to count the number of elements in an array.&lt;br /&gt;
&lt;br /&gt;
===Basic features===&lt;br /&gt;
*Tasks are accomplished using functions. The primary focus is on functions. It involves creating a list of instructions and grouping them into blocks.&lt;br /&gt;
*The functions share global data. The data is stored in data structures which act as information containers. These are global in scope and are passed around between functions using parameters.&lt;br /&gt;
*It uses a top down approach of program design. Here the system is broken down into sub-systems which are recursively refined into additional sub- system levels until we reach base level of granularity which can be implemented by a single function.&lt;br /&gt;
*Functions are implemented as black-boxes with an input and output specification.&lt;br /&gt;
&lt;br /&gt;
===Examples===&lt;br /&gt;
Some of the examples of these languages include Algol, C &amp;amp; Pascal.&lt;br /&gt;
*E.g. Pascal program multiply two numbers.&lt;br /&gt;
Here we can see that the block is delimited by &amp;quot;Begin&amp;quot; &amp;amp; &amp;quot;End&amp;quot; keywords&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
Program Multiplication_Of_Two_Numbers;&lt;br /&gt;
&lt;br /&gt;
Var Product, Multiplier, Multiplicand : Real; &lt;br /&gt;
&lt;br /&gt;
Begin&lt;br /&gt;
    Writeln('Program to multiply 2 numbers:');&lt;br /&gt;
    Writeln;&lt;br /&gt;
    Writeln;&lt;br /&gt;
    Write('Enter the Multilicand: ');&lt;br /&gt;
    Readln(Multiplicand);&lt;br /&gt;
    Writeln;&lt;br /&gt;
    Write('Enter the Multiplier: ');&lt;br /&gt;
    Readln(Multiplier);&lt;br /&gt;
   &lt;br /&gt;
    Product := Multiplicand * Multiplier;&lt;br /&gt;
    Writeln;&lt;br /&gt;
    Writeln('Product of ',Multiplicand,'and ',Multiplier,'equals '+&lt;br /&gt;
           +'to ',Product);&lt;br /&gt;
&lt;br /&gt;
End.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*E.g. C program to read a file line by line.&lt;br /&gt;
Here we can see that the blocks are delimited by the flower braces. We can also see the usage of global variable by the function print_file().&lt;br /&gt;
Also, we can see that in our case we explictly tell the main() not to expect any return value from the function print_file() with the help of keyword &amp;quot;void&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
FILE *ifp;                                                                 /*File pointer which points to the file*/&lt;br /&gt;
&lt;br /&gt;
void print_file(char *filename)&lt;br /&gt;
{&lt;br /&gt;
      char line[80];                                                       /*Assuming each line is 80 character in length*/&lt;br /&gt;
      ifp = fopen(*filename, &amp;quot;r&amp;quot;);                                         /*opens the file for &amp;quot;reading&amp;quot; mode only*/&lt;br /&gt;
&lt;br /&gt;
      while(fgets(line,1,sizeof(line),fp)                                  /*Loop till no more lines to read*/&lt;br /&gt;
      {&lt;br /&gt;
            printf(&amp;quot;%s&amp;quot;, line);                                            /*Prints this line on to the standard output*/&lt;br /&gt;
      }&lt;br /&gt;
      fclose(fp);                                                          /*Closes the file*/&lt;br /&gt;
       &lt;br /&gt;
}&lt;br /&gt;
void main()&lt;br /&gt;
{&lt;br /&gt;
     char *name;&lt;br /&gt;
&lt;br /&gt;
     printf(&amp;quot;Enter the name of the file to be opened: &amp;quot;);&lt;br /&gt;
     scanf(&amp;quot;%s&amp;quot;, name);          &lt;br /&gt;
&lt;br /&gt;
     print_file(name);                                                     /*Function call made to print_file() to print the file*/&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object Oriented Languages:==&lt;br /&gt;
&lt;br /&gt;
===Definition===&lt;br /&gt;
Object oriented languages are built on the object oriented concept. This is the programming paradigm that bundles the data and the methods that access and manage this data into single entity called an '''''Object'''''. This bundling of data and methods allows the object to act as a single entity and have a fluid interface through which it can interact with the outside world. The objects are generally modeled on real world objects.&lt;br /&gt;
&lt;br /&gt;
===Fundamental features of Object Oriented languages===&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_%28computer_science%29 Object]/[http://en.wikipedia.org/wiki/Class_%28computer_programming%29 Class]: Class consists of tightly coupled data fields and set of operations that act on the data. The entire set of data and operations represent a user-defined data type. An object is an instance of a class. Thus a class acts as a collection of objects of similar type. Multiple objects may be created out of a class. The objects may share the operations of the class but have their own set of data fields. An object can be defined by its current state (current value of its data members) and its behavior (the methods that can be called on the object).  &lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Abstraction_(computer_science) Data Abstraction]/[http://en.wikipedia.org/wiki/Encapsulation_%28object-oriented_programming%29 Encapsulation]: The wrapping of data and functions into a single unit (class) is called encapsulation. Encapsulation protects an object from accidental modifications by an external entity since data fields can be accessed only through predetermined interfaces. Encapsulation is implemented using language constructs like private, public, protected, etc. Abstraction involves representing only the essential features of the object and hiding the background details. Classes provide abstraction by hiding the data attributes and providing functions that provide a controlled view of the object to an external entity.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Inheritance_%28object-oriented_programming%29 Inheritance]:  Inheritance is the ability of a class to inherit partly or completely the features of another class. It allows the hierarchical classification of objects.For example, the bird ''Peacock'' is a part of the class ''Flying bird'' which again is part of the class ''Bird''. Inheritance allows the reusability of code. Pre-existing classes can be reused by extending them and adding new functionality to create sub classes without modifying the original classes. The sub class will then have the combined features of both classes. This allows a programmer to tailor a new class from an existing class without introducing any undesirable side effects in the base classes.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Interface_%28Java%29 Interface]: Interface is an abstract type containing no data but only a list of method definitions. A class that has all the methods defined in the interface is said to have implemented the method interface. Interfaces allow classes to define how they will communicate with an external entity.  By implementing an interface, a class promises an external entity using the interface that it will provide the functionality specified by the interface.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]/[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding]: This is the ability of a variable, method or an object to take more than one forms. This enables an objects of different types to respond to same method or property call but in its own type specific way. For example consider overloading the + operator, so that when called on strings, it concatenates the strings while for numbers it adds them up. Polymorphism also allows objects with different internal structures to share the same external interface. This helps in dynamic binding. Dynamic binding means that the code executed for a given function call is not known until actual runtime. For example, in a dynamically typed language a variable can hold an object irrespective of its data type. Hence a function call associated with a reference (variable) depends on the dynamic type of that reference (variable) i.e. the current object referenced by the variable.&lt;br /&gt;
&lt;br /&gt;
===Examples===&lt;br /&gt;
The below languages implement different degrees of OO  features&amp;lt;ref&amp;gt;[http://www.jvoegele.com/software/langcomp.html Voegele, Jason. &amp;quot;Programming Language Comparison.&amp;quot; Web.]&amp;lt;/ref&amp;gt;&lt;br /&gt;
*Ruby, Scala, Smalltalk, Eiffel are said to be purely OO languages. Here even primitives are implemented as OO objects.&lt;br /&gt;
*Java, C++, C#, VB.NET, Python are OO languages but have some degree of procedural elements&lt;br /&gt;
*Visual Basic, PHP, Perl are basically procedural languages with some OO features.&lt;br /&gt;
&lt;br /&gt;
*E.g. C++ program demonstrating the use of a class .&lt;br /&gt;
The class CRectangle represents a rectangle and has data members x,y which represent the length and breadth (ie: properties) of the rectangle. The functions set_values() and area() allow us to access these data members in a controlled manner. The class object has been instatiated in function main&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
&lt;br /&gt;
class CRectangle {&lt;br /&gt;
    int x, y;&lt;br /&gt;
  public:&lt;br /&gt;
    void set_values (int,int);&lt;br /&gt;
    int area () ;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
void CRectangle::set_values (int a, int b) {&lt;br /&gt;
  x = a;&lt;br /&gt;
  y = b;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void CRectangle::area() {&lt;br /&gt;
  return x*y;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main () {&lt;br /&gt;
  CRectangle rect;&lt;br /&gt;
  rect.set_values (3,4);&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;area: &amp;quot; &amp;lt;&amp;lt; rect.area();&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
 &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Need for Object Oriented Languages==&lt;br /&gt;
Block structured languages follow a task-centric approach rather than a data-centric approach. This resulted in certain flaws:&lt;br /&gt;
#Structured programming follows a hierarchy of structures. However in large programs data is generally in the form of a network of structures. This resulted in a lot of [http://en.wiktionary.org/wiki/tramp_data tramp data] being passed around.&amp;lt;ref&amp;gt; [http://en.wikipedia.org/wiki/Structured_programming#Object-oriented_comparison Object Oriented Comparison. Web. 8 June 2011.] &amp;lt;/ref&amp;gt;&lt;br /&gt;
#As the program size increased the amount of data being passed around increased proportionately. Ensuring parameters are correctly passed to the correct subprogram becomes a major issue.&lt;br /&gt;
#In case of large programs even a small change resulted in massive ripple-effect with changing multiple sub modules to propagate the new data through the program's hierarchy.&lt;br /&gt;
#Structured program shared a lot of global data. The acces to the data was uncontrolled and unpredictable. There was no control over who had access to what.&lt;br /&gt;
#Structured program suffered from memory leaks.&amp;lt;ref&amp;gt;[http://www.cprogramming.com/debugging/valgrind.html Using Valgrind to Find Memory Leaks and Invalid Memory Use]&amp;lt;/ref&amp;gt; This was because anyone could create global structures and there was no responsibility on the creator to deallocate the memory.&lt;br /&gt;
The main motivating factor for object-oriented approach is to remove these flaws of block structured languages. OO languages treat data as a critical element and do not allow it to flow freely around the system. The data is tied to the functions with operate on and prevent it from accidental modification from external functions.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Object-oriented languages over Block structured languages==&lt;br /&gt;
#Object oriented approach follows a data centered approach. It is possible to map objects in the problem domain to those in the program. This helps the programmer visualize the program in terms of real world objects and which leads to a much better design. Also it allows more details of a model to be captured in implementable form.&amp;lt;ref&amp;gt;Balaguruswamy, E. Object Oriented Programming with C++. New Delhi: Tata Mcgraw-Hill, 2008. Print.&amp;lt;/ref&amp;gt;&lt;br /&gt;
#The principle of data hiding helps in building secure programs. The data fields of an object can be accessed only through the interface defined by its publicly accessible methods. Such controlled access prevents any inadvertent change to object data by external modules.&amp;lt;ref&amp;gt;[http://www.developer.com/design/article.php/3387591/Hiding-Data-within-Object-Oriented-Programming.htm Weisfeld, Matt. &amp;quot;Hiding Data within Object-Oriented Programming.&amp;quot; N.p., 29 July 2004. Web.]&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Mapping real world entities into objects help reduce and easily manage software complexity . Also it helps when systems need to be upgraded to larger systems.&amp;lt;ref&amp;gt;[http://www.artima.com/interfacedesign/SigObjects.html &amp;quot;Interface Design by Bill Venners.&amp;quot; Web.]&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Objects act as black boxes with internal structure and well defined interfaces. Hence adding new functionality becomes easier, since we can easily isolate the objects/classes that need to be modified or extended. The low coupling between objects allows an object to be modified without any effect on the other objects accessing it so long as the interface through which they interact remain untouched.&lt;br /&gt;
#Most modern object oriented languages provides a load of class libraries and frameworks. These are much easier to use as compared to library functions present in block structured languages. The frameworks help in rapid software development and easier testing and debugging thus leading to increased productivity.&amp;lt;ref&amp;gt;[http://www.cs.bgsu.edu/maner/domains/RAD.htm Maner, Walter. RAPID APPLICATION DEVELOPMENT. N.p., 15 Mar. 1997. Web.]&amp;lt;/ref&amp;gt; &amp;lt;ref&amp;gt;[http://www.chillarege.com/authwork/TestingBestPractice.pdf Chillarege, Ram. &amp;quot;Software Testing Best Practices.&amp;quot; N.p., 26 Apr. 1999. Web.]&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Message passing techniques (ex: interfaces in JAVA) help separate implementation of an object from its externally visible interface. Multiple classes of objects may implement the same interface. In such cases an object can be replaced by another object without affecting external entities dependent on that object so long as the interface is untouched. This results in low coupling.&lt;br /&gt;
#Object oriented design provides much better memory management. Recent OO languages sport automated garbage handling. Even for languages without garbage collection&amp;lt;ref&amp;gt;[http://javarevisited.blogspot.com/2011/04/garbage-collection-in-java.html Paul, Javin. &amp;quot;Garbage Collection in Java.&amp;quot; How Garbage Collection Works In Java. N.p., 11 Apr. 2011. Web. ]&amp;lt;/ref&amp;gt;, memory deallocation of data members can be handled in the destructors of the objects. But in case of block structured languages (like C), it is the programmers responsibility to take care of releasing the dynamically allocated memory. Also the data structures tend to be global in scope and persist beyond the scope of the functions calling them. Hence it becomes difficult to fix responsibility on functions to deallocate them. This leads to memory leaks.&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Object-Oriented Languages==&lt;br /&gt;
Although object oriented languages sport some very advantageous features as compared to block structured languages, they do have some drawbacks. &lt;br /&gt;
#Economy of language features: The addition of OO features to a language and the additional syntax needed to implement these features makes it bulky. The complexity of these languages make them daunting to learn and master.&amp;lt;ref name=&amp;quot;badEnggProperties&amp;quot;&amp;gt;[http://lucacardelli.name/Papers/BadPropertiesOfOO.html Bad Engineering Properties of Object-Oriented Languages by Luca Cardelli]&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Economy of execution: OO style is intrinsically less efficient as compared to block structured style. In procedural languages, type checking occurs at compile time. Hence no tests are needed at runtime for pointer dereferencing for method calls.In OO style each routine is supposed to be a virtual method. This introduces indirections in the form of method tables during for dereferencing.&lt;br /&gt;
#Also many OO languages include automatic garbage collection (e.g. Java) &amp;amp; hence it might slow down the performance.&amp;lt;ref&amp;gt;[http://www.jelovic.com/articles/why_java_is_slow.htm Why Java is slow Jelovic, Dejan. &amp;quot;Why Java Is Slow.&amp;quot; Why Java Will Always Be Slower Than C++. N.p., 18 Dec. 2009. Web.]&amp;lt;/ref&amp;gt;. This along with the overhead of creation and destruction of objects degrades performance in resource intensive systems such as embedded systems and real time systems.&amp;lt;ref&amp;gt;[http://csjournals.com/IJCSC/PDF1-2/9..pdf Object Oriented Vs Procedural Programming in Embedded System ]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Usage of block-structures in Object Oriented languages==&lt;br /&gt;
OO languages have evolved from block structured languages. Though OO languages use the concepts of class and objects,inheritance,etc they inherently still rely on a blocks to define the structure of  the code. The key areas where OO languages assimilate the idea of block structure are as follows:&lt;br /&gt;
*In classes the member functions or methods are implemented using blocks. Each method can be viewed as a block that accepts parameters from an external entity that calls it, performs some processing (that may or may not require the use of data fields of the object) and returns control back with or without a return value.  &lt;br /&gt;
*Blocks are also useful in defining control structures like loops, conditional blocks(e.g. if...else), etc.&lt;br /&gt;
E.g. Java program demonstrating the usage of a while loop .The while loop encloses a group of statements within curly braces signifying a block that may be iterated upon till while condition holds true&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
package hashmap;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.LinkedHashMap;&lt;br /&gt;
import java.util.Map;&lt;br /&gt;
import java.util.Set;&lt;br /&gt;
&lt;br /&gt;
public class ExampleHashMap&lt;br /&gt;
{&lt;br /&gt;
	public static void main(String args[])&lt;br /&gt;
	{&lt;br /&gt;
		LinkedHashMap h = new LinkedHashMap();                          //Creates a LinkedHashMap. Linked HashMaps defines the &lt;br /&gt;
		h.put(&amp;quot;A&amp;quot;,100);                                                 //iteration ordering, which is normally the &lt;br /&gt;
		h.put(&amp;quot;B&amp;quot;,200);                                                 //order in which keys were inserted into the map.&lt;br /&gt;
		h.put(&amp;quot;C&amp;quot;,300);&lt;br /&gt;
		h.put(&amp;quot;D&amp;quot;,400);&lt;br /&gt;
		h.put(&amp;quot;E&amp;quot;,500);&lt;br /&gt;
		&lt;br /&gt;
		Set s = h.entrySet();                                           //Returns a set containing key value pairs&lt;br /&gt;
		Iterator i = s.iterator();                                      &lt;br /&gt;
		&lt;br /&gt;
		while(i.hasNext())&lt;br /&gt;
		{&lt;br /&gt;
			Map.Entry me = (Map.Entry)i.next();                     //Each element in the set has to be casted to Map.Entry &lt;br /&gt;
			System.out.println(me.getKey()+&amp;quot; : &amp;quot;+me.getValue());    //to access the key/value &lt;br /&gt;
		}&lt;br /&gt;
       }&lt;br /&gt;
}&lt;br /&gt;
		&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
*OO languages generally provide inbuilt language support for exception handling. Most OO languages implement exception handling in terms of blocks&lt;br /&gt;
E.g.: Try...catch  blocks in Java&lt;br /&gt;
Here the catch and finally blocks are used to delimit a group of statements that can be called to handle exceptions at runtime. Also, we can see usage of conditional statements if...else in this case.&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
try{&lt;br /&gt;
    System.out.println(&amp;quot;Inside try block of testMethod!&amp;quot;);&lt;br /&gt;
    int p=1000, q=0, i=0;&lt;br /&gt;
    if(p&amp;gt;q){                                               //We can clearly see here that a Divide by Zero exception will occur.&lt;br /&gt;
         i=p/q;&lt;br /&gt;
    }&lt;br /&gt;
    else if(q&amp;gt;p){&lt;br /&gt;
         i=q/p;&lt;br /&gt;
    }&lt;br /&gt;
    else{                                                 //In this case p equals q&lt;br /&gt;
         i=p/q;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
catch(Exception e){&lt;br /&gt;
    System.out.println(&amp;quot;Inside catch block of testMethod!&amp;quot;);&lt;br /&gt;
    System.out.println(“Caution: your trying to Divide a number by Zero...”);&lt;br /&gt;
}&lt;br /&gt;
finally{                                                  &lt;br /&gt;
    System.out.println(&amp;quot;Inside finally block of testMethod!&amp;quot;);&lt;br /&gt;
    System.out.println(“This block executes everytime an exception is caught...”);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
*Certain OO languages like JAVA allow creation of anonymous classes which are basically blocks which don’t have a name but can be instantiated without actually making a separate class.&lt;br /&gt;
E.g.&lt;br /&gt;
Here ActionListener is an interface which has been implemented as anonymous block in the below JAVA example&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
import java.awt.Button;&lt;br /&gt;
&lt;br /&gt;
class Submit()&lt;br /&gt;
{&lt;br /&gt;
       public Submit()&lt;br /&gt;
       {&lt;br /&gt;
              Button button1 = new Button(&amp;quot;Submit&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
              button1.addActionListener(new ActionListener(){&lt;br /&gt;
                     public void actionPerformed (ActionEvent event){&lt;br /&gt;
                            System.out.println(&amp;quot;actionPerfomed is implemented as anonymous class here &amp;amp; is added as a listener to button1&amp;quot;);&lt;br /&gt;
                     }  &lt;br /&gt;
              });&lt;br /&gt;
       }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
*In a pure OO language like Ruby, blocks can also be used to defines closures(blocks, procs &amp;amp; lambdas)&amp;lt;ref&amp;gt;[http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas Sosinski, Robert. &amp;quot;Understanding Ruby Blocks, Procs and Lambdas.&amp;quot; N.p., 21 Dec. 2008. Web. 8 Sept. 2011.]&amp;lt;/ref&amp;gt;&lt;br /&gt;
E.g.&lt;br /&gt;
Given below is a method which yields control to a block in Ruby &amp;amp; also passes parameter to it at the same time.&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
def block_demo&lt;br /&gt;
    yield(&amp;quot;Smith&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
block_demo { |name| puts &amp;quot; Hello #{name} &amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The Output will be&lt;br /&gt;
 Hello Smith &lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Block structured languages had their uses in earlier days when code size was small and the main focus was on efficiency(in terms of space &amp;amp; memory) of programs. However, these programs could not scale in size as maintenance and code complexity became an overhead.&lt;br /&gt;
&lt;br /&gt;
Object Oriented concepts like encapsulation, inheritance(which helps in code re-usability), interfaces &amp;amp; polymorphism allowed better visualization of the software problem and its design to handle this complexity. Moreover, class libraries and frameworks developed in these languages helped abstract the code complexity and allowed rapid program development. This led to a gradual transition from block oriented to object oriented languages for application development.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
#[http://golang.org/ The Go Programming Language by Google for multicore and networked machines]&lt;br /&gt;
#[http://download.oracle.com/javase/tutorial/java/concepts/ Object Oriented Concepts (Java Tutorials)] &lt;br /&gt;
#[http://www.daimi.au.dk/publications/PB/230/PB-230.pdf  Block Structure and Object Oriented Languages by Ole Lehrmann Madsen]&lt;br /&gt;
#[http://portal.acm.org/citation.cfm?id=356671 A Tutorial on Algol 68, by Andrew S. Tanenbaum, in Computing Surveys, Vol. 8, No. 2, June 1976, with Corrigenda (Vol. 9, No. 3, September 1977) ] &lt;br /&gt;
#[http://ruby-lang.org Official Ruby Webpage]&lt;br /&gt;
#[http://www.pascal-programming.info/index.php Pascal programming tutorials]&lt;br /&gt;
#[http://steve-yegge.blogspot.com/2008/05/dynamic-languages-strike-back.html Dynamic Languages Strike Back]&lt;br /&gt;
#[http://amath.colorado.edu/computing/mmm/funcproc.html Functional vs Procedural languages]&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_lm&amp;diff=50773</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1e lm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_lm&amp;diff=50773"/>
		<updated>2011-09-25T22:28:33Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: /* Examples */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
This wiki article discusses two things viz the effectiveness &amp;amp; durability of object oriented languages (like Java, Python, etc) as compared to block structured languages(like C, Pascal, Algol etc) and how block structure can be leveraged in an object oriented language.&lt;br /&gt;
&lt;br /&gt;
==Block structured languages==&lt;br /&gt;
&lt;br /&gt;
===Definition===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Block_(programming) block] is a piece of code consisting of one or more [http://en.wikipedia.org/wiki/Declaration_(computer_science) declarations] and [http://en.wikipedia.org/wiki/Statement_(programming) statements]. A block structured programming language is the one that would allow usage of such blocks &amp;amp; including [http://en.wikipedia.org/wiki/Nesting_(computing) nesting] of such blocks.&lt;br /&gt;
The purpose of blocks is to allow a group of statements to be treated as one.For instance, you might want to have a block of statements which does matrix multiplication.Blocks also narrow the lexical scope of the variables, procedures &amp;amp; functions declared in a block so that they don’t conflict with others(which might be used for a totally different purpose)having the same name elsewhere in the program.For instance, you might want to have a local variable “count” to count the sum of some variables whereas you might have another local variable with the same name “count” to count the number of elements in an array.&lt;br /&gt;
&lt;br /&gt;
===Basic features===&lt;br /&gt;
*Tasks are accomplished using functions. The primary focus is on functions. It involves creating a list of instructions and grouping them into blocks.&lt;br /&gt;
*The functions share global data. The data is stored in data structures which act as information containers. These are global in scope and are passed around between functions using parameters.&lt;br /&gt;
*It uses a top down approach of program design. Here the system is broken down into sub-systems which are recursively refined into additional sub- system levels until we reach base level of granularity which can be implemented by a single function.&lt;br /&gt;
*Functions are implemented as black-boxes with an input and output specification.&lt;br /&gt;
&lt;br /&gt;
===Examples===&lt;br /&gt;
Some of the examples of these languages include Algol, C &amp;amp; Pascal.&lt;br /&gt;
*E.g. Pascal program multiply two numbers.&lt;br /&gt;
Here we can see that the block is delimited by &amp;quot;Begin&amp;quot; &amp;amp; &amp;quot;End&amp;quot; keywords&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
Program Multiplication_Of_Two_Numbers;&lt;br /&gt;
&lt;br /&gt;
Var Product, Multiplier, Multiplicand : Real; &lt;br /&gt;
&lt;br /&gt;
Begin&lt;br /&gt;
    Writeln('Program to multiply 2 numbers:');&lt;br /&gt;
    Writeln;&lt;br /&gt;
    Writeln;&lt;br /&gt;
    Write('Enter the Multilicand: ');&lt;br /&gt;
    Readln(Multiplicand);&lt;br /&gt;
    Writeln;&lt;br /&gt;
    Write('Enter the Multiplier: ');&lt;br /&gt;
    Readln(Multiplier);&lt;br /&gt;
   &lt;br /&gt;
    Product := Multiplicand * Multiplier;&lt;br /&gt;
    Writeln;&lt;br /&gt;
    Writeln('Product of ',Multiplicand,'and ',Multiplier,'equals '+&lt;br /&gt;
           +'to ',Product);&lt;br /&gt;
&lt;br /&gt;
End.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*E.g. C program to read a file line by line.&lt;br /&gt;
Here we can see that the blocks are delimited by the flower braces. We can also see the usage of global variable by the function print_file().&lt;br /&gt;
Also, we can see that in our case we explictly tell the main() not to expect any return value from the function print_file() with the help of keyword &amp;quot;void&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
FILE *ifp;                                                                 /*File pointer which points to the file*/&lt;br /&gt;
&lt;br /&gt;
void print_file(char *filename)&lt;br /&gt;
{&lt;br /&gt;
      char line[80];                                                       /*Assuming each line is 80 character in length*/&lt;br /&gt;
      ifp = fopen(*filename, &amp;quot;r&amp;quot;);                                         /*opens the file for &amp;quot;reading&amp;quot; mode only*/&lt;br /&gt;
&lt;br /&gt;
      while(fgets(line,1,sizeof(line),fp)                                  /*Loop till no more lines to read*/&lt;br /&gt;
      {&lt;br /&gt;
            printf(&amp;quot;%s&amp;quot;, line);                                            /*Prints this line on to the standard output*/&lt;br /&gt;
      }&lt;br /&gt;
      fclose(fp);                                                          /*Closes the file*/&lt;br /&gt;
       &lt;br /&gt;
}&lt;br /&gt;
void main()&lt;br /&gt;
{&lt;br /&gt;
     char *name;&lt;br /&gt;
&lt;br /&gt;
     printf(&amp;quot;Enter the name of the file to be opened: &amp;quot;);&lt;br /&gt;
     scanf(&amp;quot;%s&amp;quot;, name);          &lt;br /&gt;
&lt;br /&gt;
     print_file(name);                                                     /*Function call made to print_file() to print the file*/&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object Oriented Languages:==&lt;br /&gt;
&lt;br /&gt;
===Definition===&lt;br /&gt;
Object oriented languages are built on the object oriented concept. This is the programming paradigm that bundles the data and the methods that access and manage this data into single entity called an '''''Object'''''. This bundling of data and methods allows the object to act as a single entity and have a fluid interface through which it can interact with the outside world. The objects are generally modeled on real world objects.&lt;br /&gt;
&lt;br /&gt;
===Fundamental features of Object Oriented languages===&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_%28computer_science%29 Object]/[http://en.wikipedia.org/wiki/Class_%28computer_programming%29 Class]: Class consists of tightly coupled data fields and set of operations that act on the data. The entire set of data and operations represent a user-defined data type. An object is an instance of a class. Thus a class acts as a collection of objects of similar type. Multiple objects may be created out of a class. The objects may share the operations of the class but have their own set of data fields. An object can be defined by its current state (current value of its data members) and its behavior (the methods that can be called on the object).  &lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Abstraction_(computer_science) Data Abstraction]/[http://en.wikipedia.org/wiki/Encapsulation_%28object-oriented_programming%29 Encapsulation]: The wrapping of data and functions into a single unit (class) is called encapsulation. Encapsulation protects an object from accidental modifications by an external entity since data fields can be accessed only through predetermined interfaces. Encapsulation is implemented using language constructs like private, public, protected, etc. Abstraction involves representing only the essential features of the object and hiding the background details. Classes provide abstraction by hiding the data attributes and providing functions that provide a controlled view of the object to an external entity.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Inheritance_%28object-oriented_programming%29 Inheritance]:  Inheritance is the ability of a class to inherit partly or completely the features of another class. It allows the hierarchical classification of objects.For example, the bird ''Peacock'' is a part of the class ''Flying bird'' which again is part of the class ''Bird''. Inheritance allows the reusability of code. Pre-existing classes can be reused by extending them and adding new functionality to create sub classes without modifying the original classes. The sub class will then have the combined features of both classes. This allows a programmer to tailor a new class from an existing class without introducing any undesirable side effects in the base classes.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Interface_%28Java%29 Interface]: Interface is an abstract type containing no data but only a list of method definitions. A class that has all the methods defined in the interface is said to have implemented the method interface. Interfaces allow classes to define how they will communicate with an external entity.  By implementing an interface, a class promises an external entity using the interface that it will provide the functionality specified by the interface.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]/[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding]: This is the ability of a variable, method or an object to take more than one forms. This enables an objects of different types to respond to same method or property call but in its own type specific way. For example consider overloading the + operator, so that when called on strings, it concatenates the strings while for numbers it adds them up. Polymorphism also allows objects with different internal structures to share the same external interface. This helps in dynamic binding. Dynamic binding means that the code executed for a given function call is not known until actual runtime. For example, in a dynamically typed language a variable can hold an object irrespective of its data type. Hence a function call associated with a reference (variable) depends on the dynamic type of that reference (variable) i.e. the current object referenced by the variable.&lt;br /&gt;
&lt;br /&gt;
===Examples===&lt;br /&gt;
The below languages implement different degrees of OO  features&amp;lt;ref&amp;gt;[http://www.jvoegele.com/software/langcomp.html Voegele, Jason. &amp;quot;Programming Language Comparison.&amp;quot; Web.]&amp;lt;/ref&amp;gt;&lt;br /&gt;
*Ruby, Scala, Smalltalk, Eiffel are said to be purely OO languages. Here even primitives are implemented as OO objects.&lt;br /&gt;
*Java, C++, C#, VB.NET, Python are OO languages but have some degree of procedural elements&lt;br /&gt;
*Visual Basic, PHP, Perl are basically procedural languages with some OO features.&lt;br /&gt;
&lt;br /&gt;
E.g. C++ program demonstrating the use of a class .The class CRectangle represents a rectangle and has data members x,y which represent the length and breadth (ie: properties) of the rectangle. The functions set_values() and area() allow us to access these data members in a controlled manner. The class object has been instatiated in function main&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
&lt;br /&gt;
class CRectangle {&lt;br /&gt;
    int x, y;&lt;br /&gt;
  public:&lt;br /&gt;
    void set_values (int,int);&lt;br /&gt;
    int area () ;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
void CRectangle::set_values (int a, int b) {&lt;br /&gt;
  x = a;&lt;br /&gt;
  y = b;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void CRectangle::area() {&lt;br /&gt;
  return x*y;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main () {&lt;br /&gt;
  CRectangle rect;&lt;br /&gt;
  rect.set_values (3,4);&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;area: &amp;quot; &amp;lt;&amp;lt; rect.area();&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
 &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Need for Object Oriented Languages==&lt;br /&gt;
Block structured languages follow a task-centric approach rather than a data-centric approach. This resulted in certain flaws:&lt;br /&gt;
#Structured programming follows a hierarchy of structures. However in large programs data is generally in the form of a network of structures. This resulted in a lot of [http://en.wiktionary.org/wiki/tramp_data tramp data] being passed around.&amp;lt;ref&amp;gt; [http://en.wikipedia.org/wiki/Structured_programming#Object-oriented_comparison Object Oriented Comparison. Web. 8 June 2011.] &amp;lt;/ref&amp;gt;&lt;br /&gt;
#As the program size increased the amount of data being passed around increased proportionately. Ensuring parameters are correctly passed to the correct subprogram becomes a major issue.&lt;br /&gt;
#In case of large programs even a small change resulted in massive ripple-effect with changing multiple sub modules to propagate the new data through the program's hierarchy.&lt;br /&gt;
#Structured program shared a lot of global data. The acces to the data was uncontrolled and unpredictable. There was no control over who had access to what.&lt;br /&gt;
#Structured program suffered from memory leaks.&amp;lt;ref&amp;gt;[http://www.cprogramming.com/debugging/valgrind.html Using Valgrind to Find Memory Leaks and Invalid Memory Use]&amp;lt;/ref&amp;gt; This was because anyone could create global structures and there was no responsibility on the creator to deallocate the memory.&lt;br /&gt;
The main motivating factor for object-oriented approach is to remove these flaws of block structured languages. OO languages treat data as a critical element and do not allow it to flow freely around the system. The data is tied to the functions with operate on and prevent it from accidental modification from external functions.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Object-oriented languages over Block structured languages==&lt;br /&gt;
#Object oriented approach follows a data centered approach. It is possible to map objects in the problem domain to those in the program. This helps the programmer visualize the program in terms of real world objects and which leads to a much better design. Also it allows more details of a model to be captured in implementable form.&amp;lt;ref&amp;gt;Balaguruswamy, E. Object Oriented Programming with C++. New Delhi: Tata Mcgraw-Hill, 2008. Print.&amp;lt;/ref&amp;gt;&lt;br /&gt;
#The principle of data hiding helps in building secure programs. The data fields of an object can be accessed only through the interface defined by its publicly accessible methods. Such controlled access prevents any inadvertent change to object data by external modules.&amp;lt;ref&amp;gt;[http://www.developer.com/design/article.php/3387591/Hiding-Data-within-Object-Oriented-Programming.htm Weisfeld, Matt. &amp;quot;Hiding Data within Object-Oriented Programming.&amp;quot; N.p., 29 July 2004. Web.]&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Mapping real world entities into objects help reduce and easily manage software complexity . Also it helps when systems need to be upgraded to larger systems.&amp;lt;ref&amp;gt;[http://www.artima.com/interfacedesign/SigObjects.html &amp;quot;Interface Design by Bill Venners.&amp;quot; Web.]&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Objects act as black boxes with internal structure and well defined interfaces. Hence adding new functionality becomes easier, since we can easily isolate the objects/classes that need to be modified or extended. The low coupling between objects allows an object to be modified without any effect on the other objects accessing it so long as the interface through which they interact remain untouched.&lt;br /&gt;
#Most modern object oriented languages provides a load of class libraries and frameworks. These are much easier to use as compared to library functions present in block structured languages. The frameworks help in rapid software development and easier testing and debugging thus leading to increased productivity.&amp;lt;ref&amp;gt;[http://www.cs.bgsu.edu/maner/domains/RAD.htm Maner, Walter. RAPID APPLICATION DEVELOPMENT. N.p., 15 Mar. 1997. Web.]&amp;lt;/ref&amp;gt; &amp;lt;ref&amp;gt;[http://www.chillarege.com/authwork/TestingBestPractice.pdf Chillarege, Ram. &amp;quot;Software Testing Best Practices.&amp;quot; N.p., 26 Apr. 1999. Web.]&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Message passing techniques (ex: interfaces in JAVA) help separate implementation of an object from its externally visible interface. Multiple classes of objects may implement the same interface. In such cases an object can be replaced by another object without affecting external entities dependent on that object so long as the interface is untouched. This results in low coupling.&lt;br /&gt;
#Object oriented design provides much better memory management. Recent OO languages sport automated garbage handling. Even for languages without garbage collection&amp;lt;ref&amp;gt;[http://javarevisited.blogspot.com/2011/04/garbage-collection-in-java.html Paul, Javin. &amp;quot;Garbage Collection in Java.&amp;quot; How Garbage Collection Works In Java. N.p., 11 Apr. 2011. Web. ]&amp;lt;/ref&amp;gt;, memory deallocation of data members can be handled in the destructors of the objects. But in case of block structured languages (like C), it is the programmers responsibility to take care of releasing the dynamically allocated memory. Also the data structures tend to be global in scope and persist beyond the scope of the functions calling them. Hence it becomes difficult to fix responsibility on functions to deallocate them. This leads to memory leaks.&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Object-Oriented Languages==&lt;br /&gt;
Although object oriented languages sport some very advantageous features as compared to block structured languages, they do have some drawbacks. &lt;br /&gt;
#Economy of language features: The addition of OO features to a language and the additional syntax needed to implement these features makes it bulky. The complexity of these languages make them daunting to learn and master.&amp;lt;ref name=&amp;quot;badEnggProperties&amp;quot;&amp;gt;[http://lucacardelli.name/Papers/BadPropertiesOfOO.html Bad Engineering Properties of Object-Oriented Languages by Luca Cardelli]&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Economy of execution: OO style is intrinsically less efficient as compared to block structured style. In procedural languages, type checking occurs at compile time. Hence no tests are needed at runtime for pointer dereferencing for method calls.In OO style each routine is supposed to be a virtual method. This introduces indirections in the form of method tables during for dereferencing.&lt;br /&gt;
#Also many OO languages include automatic garbage collection (e.g. Java) &amp;amp; hence it might slow down the performance.&amp;lt;ref&amp;gt;[http://www.jelovic.com/articles/why_java_is_slow.htm Why Java is slow Jelovic, Dejan. &amp;quot;Why Java Is Slow.&amp;quot; Why Java Will Always Be Slower Than C++. N.p., 18 Dec. 2009. Web.]&amp;lt;/ref&amp;gt;. This along with the overhead of creation and destruction of objects degrades performance in resource intensive systems such as embedded systems and real time systems.&amp;lt;ref&amp;gt;[http://csjournals.com/IJCSC/PDF1-2/9..pdf Object Oriented Vs Procedural Programming in Embedded System ]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Usage of block-structures in Object Oriented languages==&lt;br /&gt;
OO languages have evolved from block structured languages. Though OO languages use the concepts of class and objects,inheritance,etc they inherently still rely on a blocks to define the structure of  the code. The key areas where OO languages assimilate the idea of block structure are as follows:&lt;br /&gt;
*In classes the member functions or methods are implemented using blocks. Each method can be viewed as a block that accepts parameters from an external entity that calls it, performs some processing (that may or may not require the use of data fields of the object) and returns control back with or without a return value.  &lt;br /&gt;
*Blocks are also useful in defining control structures like loops, conditional blocks(e.g. if...else), etc.&lt;br /&gt;
E.g. Java program demonstrating the usage of a while loop .The while loop encloses a group of statements within curly braces signifying a block that may be iterated upon till while condition holds true&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
package hashmap;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.LinkedHashMap;&lt;br /&gt;
import java.util.Map;&lt;br /&gt;
import java.util.Set;&lt;br /&gt;
&lt;br /&gt;
public class ExampleHashMap&lt;br /&gt;
{&lt;br /&gt;
	public static void main(String args[])&lt;br /&gt;
	{&lt;br /&gt;
		LinkedHashMap h = new LinkedHashMap();                          //Creates a LinkedHashMap. Linked HashMaps defines the &lt;br /&gt;
		h.put(&amp;quot;A&amp;quot;,100);                                                 //iteration ordering, which is normally the &lt;br /&gt;
		h.put(&amp;quot;B&amp;quot;,200);                                                 //order in which keys were inserted into the map.&lt;br /&gt;
		h.put(&amp;quot;C&amp;quot;,300);&lt;br /&gt;
		h.put(&amp;quot;D&amp;quot;,400);&lt;br /&gt;
		h.put(&amp;quot;E&amp;quot;,500);&lt;br /&gt;
		&lt;br /&gt;
		Set s = h.entrySet();                                           //Returns a set containing key value pairs&lt;br /&gt;
		Iterator i = s.iterator();                                      &lt;br /&gt;
		&lt;br /&gt;
		while(i.hasNext())&lt;br /&gt;
		{&lt;br /&gt;
			Map.Entry me = (Map.Entry)i.next();                     //Each element in the set has to be casted to Map.Entry &lt;br /&gt;
			System.out.println(me.getKey()+&amp;quot; : &amp;quot;+me.getValue());    //to access the key/value &lt;br /&gt;
		}&lt;br /&gt;
       }&lt;br /&gt;
}&lt;br /&gt;
		&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
*OO languages generally provide inbuilt language support for exception handling. Most OO languages implement exception handling in terms of blocks&lt;br /&gt;
E.g.: Try...catch  blocks in Java&lt;br /&gt;
Here the catch and finally blocks are used to delimit a group of statements that can be called to handle exceptions at runtime. Also, we can see usage of conditional statements if...else in this case.&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
try{&lt;br /&gt;
    System.out.println(&amp;quot;Inside try block of testMethod!&amp;quot;);&lt;br /&gt;
    int p=1000, q=0, i=0;&lt;br /&gt;
    if(p&amp;gt;q){                                               //We can clearly see here that a Divide by Zero exception will occur.&lt;br /&gt;
         i=p/q;&lt;br /&gt;
    }&lt;br /&gt;
    else if(q&amp;gt;p){&lt;br /&gt;
         i=q/p;&lt;br /&gt;
    }&lt;br /&gt;
    else{                                                 //In this case p equals q&lt;br /&gt;
         i=p/q;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
catch(Exception e){&lt;br /&gt;
    System.out.println(&amp;quot;Inside catch block of testMethod!&amp;quot;);&lt;br /&gt;
    System.out.println(“Caution: your trying to Divide a number by Zero...”);&lt;br /&gt;
}&lt;br /&gt;
finally{                                                  &lt;br /&gt;
    System.out.println(&amp;quot;Inside finally block of testMethod!&amp;quot;);&lt;br /&gt;
    System.out.println(“This block executes everytime an exception is caught...”);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
*Certain OO languages like JAVA allow creation of anonymous classes which are basically blocks which don’t have a name but can be instantiated without actually making a separate class.&lt;br /&gt;
E.g.&lt;br /&gt;
Here ActionListener is an interface which has been implemented as anonymous block in the below JAVA example&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
import java.awt.Button;&lt;br /&gt;
&lt;br /&gt;
class Submit()&lt;br /&gt;
{&lt;br /&gt;
       public Submit()&lt;br /&gt;
       {&lt;br /&gt;
              Button button1 = new Button(&amp;quot;Submit&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
              button1.addActionListener(new ActionListener(){&lt;br /&gt;
                     public void actionPerformed (ActionEvent event){&lt;br /&gt;
                            System.out.println(&amp;quot;actionPerfomed is implemented as anonymous class here &amp;amp; is added as a listener to button1&amp;quot;);&lt;br /&gt;
                     }  &lt;br /&gt;
              });&lt;br /&gt;
       }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
*In a pure OO language like Ruby, blocks can also be used to defines closures(blocks, procs &amp;amp; lambdas)&amp;lt;ref&amp;gt;[http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas Sosinski, Robert. &amp;quot;Understanding Ruby Blocks, Procs and Lambdas.&amp;quot; N.p., 21 Dec. 2008. Web. 8 Sept. 2011.]&amp;lt;/ref&amp;gt;&lt;br /&gt;
E.g.&lt;br /&gt;
Given below is a method which yields control to a block in Ruby &amp;amp; also passes parameter to it at the same time.&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
def block_demo&lt;br /&gt;
    yield(&amp;quot;Smith&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
block_demo { |name| puts &amp;quot; Hello #{name} &amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The Output will be&lt;br /&gt;
 Hello Smith &lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Block structured languages had their uses in earlier days when code size was small and the main focus was on efficiency(in terms of space &amp;amp; memory) of programs. However, these programs could not scale in size as maintenance and code complexity became an overhead.&lt;br /&gt;
&lt;br /&gt;
Object Oriented concepts like encapsulation, inheritance(which helps in code re-usability), interfaces &amp;amp; polymorphism allowed better visualization of the software problem and its design to handle this complexity. Moreover, class libraries and frameworks developed in these languages helped abstract the code complexity and allowed rapid program development. This led to a gradual transition from block oriented to object oriented languages for application development.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
#[http://golang.org/ The Go Programming Language by Google for multicore and networked machines]&lt;br /&gt;
#[http://download.oracle.com/javase/tutorial/java/concepts/ Object Oriented Concepts (Java Tutorials)] &lt;br /&gt;
#[http://www.daimi.au.dk/publications/PB/230/PB-230.pdf  Block Structure and Object Oriented Languages by Ole Lehrmann Madsen]&lt;br /&gt;
#[http://portal.acm.org/citation.cfm?id=356671 A Tutorial on Algol 68, by Andrew S. Tanenbaum, in Computing Surveys, Vol. 8, No. 2, June 1976, with Corrigenda (Vol. 9, No. 3, September 1977) ] &lt;br /&gt;
#[http://ruby-lang.org Official Ruby Webpage]&lt;br /&gt;
#[http://www.pascal-programming.info/index.php Pascal programming tutorials]&lt;br /&gt;
#[http://steve-yegge.blogspot.com/2008/05/dynamic-languages-strike-back.html Dynamic Languages Strike Back]&lt;br /&gt;
#[http://amath.colorado.edu/computing/mmm/funcproc.html Functional vs Procedural languages]&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_lm&amp;diff=50762</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1e lm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_lm&amp;diff=50762"/>
		<updated>2011-09-25T22:22:09Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: /* Examples */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
This wiki article discusses two things viz the effectiveness &amp;amp; durability of object oriented languages (like Java, Python, etc) as compared to block structured languages(like C, Pascal, Algol etc) and how block structure can be leveraged in an object oriented language.&lt;br /&gt;
&lt;br /&gt;
==Block structured languages==&lt;br /&gt;
&lt;br /&gt;
===Definition===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Block_(programming) block] is a piece of code consisting of one or more [http://en.wikipedia.org/wiki/Declaration_(computer_science) declarations] and [http://en.wikipedia.org/wiki/Statement_(programming) statements]. A block structured programming language is the one that would allow usage of such blocks &amp;amp; including [http://en.wikipedia.org/wiki/Nesting_(computing) nesting] of such blocks.&lt;br /&gt;
The purpose of blocks is to allow a group of statements to be treated as one.For instance, you might want to have a block of statements which does matrix multiplication.Blocks also narrow the lexical scope of the variables, procedures &amp;amp; functions declared in a block so that they don’t conflict with others(which might be used for a totally different purpose)having the same name elsewhere in the program.For instance, you might want to have a local variable “count” to count the sum of some variables whereas you might have another local variable with the same name “count” to count the number of elements in an array.&lt;br /&gt;
&lt;br /&gt;
===Basic features===&lt;br /&gt;
*Tasks are accomplished using functions. The primary focus is on functions. It involves creating a list of instructions and grouping them into blocks.&lt;br /&gt;
*The functions share global data. The data is stored in data structures which act as information containers. These are global in scope and are passed around between functions using parameters.&lt;br /&gt;
*It uses a top down approach of program design. Here the system is broken down into sub-systems which are recursively refined into additional sub- system levels until we reach base level of granularity which can be implemented by a single function.&lt;br /&gt;
*Functions are implemented as black-boxes with an input and output specification.&lt;br /&gt;
&lt;br /&gt;
===Examples===&lt;br /&gt;
Some of the examples of these languages include Algol, C &amp;amp; Pascal.&lt;br /&gt;
*E.g. Pascal program multiply two numbers.&lt;br /&gt;
Here we can see that the block is delimited by &amp;quot;Begin&amp;quot; &amp;amp; &amp;quot;End&amp;quot; keywords&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
Program Multiplication_Of_Two_Numbers;&lt;br /&gt;
&lt;br /&gt;
Var Product, Multiplier, Multiplicand : Real; &lt;br /&gt;
&lt;br /&gt;
Begin&lt;br /&gt;
    Writeln('Program to multiply 2 numbers:');&lt;br /&gt;
    Writeln;&lt;br /&gt;
    Writeln;&lt;br /&gt;
    Write('Enter the Multilicand: ');&lt;br /&gt;
    Readln(Multiplicand);&lt;br /&gt;
    Writeln;&lt;br /&gt;
    Write('Enter the Multiplier: ');&lt;br /&gt;
    Readln(Multiplier);&lt;br /&gt;
   &lt;br /&gt;
    Product := Multiplicand * Multiplier;&lt;br /&gt;
    Writeln;&lt;br /&gt;
    Writeln('Product of ',Multiplicand,'and ',Multiplier,'equals '+&lt;br /&gt;
           +'to ',Product);&lt;br /&gt;
&lt;br /&gt;
End.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*E.g. C program to read a file line by line.&lt;br /&gt;
Here we can see that the blocks are delimited by the flower braces. We can also see the usage of global variable by the function print_file().&lt;br /&gt;
Also, we can see that in our case we explictly tell the main() not to expect any return value from the function print_file() with the help of keyword &amp;quot;void&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
FILE *ifp;                                                                 /*File pointer which points to the file*/&lt;br /&gt;
&lt;br /&gt;
void print_file(char *filename)&lt;br /&gt;
{&lt;br /&gt;
      char line[80];                                                       /*Assuming each line is 80 character in length*/&lt;br /&gt;
      ifp = fopen(*filename, &amp;quot;r&amp;quot;);                                         /*opens the file for &amp;quot;reading&amp;quot; mode only*/&lt;br /&gt;
&lt;br /&gt;
      while(fgets(line,1,sizeof(line),fp)                                  /*Loop till no more lines to read*/&lt;br /&gt;
      {&lt;br /&gt;
            printf(&amp;quot;%s&amp;quot;, line);                                            /*Prints this line on to the standard output*/&lt;br /&gt;
      }&lt;br /&gt;
      fclose(fp);                                                          /*Closes the file*/&lt;br /&gt;
       &lt;br /&gt;
}&lt;br /&gt;
void main()&lt;br /&gt;
{&lt;br /&gt;
     char *name;&lt;br /&gt;
&lt;br /&gt;
     printf(&amp;quot;Enter the name of the file to be opened: &amp;quot;);&lt;br /&gt;
     scanf(&amp;quot;%s&amp;quot;, name);          &lt;br /&gt;
&lt;br /&gt;
     print_file(name);                                                     /*Function call made to print_file() to print the file*/&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object Oriented Languages:==&lt;br /&gt;
&lt;br /&gt;
===Definition===&lt;br /&gt;
Object oriented languages are built on the object oriented concept. This is the programming paradigm that bundles the data and the methods that access and manage this data into single entity called an '''''Object'''''. This bundling of data and methods allows the object to act as a single entity and have a fluid interface through which it can interact with the outside world. The objects are generally modeled on real world objects.&lt;br /&gt;
&lt;br /&gt;
===Fundamental features of Object Oriented languages===&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_%28computer_science%29 Object]/[http://en.wikipedia.org/wiki/Class_%28computer_programming%29 Class]: Class consists of tightly coupled data fields and set of operations that act on the data. The entire set of data and operations represent a user-defined data type. An object is an instance of a class. Thus a class acts as a collection of objects of similar type. Multiple objects may be created out of a class. The objects may share the operations of the class but have their own set of data fields. An object can be defined by its current state (current value of its data members) and its behavior (the methods that can be called on the object).  &lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Abstraction_(computer_science) Data Abstraction]/[http://en.wikipedia.org/wiki/Encapsulation_%28object-oriented_programming%29 Encapsulation]: The wrapping of data and functions into a single unit (class) is called encapsulation. Encapsulation protects an object from accidental modifications by an external entity since data fields can be accessed only through predetermined interfaces. Encapsulation is implemented using language constructs like private, public, protected, etc. Abstraction involves representing only the essential features of the object and hiding the background details. Classes provide abstraction by hiding the data attributes and providing functions that provide a controlled view of the object to an external entity.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Inheritance_%28object-oriented_programming%29 Inheritance]:  Inheritance is the ability of a class to inherit partly or completely the features of another class. It allows the hierarchical classification of objects.For example, the bird ''Peacock'' is a part of the class ''Flying bird'' which again is part of the class ''Bird''. Inheritance allows the reusability of code. Pre-existing classes can be reused by extending them and adding new functionality to create sub classes without modifying the original classes. The sub class will then have the combined features of both classes. This allows a programmer to tailor a new class from an existing class without introducing any undesirable side effects in the base classes.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Interface_%28Java%29 Interface]: Interface is an abstract type containing no data but only a list of method definitions. A class that has all the methods defined in the interface is said to have implemented the method interface. Interfaces allow classes to define how they will communicate with an external entity.  By implementing an interface, a class promises an external entity using the interface that it will provide the functionality specified by the interface.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]/[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding]: This is the ability of a variable, method or an object to take more than one forms. This enables an objects of different types to respond to same method or property call but in its own type specific way. For example consider overloading the + operator, so that when called on strings, it concatenates the strings while for numbers it adds them up. Polymorphism also allows objects with different internal structures to share the same external interface. This helps in dynamic binding. Dynamic binding means that the code executed for a given function call is not known until actual runtime. For example, in a dynamically typed language a variable can hold an object irrespective of its data type. Hence a function call associated with a reference (variable) depends on the dynamic type of that reference (variable) i.e. the current object referenced by the variable.&lt;br /&gt;
&lt;br /&gt;
===Examples===&lt;br /&gt;
The below languages implement different degrees of OO  features&lt;br /&gt;
*Ruby, Scala, Smalltalk, Eiffel are said to be purely OO languages. Here even primitives are implemented as OO objects.&lt;br /&gt;
*Java, C# , VB.NET, Python are OO languages but have some degree of procedural elements&lt;br /&gt;
*Visual Basic, PHP, Perl are basically procedural languages with some OO features.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
&lt;br /&gt;
class CRectangle {&lt;br /&gt;
    int x, y;&lt;br /&gt;
  public:&lt;br /&gt;
    void set_values (int,int);&lt;br /&gt;
    int area () ;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
void CRectangle::set_values (int a, int b) {&lt;br /&gt;
  x = a;&lt;br /&gt;
  y = b;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void CRectangle::area() {&lt;br /&gt;
  return x*y;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main () {&lt;br /&gt;
  CRectangle rect;&lt;br /&gt;
  rect.set_values (3,4);&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;area: &amp;quot; &amp;lt;&amp;lt; rect.area();&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
 &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Need for Object Oriented Languages==&lt;br /&gt;
Block structured languages follow a task-centric approach rather than a data-centric approach. This resulted in certain flaws:&lt;br /&gt;
#Structured programming follows a hierarchy of structures. However in large programs data is generally in the form of a network of structures. This resulted in a lot of [http://en.wiktionary.org/wiki/tramp_data tramp data] being passed around.&amp;lt;ref&amp;gt; [http://en.wikipedia.org/wiki/Structured_programming#Object-oriented_comparison Object Oriented Comparison. Web. 8 June 2011.] &amp;lt;/ref&amp;gt;&lt;br /&gt;
#As the program size increased the amount of data being passed around increased proportionately. Ensuring parameters are correctly passed to the correct subprogram becomes a major issue.&lt;br /&gt;
#In case of large programs even a small change resulted in massive ripple-effect with changing multiple sub modules to propagate the new data through the program's hierarchy.&lt;br /&gt;
#Structured program shared a lot of global data. The acces to the data was uncontrolled and unpredictable. There was no control over who had access to what.&lt;br /&gt;
#Structured program suffered from memory leaks.&amp;lt;ref&amp;gt;[http://www.cprogramming.com/debugging/valgrind.html Using Valgrind to Find Memory Leaks and Invalid Memory Use]&amp;lt;/ref&amp;gt; This was because anyone could create global structures and there was no responsibility on the creator to deallocate the memory.&lt;br /&gt;
The main motivating factor for object-oriented approach is to remove these flaws of block structured languages. OO languages treat data as a critical element and do not allow it to flow freely around the system. The data is tied to the functions with operate on and prevent it from accidental modification from external functions.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Object-oriented languages over Block structured languages==&lt;br /&gt;
#Object oriented approach follows a data centered approach. It is possible to map objects in the problem domain to those in the program. This helps the programmer visualize the program in terms of real world objects and which leads to a much better design. Also it allows more details of a model to be captured in implementable form.&amp;lt;ref&amp;gt;Balaguruswamy, E. Object Oriented Programming with C++. New Delhi: Tata Mcgraw-Hill, 2008. Print.&amp;lt;/ref&amp;gt;&lt;br /&gt;
#The principle of data hiding helps in building secure programs. The data fields of an object can be accessed only through the interface defined by its publicly accessible methods. Such controlled access prevents any inadvertent change to object data by external modules.&amp;lt;ref&amp;gt;[http://www.developer.com/design/article.php/3387591/Hiding-Data-within-Object-Oriented-Programming.htm Weisfeld, Matt. &amp;quot;Hiding Data within Object-Oriented Programming.&amp;quot; N.p., 29 July 2004. Web.]&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Mapping real world entities into objects help reduce and easily manage software complexity . Also it helps when systems need to be upgraded to larger systems.&amp;lt;ref&amp;gt;[http://www.artima.com/interfacedesign/SigObjects.html &amp;quot;Interface Design by Bill Venners.&amp;quot; Web.]&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Objects act as black boxes with internal structure and well defined interfaces. Hence adding new functionality becomes easier, since we can easily isolate the objects/classes that need to be modified or extended. The low coupling between objects allows an object to be modified without any effect on the other objects accessing it so long as the interface through which they interact remain untouched.&lt;br /&gt;
#Most modern object oriented languages provides a load of class libraries and frameworks. These are much easier to use as compared to library functions present in block structured languages. The frameworks help in rapid software development and easier testing and debugging thus leading to increased productivity.&lt;br /&gt;
#Message passing techniques (ex: interfaces in JAVA) help separate implementation of an object from its externally visible interface. Multiple classes of objects may implement the same interface. In such cases an object can be replaced by another object without affecting external entities dependent on that object so long as the interface is untouched. This results in low coupling.&lt;br /&gt;
#Object oriented design provides much better memory management. Recent OO languages sport automated garbage handling. Even for languages without garbage collection&amp;lt;ref&amp;gt;[http://javarevisited.blogspot.com/2011/04/garbage-collection-in-java.html Paul, Javin. &amp;quot;Garbage Collection in Java.&amp;quot; How Garbage Collection Works In Java. N.p., 11 Apr. 2011. Web. ]&amp;lt;/ref&amp;gt;, memory deallocation of data members can be handled in the destructors of the objects. But in case of block structured languages (like C), it is the programmers responsibility to take care of releasing the dynamically allocated memory. Also the data structures tend to be global in scope and persist beyond the scope of the functions calling them. Hence it becomes difficult to fix responsibility on functions to deallocate them. This leads to memory leaks.&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Object-Oriented Languages==&lt;br /&gt;
Although object oriented languages sport some very advantageous features as compared to block structured languages, they do have some drawbacks. &lt;br /&gt;
#Economy of language features: The addition of OO features to a language and the additional syntax needed to implement these features makes it bulky. The complexity of these languages make them daunting to learn and master.&amp;lt;ref name=&amp;quot;badEnggProperties&amp;quot;&amp;gt;[http://lucacardelli.name/Papers/BadPropertiesOfOO.html Bad Engineering Properties of Object-Oriented Languages by Luca Cardelli]&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Economy of execution: OO style is intrinsically less efficient as compared to block structured style. In procedural languages, type checking occurs at compile time. Hence no tests are needed at runtime for pointer dereferencing for method calls.In OO style each routine is supposed to be a virtual method. This introduces indirections in the form of method tables during for dereferencing.&lt;br /&gt;
#Also many OO languages include automatic garbage collection (e.g. Java) &amp;amp; hence it might slow down the performance.&amp;lt;ref&amp;gt;[http://www.jelovic.com/articles/why_java_is_slow.htm Why Java is slow Jelovic, Dejan. &amp;quot;Why Java Is Slow.&amp;quot; Why Java Will Always Be Slower Than C++. N.p., 18 Dec. 2009. Web.]&amp;lt;/ref&amp;gt;. This along with the overhead of creation and destruction of objects degrades performance in resource intensive systems such as embedded systems and real time systems.&amp;lt;ref&amp;gt;[http://csjournals.com/IJCSC/PDF1-2/9..pdf Object Oriented Vs Procedural Programming in Embedded System ]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Usage of block-structures in Object Oriented languages==&lt;br /&gt;
OO languages have evolved from block structured languages. Though OO languages use the concepts of class and objects,inheritance,etc they inherently still rely on a blocks to define the structure of  the code. The key areas where OO languages assimilate the idea of block structure are as follows:&lt;br /&gt;
*In classes the member functions or methods are implemented using blocks. Each method can be viewed as a block that accepts parameters from an external entity that calls it, performs some processing (that may or may not require the use of data fields of the object) and returns control back with or without a return value.  &lt;br /&gt;
*Blocks are also useful in defining control structures like loops, conditional blocks(e.g. if...else), etc.&lt;br /&gt;
E.g. Java program demonstrating the usage of a while loop .The while loop encloses a group of statements within curly braces signifying a block that may be iterated upon till while condition holds true&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
package hashmap;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.LinkedHashMap;&lt;br /&gt;
import java.util.Map;&lt;br /&gt;
import java.util.Set;&lt;br /&gt;
&lt;br /&gt;
public class ExampleHashMap&lt;br /&gt;
{&lt;br /&gt;
	public static void main(String args[])&lt;br /&gt;
	{&lt;br /&gt;
		LinkedHashMap h = new LinkedHashMap();                          //Creates a LinkedHashMap. Linked HashMaps defines the &lt;br /&gt;
		h.put(&amp;quot;A&amp;quot;,100);                                                 //iteration ordering, which is normally the &lt;br /&gt;
		h.put(&amp;quot;B&amp;quot;,200);                                                 //order in which keys were inserted into the map.&lt;br /&gt;
		h.put(&amp;quot;C&amp;quot;,300);&lt;br /&gt;
		h.put(&amp;quot;D&amp;quot;,400);&lt;br /&gt;
		h.put(&amp;quot;E&amp;quot;,500);&lt;br /&gt;
		&lt;br /&gt;
		Set s = h.entrySet();                                           //Returns a set containing key value pairs&lt;br /&gt;
		Iterator i = s.iterator();                                      &lt;br /&gt;
		&lt;br /&gt;
		while(i.hasNext())&lt;br /&gt;
		{&lt;br /&gt;
			Map.Entry me = (Map.Entry)i.next();                     //Each element in the set has to be casted to Map.Entry &lt;br /&gt;
			System.out.println(me.getKey()+&amp;quot; : &amp;quot;+me.getValue());    //to access the key/value &lt;br /&gt;
		}&lt;br /&gt;
       }&lt;br /&gt;
}&lt;br /&gt;
		&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
*OO languages generally provide inbuilt language support for exception handling. Most OO languages implement exception handling in terms of blocks&lt;br /&gt;
E.g.: Try...catch  blocks in Java&lt;br /&gt;
Here the catch and finally blocks are used to delimit a group of statements that can be called to handle exceptions at runtime. Also, we can see usage of conditional statements if...else in this case.&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
try{&lt;br /&gt;
    System.out.println(&amp;quot;Inside try block of testMethod!&amp;quot;);&lt;br /&gt;
    int p=1000, q=0, i=0;&lt;br /&gt;
    if(p&amp;gt;q){                                               //We can clearly see here that a Divide by Zero exception will occur.&lt;br /&gt;
         i=p/q;&lt;br /&gt;
    }&lt;br /&gt;
    else if(q&amp;gt;p){&lt;br /&gt;
         i=q/p;&lt;br /&gt;
    }&lt;br /&gt;
    else{                                                 //In this case p equals q&lt;br /&gt;
         i=p/q;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
catch(Exception e){&lt;br /&gt;
    System.out.println(&amp;quot;Inside catch block of testMethod!&amp;quot;);&lt;br /&gt;
    System.out.println(“Caution: your trying to Divide a number by Zero...”);&lt;br /&gt;
}&lt;br /&gt;
finally{                                                  &lt;br /&gt;
    System.out.println(&amp;quot;Inside finally block of testMethod!&amp;quot;);&lt;br /&gt;
    System.out.println(“This block executes everytime an exception is caught...”);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
*Certain OO languages like JAVA allow creation of anonymous classes which are basically blocks which don’t have a name but can be instantiated without actually making a separate class.&lt;br /&gt;
E.g.&lt;br /&gt;
Here ActionListener is an interface which has been implemented as anonymous block in the below JAVA example&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
import java.awt.Button;&lt;br /&gt;
&lt;br /&gt;
class Submit()&lt;br /&gt;
{&lt;br /&gt;
       public Submit()&lt;br /&gt;
       {&lt;br /&gt;
              Button button1 = new Button(&amp;quot;Submit&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
              button1.addActionListener(new ActionListener(){&lt;br /&gt;
                     public void actionPerformed (ActionEvent event){&lt;br /&gt;
                            System.out.println(&amp;quot;actionPerfomed is implemented as anonymous class here &amp;amp; is added as a listener to button1&amp;quot;);&lt;br /&gt;
                     }  &lt;br /&gt;
              });&lt;br /&gt;
       }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
*In a pure OO language like Ruby, blocks can also be used to defines closures(blocks, procs &amp;amp; lambdas)&amp;lt;ref&amp;gt;[http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas Sosinski, Robert. &amp;quot;Understanding Ruby Blocks, Procs and Lambdas.&amp;quot; N.p., 21 Dec. 2008. Web. 8 Sept. 2011.]&amp;lt;/ref&amp;gt;&lt;br /&gt;
E.g.&lt;br /&gt;
Given below is a method which yields control to a block in Ruby &amp;amp; also passes parameter to it at the same time.&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
def block_demo&lt;br /&gt;
    yield(&amp;quot;Smith&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
block_demo { |name| puts &amp;quot; Hello #{name} &amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The Output will be&lt;br /&gt;
 Hello Smith &lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Block structured languages had their uses in earlier days when code size was small and the main focus was on efficiency(in terms of space &amp;amp; memory) of programs. However, these programs could not scale in size as maintenance and code complexity became an overhead.&lt;br /&gt;
&lt;br /&gt;
Object Oriented concepts like encapsulation, inheritance(which helps in code re-usability), interfaces &amp;amp; polymorphism allowed better visualization of the software problem and its design to handle this complexity. Moreover, class libraries and frameworks developed in these languages helped abstract the code complexity and allowed rapid program development. This led to a gradual transition from block oriented to object oriented languages for application development.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
#[http://golang.org/ The Go Programming Language by Google for multicore and networked machines]&lt;br /&gt;
#[http://download.oracle.com/javase/tutorial/java/concepts/ Object Oriented Concepts (Java Tutorials)] &lt;br /&gt;
#[http://www.daimi.au.dk/publications/PB/230/PB-230.pdf  Block Structure and Object Oriented Languages by Ole Lehrmann Madsen]&lt;br /&gt;
#[http://portal.acm.org/citation.cfm?id=356671 A Tutorial on Algol 68, by Andrew S. Tanenbaum, in Computing Surveys, Vol. 8, No. 2, June 1976, with Corrigenda (Vol. 9, No. 3, September 1977) ] &lt;br /&gt;
#[http://ruby-lang.org Official Ruby Webpage]&lt;br /&gt;
#[http://www.pascal-programming.info/index.php Pascal programming tutorials]&lt;br /&gt;
#[http://steve-yegge.blogspot.com/2008/05/dynamic-languages-strike-back.html Dynamic Languages Strike Back]&lt;br /&gt;
#[http://amath.colorado.edu/computing/mmm/funcproc.html Functional vs Procedural languages]&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_lm&amp;diff=50761</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1e lm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_lm&amp;diff=50761"/>
		<updated>2011-09-25T22:21:24Z</updated>

		<summary type="html">&lt;p&gt;Mndsouza: /* Examples */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
This wiki article discusses two things viz the effectiveness &amp;amp; durability of object oriented languages (like Java, Python, etc) as compared to block structured languages(like C, Pascal, Algol etc) and how block structure can be leveraged in an object oriented language.&lt;br /&gt;
&lt;br /&gt;
==Block structured languages==&lt;br /&gt;
&lt;br /&gt;
===Definition===&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Block_(programming) block] is a piece of code consisting of one or more [http://en.wikipedia.org/wiki/Declaration_(computer_science) declarations] and [http://en.wikipedia.org/wiki/Statement_(programming) statements]. A block structured programming language is the one that would allow usage of such blocks &amp;amp; including [http://en.wikipedia.org/wiki/Nesting_(computing) nesting] of such blocks.&lt;br /&gt;
The purpose of blocks is to allow a group of statements to be treated as one.For instance, you might want to have a block of statements which does matrix multiplication.Blocks also narrow the lexical scope of the variables, procedures &amp;amp; functions declared in a block so that they don’t conflict with others(which might be used for a totally different purpose)having the same name elsewhere in the program.For instance, you might want to have a local variable “count” to count the sum of some variables whereas you might have another local variable with the same name “count” to count the number of elements in an array.&lt;br /&gt;
&lt;br /&gt;
===Basic features===&lt;br /&gt;
*Tasks are accomplished using functions. The primary focus is on functions. It involves creating a list of instructions and grouping them into blocks.&lt;br /&gt;
*The functions share global data. The data is stored in data structures which act as information containers. These are global in scope and are passed around between functions using parameters.&lt;br /&gt;
*It uses a top down approach of program design. Here the system is broken down into sub-systems which are recursively refined into additional sub- system levels until we reach base level of granularity which can be implemented by a single function.&lt;br /&gt;
*Functions are implemented as black-boxes with an input and output specification.&lt;br /&gt;
&lt;br /&gt;
===Examples===&lt;br /&gt;
Some of the examples of these languages include Algol, C &amp;amp; Pascal.&lt;br /&gt;
*E.g. Pascal program multiply two numbers.&lt;br /&gt;
Here we can see that the block is delimited by &amp;quot;Begin&amp;quot; &amp;amp; &amp;quot;End&amp;quot; keywords&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
Program Multiplication_Of_Two_Numbers;&lt;br /&gt;
&lt;br /&gt;
Var Product, Multiplier, Multiplicand : Real; &lt;br /&gt;
&lt;br /&gt;
Begin&lt;br /&gt;
    Writeln('Program to multiply 2 numbers:');&lt;br /&gt;
    Writeln;&lt;br /&gt;
    Writeln;&lt;br /&gt;
    Write('Enter the Multilicand: ');&lt;br /&gt;
    Readln(Multiplicand);&lt;br /&gt;
    Writeln;&lt;br /&gt;
    Write('Enter the Multiplier: ');&lt;br /&gt;
    Readln(Multiplier);&lt;br /&gt;
   &lt;br /&gt;
    Product := Multiplicand * Multiplier;&lt;br /&gt;
    Writeln;&lt;br /&gt;
    Writeln('Product of ',Multiplicand,'and ',Multiplier,'equals '+&lt;br /&gt;
           +'to ',Product);&lt;br /&gt;
&lt;br /&gt;
End.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*E.g. C program to read a file line by line.&lt;br /&gt;
Here we can see that the blocks are delimited by the flower braces. We can also see the usage of global variable by the function print_file().&lt;br /&gt;
Also, we can see that in our case we explictly tell the main() not to expect any return value from the function print_file() with the help of keyword &amp;quot;void&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
FILE *ifp;                                                                 /*File pointer which points to the file*/&lt;br /&gt;
&lt;br /&gt;
void print_file(char *filename)&lt;br /&gt;
{&lt;br /&gt;
      char line[80];                                                       /*Assuming each line is 80 character in length*/&lt;br /&gt;
      ifp = fopen(*filename, &amp;quot;r&amp;quot;);                                         /*opens the file for &amp;quot;reading&amp;quot; mode only*/&lt;br /&gt;
&lt;br /&gt;
      while(fgets(line,1,sizeof(line),fp)                                  /*Loop till no more lines to read*/&lt;br /&gt;
      {&lt;br /&gt;
            printf(&amp;quot;%s&amp;quot;, line);                                            /*Prints this line on to the standard output*/&lt;br /&gt;
      }&lt;br /&gt;
      fclose(fp);                                                          /*Closes the file*/&lt;br /&gt;
       &lt;br /&gt;
}&lt;br /&gt;
void main()&lt;br /&gt;
{&lt;br /&gt;
     char *name;&lt;br /&gt;
&lt;br /&gt;
     printf(&amp;quot;Enter the name of the file to be opened: &amp;quot;);&lt;br /&gt;
     scanf(&amp;quot;%s&amp;quot;, name);          &lt;br /&gt;
&lt;br /&gt;
     print_file(name);                                                     /*Function call made to print_file() to print the file*/&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object Oriented Languages:==&lt;br /&gt;
&lt;br /&gt;
===Definition===&lt;br /&gt;
Object oriented languages are built on the object oriented concept. This is the programming paradigm that bundles the data and the methods that access and manage this data into single entity called an '''''Object'''''. This bundling of data and methods allows the object to act as a single entity and have a fluid interface through which it can interact with the outside world. The objects are generally modeled on real world objects.&lt;br /&gt;
&lt;br /&gt;
===Fundamental features of Object Oriented languages===&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_%28computer_science%29 Object]/[http://en.wikipedia.org/wiki/Class_%28computer_programming%29 Class]: Class consists of tightly coupled data fields and set of operations that act on the data. The entire set of data and operations represent a user-defined data type. An object is an instance of a class. Thus a class acts as a collection of objects of similar type. Multiple objects may be created out of a class. The objects may share the operations of the class but have their own set of data fields. An object can be defined by its current state (current value of its data members) and its behavior (the methods that can be called on the object).  &lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Abstraction_(computer_science) Data Abstraction]/[http://en.wikipedia.org/wiki/Encapsulation_%28object-oriented_programming%29 Encapsulation]: The wrapping of data and functions into a single unit (class) is called encapsulation. Encapsulation protects an object from accidental modifications by an external entity since data fields can be accessed only through predetermined interfaces. Encapsulation is implemented using language constructs like private, public, protected, etc. Abstraction involves representing only the essential features of the object and hiding the background details. Classes provide abstraction by hiding the data attributes and providing functions that provide a controlled view of the object to an external entity.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Inheritance_%28object-oriented_programming%29 Inheritance]:  Inheritance is the ability of a class to inherit partly or completely the features of another class. It allows the hierarchical classification of objects.For example, the bird ''Peacock'' is a part of the class ''Flying bird'' which again is part of the class ''Bird''. Inheritance allows the reusability of code. Pre-existing classes can be reused by extending them and adding new functionality to create sub classes without modifying the original classes. The sub class will then have the combined features of both classes. This allows a programmer to tailor a new class from an existing class without introducing any undesirable side effects in the base classes.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Interface_%28Java%29 Interface]: Interface is an abstract type containing no data but only a list of method definitions. A class that has all the methods defined in the interface is said to have implemented the method interface. Interfaces allow classes to define how they will communicate with an external entity.  By implementing an interface, a class promises an external entity using the interface that it will provide the functionality specified by the interface.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]/[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding]: This is the ability of a variable, method or an object to take more than one forms. This enables an objects of different types to respond to same method or property call but in its own type specific way. For example consider overloading the + operator, so that when called on strings, it concatenates the strings while for numbers it adds them up. Polymorphism also allows objects with different internal structures to share the same external interface. This helps in dynamic binding. Dynamic binding means that the code executed for a given function call is not known until actual runtime. For example, in a dynamically typed language a variable can hold an object irrespective of its data type. Hence a function call associated with a reference (variable) depends on the dynamic type of that reference (variable) i.e. the current object referenced by the variable.&lt;br /&gt;
&lt;br /&gt;
===Examples===&lt;br /&gt;
The below languages implement different degrees of OO  features&lt;br /&gt;
*Ruby, Scala, Smalltalk, Eiffel are said to be purely OO languages. Here even primitives are implemented as OO objects.&lt;br /&gt;
*Java, C# , VB.NET, Python are OO languages but have some degree of procedural elements&lt;br /&gt;
*Visual Basic, PHP, Perl are basically procedural languages with some OO features.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
&lt;br /&gt;
class CRectangle {&lt;br /&gt;
    int x, y;&lt;br /&gt;
  public:&lt;br /&gt;
    void set_values (int,int);&lt;br /&gt;
    int area () ;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
void CRectangle::set_values (int a, int b) {&lt;br /&gt;
  x = a;&lt;br /&gt;
  y = b;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void CRectangle::area() {&lt;br /&gt;
  return x*y;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main () {&lt;br /&gt;
  CRectangle rect;&lt;br /&gt;
  rect.set_values (3,4);&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;area: &amp;quot; &amp;lt;&amp;lt; rect.area();&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Need for Object Oriented Languages==&lt;br /&gt;
Block structured languages follow a task-centric approach rather than a data-centric approach. This resulted in certain flaws:&lt;br /&gt;
#Structured programming follows a hierarchy of structures. However in large programs data is generally in the form of a network of structures. This resulted in a lot of [http://en.wiktionary.org/wiki/tramp_data tramp data] being passed around.&amp;lt;ref&amp;gt; [http://en.wikipedia.org/wiki/Structured_programming#Object-oriented_comparison Object Oriented Comparison. Web. 8 June 2011.] &amp;lt;/ref&amp;gt;&lt;br /&gt;
#As the program size increased the amount of data being passed around increased proportionately. Ensuring parameters are correctly passed to the correct subprogram becomes a major issue.&lt;br /&gt;
#In case of large programs even a small change resulted in massive ripple-effect with changing multiple sub modules to propagate the new data through the program's hierarchy.&lt;br /&gt;
#Structured program shared a lot of global data. The acces to the data was uncontrolled and unpredictable. There was no control over who had access to what.&lt;br /&gt;
#Structured program suffered from memory leaks.&amp;lt;ref&amp;gt;[http://www.cprogramming.com/debugging/valgrind.html Using Valgrind to Find Memory Leaks and Invalid Memory Use]&amp;lt;/ref&amp;gt; This was because anyone could create global structures and there was no responsibility on the creator to deallocate the memory.&lt;br /&gt;
The main motivating factor for object-oriented approach is to remove these flaws of block structured languages. OO languages treat data as a critical element and do not allow it to flow freely around the system. The data is tied to the functions with operate on and prevent it from accidental modification from external functions.&lt;br /&gt;
&lt;br /&gt;
==Advantages of Object-oriented languages over Block structured languages==&lt;br /&gt;
#Object oriented approach follows a data centered approach. It is possible to map objects in the problem domain to those in the program. This helps the programmer visualize the program in terms of real world objects and which leads to a much better design. Also it allows more details of a model to be captured in implementable form.&amp;lt;ref&amp;gt;Balaguruswamy, E. Object Oriented Programming with C++. New Delhi: Tata Mcgraw-Hill, 2008. Print.&amp;lt;/ref&amp;gt;&lt;br /&gt;
#The principle of data hiding helps in building secure programs. The data fields of an object can be accessed only through the interface defined by its publicly accessible methods. Such controlled access prevents any inadvertent change to object data by external modules.&amp;lt;ref&amp;gt;[http://www.developer.com/design/article.php/3387591/Hiding-Data-within-Object-Oriented-Programming.htm Weisfeld, Matt. &amp;quot;Hiding Data within Object-Oriented Programming.&amp;quot; N.p., 29 July 2004. Web.]&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Mapping real world entities into objects help reduce and easily manage software complexity . Also it helps when systems need to be upgraded to larger systems.&amp;lt;ref&amp;gt;[http://www.artima.com/interfacedesign/SigObjects.html &amp;quot;Interface Design by Bill Venners.&amp;quot; Web.]&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Objects act as black boxes with internal structure and well defined interfaces. Hence adding new functionality becomes easier, since we can easily isolate the objects/classes that need to be modified or extended. The low coupling between objects allows an object to be modified without any effect on the other objects accessing it so long as the interface through which they interact remain untouched.&lt;br /&gt;
#Most modern object oriented languages provides a load of class libraries and frameworks. These are much easier to use as compared to library functions present in block structured languages. The frameworks help in rapid software development and easier testing and debugging thus leading to increased productivity.&lt;br /&gt;
#Message passing techniques (ex: interfaces in JAVA) help separate implementation of an object from its externally visible interface. Multiple classes of objects may implement the same interface. In such cases an object can be replaced by another object without affecting external entities dependent on that object so long as the interface is untouched. This results in low coupling.&lt;br /&gt;
#Object oriented design provides much better memory management. Recent OO languages sport automated garbage handling. Even for languages without garbage collection&amp;lt;ref&amp;gt;[http://javarevisited.blogspot.com/2011/04/garbage-collection-in-java.html Paul, Javin. &amp;quot;Garbage Collection in Java.&amp;quot; How Garbage Collection Works In Java. N.p., 11 Apr. 2011. Web. ]&amp;lt;/ref&amp;gt;, memory deallocation of data members can be handled in the destructors of the objects. But in case of block structured languages (like C), it is the programmers responsibility to take care of releasing the dynamically allocated memory. Also the data structures tend to be global in scope and persist beyond the scope of the functions calling them. Hence it becomes difficult to fix responsibility on functions to deallocate them. This leads to memory leaks.&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Object-Oriented Languages==&lt;br /&gt;
Although object oriented languages sport some very advantageous features as compared to block structured languages, they do have some drawbacks. &lt;br /&gt;
#Economy of language features: The addition of OO features to a language and the additional syntax needed to implement these features makes it bulky. The complexity of these languages make them daunting to learn and master.&amp;lt;ref name=&amp;quot;badEnggProperties&amp;quot;&amp;gt;[http://lucacardelli.name/Papers/BadPropertiesOfOO.html Bad Engineering Properties of Object-Oriented Languages by Luca Cardelli]&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Economy of execution: OO style is intrinsically less efficient as compared to block structured style. In procedural languages, type checking occurs at compile time. Hence no tests are needed at runtime for pointer dereferencing for method calls.In OO style each routine is supposed to be a virtual method. This introduces indirections in the form of method tables during for dereferencing.&lt;br /&gt;
#Also many OO languages include automatic garbage collection (e.g. Java) &amp;amp; hence it might slow down the performance.&amp;lt;ref&amp;gt;[http://www.jelovic.com/articles/why_java_is_slow.htm Why Java is slow Jelovic, Dejan. &amp;quot;Why Java Is Slow.&amp;quot; Why Java Will Always Be Slower Than C++. N.p., 18 Dec. 2009. Web.]&amp;lt;/ref&amp;gt;. This along with the overhead of creation and destruction of objects degrades performance in resource intensive systems such as embedded systems and real time systems.&amp;lt;ref&amp;gt;[http://csjournals.com/IJCSC/PDF1-2/9..pdf Object Oriented Vs Procedural Programming in Embedded System ]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Usage of block-structures in Object Oriented languages==&lt;br /&gt;
OO languages have evolved from block structured languages. Though OO languages use the concepts of class and objects,inheritance,etc they inherently still rely on a blocks to define the structure of  the code. The key areas where OO languages assimilate the idea of block structure are as follows:&lt;br /&gt;
*In classes the member functions or methods are implemented using blocks. Each method can be viewed as a block that accepts parameters from an external entity that calls it, performs some processing (that may or may not require the use of data fields of the object) and returns control back with or without a return value.  &lt;br /&gt;
*Blocks are also useful in defining control structures like loops, conditional blocks(e.g. if...else), etc.&lt;br /&gt;
E.g. Java program demonstrating the usage of a while loop .The while loop encloses a group of statements within curly braces signifying a block that may be iterated upon till while condition holds true&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
package hashmap;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.LinkedHashMap;&lt;br /&gt;
import java.util.Map;&lt;br /&gt;
import java.util.Set;&lt;br /&gt;
&lt;br /&gt;
public class ExampleHashMap&lt;br /&gt;
{&lt;br /&gt;
	public static void main(String args[])&lt;br /&gt;
	{&lt;br /&gt;
		LinkedHashMap h = new LinkedHashMap();                          //Creates a LinkedHashMap. Linked HashMaps defines the &lt;br /&gt;
		h.put(&amp;quot;A&amp;quot;,100);                                                 //iteration ordering, which is normally the &lt;br /&gt;
		h.put(&amp;quot;B&amp;quot;,200);                                                 //order in which keys were inserted into the map.&lt;br /&gt;
		h.put(&amp;quot;C&amp;quot;,300);&lt;br /&gt;
		h.put(&amp;quot;D&amp;quot;,400);&lt;br /&gt;
		h.put(&amp;quot;E&amp;quot;,500);&lt;br /&gt;
		&lt;br /&gt;
		Set s = h.entrySet();                                           //Returns a set containing key value pairs&lt;br /&gt;
		Iterator i = s.iterator();                                      &lt;br /&gt;
		&lt;br /&gt;
		while(i.hasNext())&lt;br /&gt;
		{&lt;br /&gt;
			Map.Entry me = (Map.Entry)i.next();                     //Each element in the set has to be casted to Map.Entry &lt;br /&gt;
			System.out.println(me.getKey()+&amp;quot; : &amp;quot;+me.getValue());    //to access the key/value &lt;br /&gt;
		}&lt;br /&gt;
       }&lt;br /&gt;
}&lt;br /&gt;
		&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
*OO languages generally provide inbuilt language support for exception handling. Most OO languages implement exception handling in terms of blocks&lt;br /&gt;
E.g.: Try...catch  blocks in Java&lt;br /&gt;
Here the catch and finally blocks are used to delimit a group of statements that can be called to handle exceptions at runtime. Also, we can see usage of conditional statements if...else in this case.&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
try{&lt;br /&gt;
    System.out.println(&amp;quot;Inside try block of testMethod!&amp;quot;);&lt;br /&gt;
    int p=1000, q=0, i=0;&lt;br /&gt;
    if(p&amp;gt;q){                                               //We can clearly see here that a Divide by Zero exception will occur.&lt;br /&gt;
         i=p/q;&lt;br /&gt;
    }&lt;br /&gt;
    else if(q&amp;gt;p){&lt;br /&gt;
         i=q/p;&lt;br /&gt;
    }&lt;br /&gt;
    else{                                                 //In this case p equals q&lt;br /&gt;
         i=p/q;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
catch(Exception e){&lt;br /&gt;
    System.out.println(&amp;quot;Inside catch block of testMethod!&amp;quot;);&lt;br /&gt;
    System.out.println(“Caution: your trying to Divide a number by Zero...”);&lt;br /&gt;
}&lt;br /&gt;
finally{                                                  &lt;br /&gt;
    System.out.println(&amp;quot;Inside finally block of testMethod!&amp;quot;);&lt;br /&gt;
    System.out.println(“This block executes everytime an exception is caught...”);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
*Certain OO languages like JAVA allow creation of anonymous classes which are basically blocks which don’t have a name but can be instantiated without actually making a separate class.&lt;br /&gt;
E.g.&lt;br /&gt;
Here ActionListener is an interface which has been implemented as anonymous block in the below JAVA example&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
import java.awt.Button;&lt;br /&gt;
&lt;br /&gt;
class Submit()&lt;br /&gt;
{&lt;br /&gt;
       public Submit()&lt;br /&gt;
       {&lt;br /&gt;
              Button button1 = new Button(&amp;quot;Submit&amp;quot;);&lt;br /&gt;
                &lt;br /&gt;
              button1.addActionListener(new ActionListener(){&lt;br /&gt;
                     public void actionPerformed (ActionEvent event){&lt;br /&gt;
                            System.out.println(&amp;quot;actionPerfomed is implemented as anonymous class here &amp;amp; is added as a listener to button1&amp;quot;);&lt;br /&gt;
                     }  &lt;br /&gt;
              });&lt;br /&gt;
       }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
*In a pure OO language like Ruby, blocks can also be used to defines closures(blocks, procs &amp;amp; lambdas)&amp;lt;ref&amp;gt;[http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas Sosinski, Robert. &amp;quot;Understanding Ruby Blocks, Procs and Lambdas.&amp;quot; N.p., 21 Dec. 2008. Web. 8 Sept. 2011.]&amp;lt;/ref&amp;gt;&lt;br /&gt;
E.g.&lt;br /&gt;
Given below is a method which yields control to a block in Ruby &amp;amp; also passes parameter to it at the same time.&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
def block_demo&lt;br /&gt;
    yield(&amp;quot;Smith&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
block_demo { |name| puts &amp;quot; Hello #{name} &amp;quot; }&lt;br /&gt;
&lt;br /&gt;
The Output will be&lt;br /&gt;
 Hello Smith &lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Block structured languages had their uses in earlier days when code size was small and the main focus was on efficiency(in terms of space &amp;amp; memory) of programs. However, these programs could not scale in size as maintenance and code complexity became an overhead.&lt;br /&gt;
&lt;br /&gt;
Object Oriented concepts like encapsulation, inheritance(which helps in code re-usability), interfaces &amp;amp; polymorphism allowed better visualization of the software problem and its design to handle this complexity. Moreover, class libraries and frameworks developed in these languages helped abstract the code complexity and allowed rapid program development. This led to a gradual transition from block oriented to object oriented languages for application development.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
#[http://golang.org/ The Go Programming Language by Google for multicore and networked machines]&lt;br /&gt;
#[http://download.oracle.com/javase/tutorial/java/concepts/ Object Oriented Concepts (Java Tutorials)] &lt;br /&gt;
#[http://www.daimi.au.dk/publications/PB/230/PB-230.pdf  Block Structure and Object Oriented Languages by Ole Lehrmann Madsen]&lt;br /&gt;
#[http://portal.acm.org/citation.cfm?id=356671 A Tutorial on Algol 68, by Andrew S. Tanenbaum, in Computing Surveys, Vol. 8, No. 2, June 1976, with Corrigenda (Vol. 9, No. 3, September 1977) ] &lt;br /&gt;
#[http://ruby-lang.org Official Ruby Webpage]&lt;br /&gt;
#[http://www.pascal-programming.info/index.php Pascal programming tutorials]&lt;br /&gt;
#[http://steve-yegge.blogspot.com/2008/05/dynamic-languages-strike-back.html Dynamic Languages Strike Back]&lt;br /&gt;
#[http://amath.colorado.edu/computing/mmm/funcproc.html Functional vs Procedural languages]&lt;/div&gt;</summary>
		<author><name>Mndsouza</name></author>
	</entry>
</feed>