<?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=Nmsarda</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=Nmsarda"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Nmsarda"/>
	<updated>2026-05-06T12:22:56Z</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_2012/ch2b_1w59_nm&amp;diff=70351</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 1w59 nm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_1w59_nm&amp;diff=70351"/>
		<updated>2012-11-18T21:54:18Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;SaaS - 3.7. Mixins and Duck Typing&amp;lt;/h1&amp;gt;&lt;br /&gt;
This wiki is aimed at acting as a complementary read to the [http://www.youtube.com/watch?v=Q1h-W0sWVoM SaaS - 3.7. Mixins and Duck Typing lecture][1]. This lecture describes three of the most important fundamentals of the Ruby language: Duck-Typing, Modules and Mixins. &lt;br /&gt;
&amp;lt;h1&amp;gt;Introduction&amp;lt;/h1&amp;gt;&lt;br /&gt;
*Duck-typing is one of the main features of many object-oriented languages. Duck-Typing describes how a language like Ruby, treats its objects as compared to a language like JAVA. We describe the beautiful features of Duck-typing and also its negative side-effects.&amp;lt;br&amp;gt;&lt;br /&gt;
*Modules define a namespace. It provides a mechanism to club several things together which naturally do-not form a common class. With modules, programmer does not have to worry of possible name-clashes of method name's with other methods written by different programmers. Modules handles this by making use of namespace.&amp;lt;br&amp;gt;&lt;br /&gt;
*Mixins extend from modules. modules are not classes and hence we cannot instantiate them. However, when modules are 'include'ed in a class, all its methods are available to that class. We say modules are 'mixed in' to the class or in other words 'mixins'. Modules which are mixed in behave as superclass and provide multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;[http://en.wikipedia.org/wiki/Duck_typing Duck Typing][2]&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;What is Duck Typing&amp;lt;/h2&amp;gt;&lt;br /&gt;
                     '''“If a object walks like a duck, quacks like a duck then treat it as a duck”'''&lt;br /&gt;
&lt;br /&gt;
[[File:Dtype.png|frame|left|[http://www.codecapers.com/post/Duck-Typing.aspx A cat or a duck or a cat walking like a duck?][3]]]&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;This is the ‘mantra’ of Ruby language and the most pleasing aspect of the language. What it means is that, whether an object responds to a method?, is only important and not the type of the object. &lt;br /&gt;
If a dragon responds to methods like swim and quacking, ruby is happy to treat dragon as a duck without having a concern of whether it actually is a duck or not. This gives powerful tools for a ruby programmer. &lt;br /&gt;
&lt;br /&gt;
This is possible in Ruby because we don’t declare the '[http://en.wikipedia.org/wiki/Programming_language#Type_system Type][4] of variables or methods like in JAVA; in Ruby, everything is an object and each object can be individually modified, irrespective of its implementation in the class to which it belongs. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the following example taken from the SAAS lecture:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
my_list.sort&lt;br /&gt;
&lt;br /&gt;
[5, 4, 3].sort&lt;br /&gt;
[&amp;quot;dog&amp;quot;, &amp;quot;cat&amp;quot;, &amp;quot;rat&amp;quot;].sort&lt;br /&gt;
[:a, :b, :c].sort&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here, the same function, sort, is responding to an array of integers, strings and literals. This is classic Ruby duck-typing at work. How sort works is that it compares the elements which are needed to be sorted. So as long as the elements can be compared, sort function works. In the above example, the array of integers, strings and literals implement the comparison operator and hence the same sort function works for all of them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; Why in Ruby and not in JAVA? &amp;lt;/h2&amp;gt;&lt;br /&gt;
The reason for duck-typing to be easily possible and implemented in language like Ruby and not in language like JAVA is that these 2 languages treat ‘type’ differently. &lt;br /&gt;
In JAVA or C++, type means class. When someone asks the type of “abc”, we say it is String. So, such language allows String objects to refer to only other String objects.&lt;br /&gt;
String a; a will never be allowed to refer to anything other than a string, like a=5.&lt;br /&gt;
&lt;br /&gt;
While in Ruby, type refers to how the object reacts to a method or in other words does the object have a particular capability. Type refers to this capability. While a class to which the object belongs provides the initial capabilities, from that point on, the object is free to extend its capabilities independently of its pre-defined class. So,&lt;br /&gt;
&lt;br /&gt;
 a= ”abc”&lt;br /&gt;
 a= 5 are two perfectly acceptable statements in Ruby.&lt;br /&gt;
&lt;br /&gt;
This is duck-typing.&lt;br /&gt;
[http://bigdingus.com/2007/12/08/just-what-is-this-javascript-object-you-handed-me/ A classic example][5] of how a scripting language, JavaScript, not implementing duck-typing can lead to some bad code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; Duck-Typing in action&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class American&lt;br /&gt;
  def football&lt;br /&gt;
    puts &amp;quot;We play it with hands&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class European&lt;br /&gt;
  def football&lt;br /&gt;
    puts &amp;quot;We play it with legs&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Martian&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
footballManiac = [American.new,European.new]&lt;br /&gt;
&lt;br /&gt;
footballManiac.each {|game| game.football}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output of this program is We play it with hands(next line) We play it with legs&lt;br /&gt;
&lt;br /&gt;
This is because of duck-typing. footballManiac first acted as an American class object and since that class responded to the football method, it executed it. Then it acted as an European class object and again that class responded to the method, it executed it. In static-typed languages, we would have to type cast each object and even then we might get compile time or run time error. Nothing of that sorts in Ruby. Now let us also create a Martian class object.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
footballManiac = [American.new,European.new,Martian.new]&lt;br /&gt;
&lt;br /&gt;
footballManiac.each {|game| game.football}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On executing it, we get the following error&lt;br /&gt;
&amp;lt;pre&amp;gt;undefined method `football' for #&amp;lt;Martian:0x3425358&amp;gt; (NoMethodError)&amp;lt;/pre&amp;gt;&lt;br /&gt;
This is obvious because martians do not play football. But the point is, Ruby still checked the martian class and when it found that the object does not respond to the method, it complained.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; The other side to Duck-Typing &amp;lt;/h2&amp;gt;&lt;br /&gt;
As with everything in life, there is always 2 perspectives about its usefulness. Following are some of the criticism against duck-typing :&lt;br /&gt;
&amp;lt;h3&amp;gt;Lack of compiler security&amp;lt;/h3&amp;gt;&lt;br /&gt;
There is no help from compiler for a programmer error like assigning a string to an integer and so on. Only when you run the code, the output might be funny and that’s when you realize a probable mistake in assignment. In a static-type language, there is always a compiler check and hence the problem does not arise. Obviously this problem can be alleviated by proper and exhaustive testing. But as we all know, no one can guarantee 100% code tests.&lt;br /&gt;
&amp;lt;h3&amp;gt;Contract between caller and the callee&amp;lt;/h3&amp;gt;&lt;br /&gt;
In a static type language, the caller knows the exact ‘type’ of parameters required by the callee and also the return-type from the callee. In dynamic type languages, no type is provided. So to understand what the callee expects, one has to look at the code and understand. This is difficult in most of the cases and mainly for third-party code. Again this problem can be solved by providing proper documentation of each API and the pre-requirements for execution. But who can guarantee that the documents correctly map the requirements?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Modules and Mixins&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;What are modules and mixins?&amp;lt;/h2&amp;gt;&lt;br /&gt;
In Ruby language modules facilitate grouping of classes, methods and constants. A module has been defined as collection of classes and instance methods but they are not a class so they can’t be instantiated. However, modules can be mixed-in a class. The implementation of module is intertwined with that of a class thus providing the functionality of a module to given class. Modules provide a namespace which prevents name clashes and implementation of mixin facility.&lt;br /&gt;
&lt;br /&gt;
The namespace facility is used when a class requires two methods from different classes having the same name. The class does not have the ability to recognize the methods independently which leads to name clashes. Consider the following example where Math module defines sin method and Action module defines sin method as well. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Math&lt;br /&gt;
PI = 3.141592654&lt;br /&gt;
     def Math.sin(x)&lt;br /&gt;
     #.....&lt;br /&gt;
     end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Action&lt;br /&gt;
BAD = -1&lt;br /&gt;
     def Action.sin(badness)&lt;br /&gt;
     #.....&lt;br /&gt;
     end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By implementing sin methods in different modules eliminates name clashes since the methods will now be referenced in a class which includes these modules. The modules can be included in a class using the require statement, this is shown as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require Math&lt;br /&gt;
require Action&lt;br /&gt;
&lt;br /&gt;
sinValue = Math.sin(Math::PI/2)&lt;br /&gt;
bad_action = Action.sin(Action::BAD)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Module methods][6] are invoked in the same manner as class methods by preceding its name with the module's name and a period, and by referencing a constant using the module name and two colons.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;[http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby Types of mixins][7]&amp;lt;/h2&amp;gt;&lt;br /&gt;
The following example demonstrates two types of mixins. Consider the following example of a module,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Example&lt;br /&gt;
&lt;br /&gt;
def example&lt;br /&gt;
     if @variable &amp;gt; 0&lt;br /&gt;
          return 1&lt;br /&gt;
     else&lt;br /&gt;
          return 0&lt;br /&gt;
     end &lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Example module makes use of @variable instance variable. The class which mixes this module has to define the @variable since the Example module makes use of @variable but does not define it. A module can make use of methods not defined within the module but the class in which it has been mixed in. Thus, this module exhibits dependency on the implementation of the class it is mixed with.&lt;br /&gt;
&lt;br /&gt;
The following example demonstrates an independent module ,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module AddSub&lt;br /&gt;
&lt;br /&gt;
#Class method&lt;br /&gt;
def add(var_one, var_two)&lt;br /&gt;
     SampleClass.new(var_one + var_two)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Addition module defines a class method as well as an instance method. Class methods are also known as static methods. The add method in the AddSub module defines accepts two integers and returns an instance of SampleClass. The mixin SampleClass class is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Base Integer class&lt;br /&gt;
Class Integer&lt;br /&gt;
  def value&lt;br /&gt;
    @variable&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# SampleClass extends Integer&lt;br /&gt;
class SampleClass &amp;lt; Integer&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
  # Add instance methods from Example&lt;br /&gt;
  include Stringify&lt;br /&gt;
 &lt;br /&gt;
  # Add class methods from AddSub&lt;br /&gt;
  extend AddSub&lt;br /&gt;
 &lt;br /&gt;
  # Add a constructor with one parameter&lt;br /&gt;
  def initialize(value)&lt;br /&gt;
    @variable = value&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The module methods have been mixed into the class using the include and extend methods respectively. The extend method will mix the methods as class/static methods.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Call class method from AddSub module&lt;br /&gt;
&lt;br /&gt;
object1 = SampleClass.add(2,3)&lt;br /&gt;
 puts object1.value	# -&amp;gt; 5&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The include method will mix the methods defined in the module as instance methods.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Call instance method from Example module&lt;br /&gt;
&lt;br /&gt;
puts object1.example	# -&amp;gt; 1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
However, care must be taken that including an instance method does not copy the module's instance methods into the class. A reference is generated from the class to the included module. This applies to all the classes which have included the module. If any change is made in the &lt;br /&gt;
instance method it will be reflected in the behavior of all the classes that include the &lt;br /&gt;
module.&lt;br /&gt;
The extend method comes with an additional feature. An object instance can be enhanced at run time by mixing it in a module at run time. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module  ScaleValue&lt;br /&gt;
     def multiply&lt;br /&gt;
          “#{@variable}*100”&lt;br /&gt;
     end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An object instance can be extended with a module using the following commands:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Adding module methods to this object instance only!&lt;br /&gt;
&lt;br /&gt;
object1.extend ScaleValue&lt;br /&gt;
puts object1.multiply # -&amp;gt; 500&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The extend method includes the module methods only on object1, the remaining objects will not this functionality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
puts object2.multiple # generate error&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Mix-in is a contract&amp;lt;/h2&amp;gt;&lt;br /&gt;
The Enumerable module provides various methods as a mixin to a given class. However, the module assumes that all the objects of a given class respond to &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; method. Apart from this, Enumerable provides methods which can be used to query an object whether it responds to a given method or not. Given the ability of an object to respond to &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; method which iterates over the collection of elements one can make use of following methods to find out the properties of an object.  Some of the methods Enumerable provides are as follows:&lt;br /&gt;
&lt;br /&gt;
•	all?, any?, collect, find, include?, inject, map, and partition&lt;br /&gt;
&lt;br /&gt;
In Java one can find the declaration “Array implements Enumerable”, no such declaration is found in Ruby. The reason being the methods in Enumerable module assume that the target objects are capable of responding to the methods invoked on them.&lt;br /&gt;
&lt;br /&gt;
Comparable is another module which assumes that objects of a given class respond to &amp;lt; = &amp;gt; (spaceship) operator. Similar to the Enumerable module, Comparable provides various methods for comparing the objects and some of them are mentioned below:&lt;br /&gt;
&lt;br /&gt;
•	&amp;lt;, &amp;lt;=, &amp;gt;, &amp;gt;=, ==, and between?&lt;br /&gt;
&lt;br /&gt;
Given a class which implements spaceship operator and &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; method, the objects can make use of sort method which requires implementation of spaceship operator so that the objects are capable to comparing with each other and the &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; method in order to retrieve the objects one at a time. By mixing modules within a class, an object can perform various actions such as map, reduce, and many more. The classes do not matter unless they respond to certain methods. Consider an example of constructing a data structure, by implementing &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; method the data structure can implement various methods such as sort, map, reduce etc in its own way. Thus, mixing modules facilitate behavior reuse which is an added advantage.&lt;br /&gt;
&lt;br /&gt;
The video lecture presents an example of Account class which implements spaceship operator. &lt;br /&gt;
The spaceship operator is used to compare the balance of two different objects.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Account&lt;br /&gt;
   include Comparable&lt;br /&gt;
        def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
             self.balance &amp;lt;=&amp;gt; other.balance&lt;br /&gt;
        end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On defining the spaceship operator the objects of Account class can define of all the other methods Comparable module provides. In this manner the Account class takes advantage of behavior reuse.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt; Conclusion &amp;lt;/h1&amp;gt;&lt;br /&gt;
Duck typing is a very powerful tool in the hands of the programmer and it increases the speed of production. If used wisely, it is a boon. But if written in a non-sensical manner, then tracking errors becomes difficult and one has to trace the code to find out the errors. Modules provide a great way to stuff in the non-related or related code under one namespace and when it is mixed-in to a class, it acts as a superclass providing a path to do multiple inheritance, if the need be.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;References&amp;lt;/h1&amp;gt;&lt;br /&gt;
*[1] http://www.youtube.com/watch?v=Q1h-W0sWVoM&lt;br /&gt;
*[2] http://en.wikipedia.org/wiki/Duck_typing&lt;br /&gt;
*[3] http://www.codecapers.com/post/Duck-Typing.aspx&lt;br /&gt;
*[4] http://en.wikipedia.org/wiki/Programming_language#Type_system&lt;br /&gt;
*[5] http://bigdingus.com/2007/12/08/just-what-is-this-javascript-object-you-handed-me/&lt;br /&gt;
*[6] http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html&lt;br /&gt;
*[7] http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Additional Reading&amp;lt;/h1&amp;gt;&lt;br /&gt;
* Sam Ruby, Dave Thomas, David Heinemeier Hansson.agile web development with rails (fourth edition)&lt;br /&gt;
* [http://beust.com/weblog/2005/04/15/the-perils-of-duck-typing/ Perils of Duck Typing]&lt;br /&gt;
* [http://stackoverflow.com/questions/4205130/what-is-duck-typing What is Duck Typing : Stack Overflow]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Type_system What actually do you mean by 'Type']&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012&amp;diff=70349</id>
		<title>CSC/ECE 517 Fall 2012</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012&amp;diff=70349"/>
		<updated>2012-11-18T21:33:19Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*[[CSC/ECE_517_Fall_2012/Table_Of_Contents]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 n xx]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w1 rk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w20 pp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w5 su]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w6 pp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w4 aj]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w7 am]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w8 aa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w9 av]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w10 pk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w11 ap]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w12 mv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w14 gv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w17 ir]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w18 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w22 an]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w21 aa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w21 wi]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w31 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w16 br]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w23 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w24 nr]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w15 rt]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w3 pl]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w32 cm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w5 dp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w37 ss]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w67 ks]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w27 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w29 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w33 op]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w19 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w34 vd]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w35 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w30 rp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w58 am]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w47 sk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w69 mv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w44 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w45 is]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w53 kc]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w40 ar]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w39 sn]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w54 go]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w56 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w64 nn]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w66 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w40 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w42 js]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w46 sm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w71 gs]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w63 dv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w55 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w57 mp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w52 an]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch1b 1w38 nm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w60 ac]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w62 rb]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w29 st]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w3_sm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w30 an]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w17 pt]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w31 up]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w9 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w19 is]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w26 aj]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w5 dp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w16 dp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w8 vp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w18 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w3 jm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w23 sr]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w11_aa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w15 rr]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w33 pv]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w20_aa]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w14_bb]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w21_ap]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w13_sm]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w4_sa]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w25_nr]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w12_sv]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w7_ma]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w6_ar]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w32_mk]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w10_rc]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w70_sm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w67_sk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w40_sn]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w22_sk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w-1w65_am]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w59_bc]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w60_ns]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b 2w47 am]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w69_as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w39_ka]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w36_av]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w37_ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w43_iv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w53_iv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w63_sp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w49_ps]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w52_sj]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w28_dh]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 2w41 dc]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2b_1w59_nm]]&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_1w59_nm&amp;diff=70348</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 1w59 nm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_1w59_nm&amp;diff=70348"/>
		<updated>2012-11-18T21:31:52Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;SaaS - 3.7. Mixins and Duck Typing&amp;lt;/h1&amp;gt;&lt;br /&gt;
This wiki is aimed at acting as a complementary read to the [http://www.youtube.com/watch?v=Q1h-W0sWVoM SaaS - 3.7. Mixins and Duck Typing lecture][1]. This lecture describes three of the most important fundamentals of the Ruby language: Duck-Typing, Modules and Mixins. &lt;br /&gt;
&amp;lt;h1&amp;gt;Introduction&amp;lt;/h1&amp;gt;&lt;br /&gt;
*Duck-typing is one of the main features of many object-oriented languages. Duck-Typing describes how a language like Ruby, treats its objects as compared to a language like JAVA. We describe the beautiful features of Duck-typing and also its negative side-effects.&amp;lt;br&amp;gt;&lt;br /&gt;
*Modules define a namespace. It provides a mechanism to club several things together which naturally do-not form a common class. With modules, programmer does not have to worry of possible name-clashes of method name's with other methods written by different programmers. Modules handles this by making use of namespace.&amp;lt;br&amp;gt;&lt;br /&gt;
*Mixins extend from modules. modules are not classes and hence we cannot instantiate them. However, when modules are 'include'ed in a class, all its methods are available to that class. We say modules are 'mixed in' to the class or in other words 'mixins'. Modules which are mixed in behave as superclass and provide multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;[http://en.wikipedia.org/wiki/Duck_typing Duck Typing][2]&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;What is Duck Typing&amp;lt;/h2&amp;gt;&lt;br /&gt;
                     '''“If a object walks like a duck, quacks like a duck then treat it as a duck”'''&lt;br /&gt;
&lt;br /&gt;
[[File:Dtype.png|frame|left|[http://www.codecapers.com/post/Duck-Typing.aspx A cat or a duck or a cat walking like a duck?][3]]]&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;This is the ‘mantra’ of Ruby language and the most pleasing aspect of the language. What it means is that, whether an object responds to a method?, is only important and not the type of the object. &lt;br /&gt;
If a dragon responds to methods like swim and quacking, ruby is happy to treat dragon as a duck without having a concern of whether it actually is a duck or not. This gives powerful tools for a ruby programmer. &lt;br /&gt;
&lt;br /&gt;
This is possible in Ruby because we don’t declare the '[http://en.wikipedia.org/wiki/Programming_language#Type_system Type][4] of variables or methods like in JAVA; in Ruby, everything is an object and each object can be individually modified, irrespective of its implementation in the class to which it belongs. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the following example taken from the SAAS lecture:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
my_list.sort&lt;br /&gt;
&lt;br /&gt;
[5, 4, 3].sort&lt;br /&gt;
[&amp;quot;dog&amp;quot;, &amp;quot;cat&amp;quot;, &amp;quot;rat&amp;quot;].sort&lt;br /&gt;
[:a, :b, :c].sort&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here, the same function, sort, is responding to an array of integers, strings and literals. This is classic Ruby duck-typing at work. How sort works is that it compares the elements which are needed to be sorted. So as long as the elements can be compared, sort function works. In the above example, the array of integers, strings and literals implement the comparison operator and hence the same sort function works for all of them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; Why in Ruby and not in JAVA? &amp;lt;/h2&amp;gt;&lt;br /&gt;
The reason for duck-typing to be easily possible and implemented in language like Ruby and not in language like JAVA is that these 2 languages treat ‘type’ differently. &lt;br /&gt;
In JAVA or C++, type means class. When someone asks the type of “abc”, we say it is String. So, such language allows String objects to refer to only other String objects.&lt;br /&gt;
String a; a will never be allowed to refer to anything other than a string, like a=5.&lt;br /&gt;
&lt;br /&gt;
While in Ruby, type refers to how the object reacts to a method or in other words does the object have a particular capability. Type refers to this capability. While a class to which the object belongs provides the initial capabilities, from that point on, the object is free to extend its capabilities independently of its pre-defined class. So,&lt;br /&gt;
&lt;br /&gt;
 a= ”abc”&lt;br /&gt;
 a= 5 are two perfectly acceptable statements in Ruby.&lt;br /&gt;
&lt;br /&gt;
This is duck-typing.&lt;br /&gt;
[http://bigdingus.com/2007/12/08/just-what-is-this-javascript-object-you-handed-me/ A classic example][5] of how a scripting language, JavaScript, not implementing duck-typing can lead to some bad code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; Duck-Typing in action&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class American&lt;br /&gt;
  def football&lt;br /&gt;
    puts &amp;quot;We play it with hands&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class European&lt;br /&gt;
  def football&lt;br /&gt;
    puts &amp;quot;We play it with legs&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Martian&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
footballManiac = [American.new,European.new]&lt;br /&gt;
&lt;br /&gt;
footballManiac.each {|game| game.football}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output of this program is We play it with hands(next line) We play it with legs&lt;br /&gt;
&lt;br /&gt;
This is because of duck-typing. footballManiac first acted as an American class object and since that class responded to the football method, it executed it. Then it acted as an European class object and again that class responded to the method, it executed it. In static-typed languages, we would have to type cast each object and even then we might get compile time or run time error. Nothing of that sorts in Ruby. Now let us also create a Martian class object.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
footballManiac = [American.new,European.new,Martian.new]&lt;br /&gt;
&lt;br /&gt;
footballManiac.each {|game| game.football}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On executing it, we get the following error&lt;br /&gt;
&amp;lt;pre&amp;gt;undefined method `football' for #&amp;lt;Martian:0x3425358&amp;gt; (NoMethodError)&amp;lt;/pre&amp;gt;&lt;br /&gt;
This is obvious because martians do not play football. But the point is, Ruby still checked the martian class and when it found that the object does not respond to the method, it complained.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; The other side to Duck-Typing &amp;lt;/h2&amp;gt;&lt;br /&gt;
As with everything in life, there is always 2 perspectives about its usefulness. Following are some of the criticism against duck-typing :&lt;br /&gt;
&amp;lt;h3&amp;gt;Lack of compiler security&amp;lt;/h3&amp;gt;&lt;br /&gt;
There is no help from compiler for a programmer error like assigning a string to an integer and so on. Only when you run the code, the output might be funny and that’s when you realize a probable mistake in assignment. In a static-type language, there is always a compiler check and hence the problem does not arise. Obviously this problem can be alleviated by proper and exhaustive testing. But as we all know, no one can guarantee 100% code tests.&lt;br /&gt;
&amp;lt;h3&amp;gt;Contract between caller and the callee&amp;lt;/h3&amp;gt;&lt;br /&gt;
In a static type language, the caller knows the exact ‘type’ of parameters required by the callee and also the return-type from the callee. In dynamic type languages, no type is provided. So to understand what the callee expects, one has to look at the code and understand. This is difficult in most of the cases and mainly for third-party code. Again this problem can be solved by providing proper documentation of each API and the pre-requirements for execution. But who can guarantee that the documents correctly map the requirements?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Modules and Mixins&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;What are modules and mixins?&amp;lt;/h2&amp;gt;&lt;br /&gt;
In Ruby language modules facilitate grouping of classes, methods and constants. A module has been defined as collection of classes and instance methods but they are not a class so they can’t be instantiated. However, modules can be mixed-in a class. The implementation of module is intertwined with that of a class thus providing the functionality of a module to given class. Modules provide a namespace which prevents name clashes and implementation of mixin facility.&lt;br /&gt;
&lt;br /&gt;
The namespace facility is used when a class requires two methods from different classes having the same name. The class does not have the ability to recognize the methods independently which leads to name clashes. Consider the following example where Math module defines sin method and Action module defines sin method as well. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Math&lt;br /&gt;
PI = 3.141592654&lt;br /&gt;
     def Math.sin(x)&lt;br /&gt;
     #.....&lt;br /&gt;
     end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Action&lt;br /&gt;
BAD = -1&lt;br /&gt;
     def Action.sin(badness)&lt;br /&gt;
     #.....&lt;br /&gt;
     end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By implementing sin methods in different modules eliminates name clashes since the methods will now be referenced in a class which includes these modules. The modules can be included in a class using the require statement, this is shown as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require Math&lt;br /&gt;
require Action&lt;br /&gt;
&lt;br /&gt;
sinValue = Math.sin(Math::PI/2)&lt;br /&gt;
bad_action = Action.sin(Action::BAD)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Module methods][6] are invoked in the same manner as class methods by preceding its name with the module's name and a period, and by referencing a constant using the module name and two colons.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;[http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby Types of mixins][7]&amp;lt;/h2&amp;gt;&lt;br /&gt;
The following example demonstrates two types of mixins. Consider the following example of a module,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Example&lt;br /&gt;
&lt;br /&gt;
def example&lt;br /&gt;
     if @variable &amp;gt; 0&lt;br /&gt;
          return 1&lt;br /&gt;
     else&lt;br /&gt;
          return 0&lt;br /&gt;
     end &lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Example module makes use of @variable instance variable. The class which mixes this module has to define the @variable since the Example module makes use of @variable but does not define it. A module can make use of methods not defined within the module but the class in which it has been mixed in. Thus, this module exhibits dependency on the implementation of the class it is mixed with.&lt;br /&gt;
&lt;br /&gt;
The following example demonstrates an independent module ,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module AddSub&lt;br /&gt;
&lt;br /&gt;
#Class method&lt;br /&gt;
def add(var_one, var_two)&lt;br /&gt;
     SampleClass.new(var_one + var_two)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Addition module defines a class method as well as an instance method. Class methods are also known as static methods. The add method in the AddSub module defines accepts two integers and returns an instance of SampleClass. The mixin SampleClass class is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Base Integer class&lt;br /&gt;
Class Integer&lt;br /&gt;
  def value&lt;br /&gt;
    @variable&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# SampleClass extends Integer&lt;br /&gt;
class SampleClass &amp;lt; Integer&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
  # Add instance methods from Example&lt;br /&gt;
  include Stringify&lt;br /&gt;
 &lt;br /&gt;
  # Add class methods from AddSub&lt;br /&gt;
  extend AddSub&lt;br /&gt;
 &lt;br /&gt;
  # Add a constructor with one parameter&lt;br /&gt;
  def initialize(value)&lt;br /&gt;
    @variable = value&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The module methods have been mixed into the class using the include and extend methods respectively. The extend method will mix the methods as class/static methods.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Call class method from AddSub module&lt;br /&gt;
&lt;br /&gt;
object1 = SampleClass.add(2,3)&lt;br /&gt;
 puts object1.value	# -&amp;gt; 5&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The include method will mix the methods defined in the module as instance methods.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Call instance method from Example module&lt;br /&gt;
&lt;br /&gt;
puts object1.example	# -&amp;gt; 1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
However, care must be taken that including an instance method does not copy the module's instance methods into the class. A reference is generated from the class to the included module. This applies to all the classes which have included the module. If any change is made in the &lt;br /&gt;
instance method it will be reflected in the behavior of all the classes that include the &lt;br /&gt;
module.&lt;br /&gt;
The extend method comes with an additional feature. An object instance can be enhanced at run time by mixing it in a module at run time. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module  ScaleValue&lt;br /&gt;
     def multiply&lt;br /&gt;
          “#{@variable}*100”&lt;br /&gt;
     end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An object instance can be extended with a module using the following commands:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Adding module methods to this object instance only!&lt;br /&gt;
&lt;br /&gt;
object1.extend ScaleValue&lt;br /&gt;
puts object1.multiply # -&amp;gt; 500&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The extend method includes the module methods only on object1, the remaining objects will not this functionality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
puts object2.multiple # generate error&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Mix-in is a contract&amp;lt;/h2&amp;gt;&lt;br /&gt;
The Enumerable module provides various methods as a mixin to a given class. However, the module assumes that all the objects of a given class respond to &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; method. Apart from this, Enumerable provides methods which can be used to query an object whether it responds to a given method or not. Given the ability of an object to respond to &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; method which iterates over the collection of elements one can make use of following methods to find out the properties of an object.  Some of the methods Enumerable provides are as follows:&lt;br /&gt;
&lt;br /&gt;
•	all?, any?, collect, find, include?, inject, map, and partition&lt;br /&gt;
&lt;br /&gt;
In Java one can find the declaration “Array implements Enumerable”, no such declaration is found in Ruby. The reason being the methods in Enumerable module assume that the target objects are capable of responding to the methods invoked on them.&lt;br /&gt;
&lt;br /&gt;
Comparable is another module which assumes that objects of a given class respond to &amp;lt; = &amp;gt; (spaceship) operator. Similar to the Enumerable module, Comparable provides various methods for comparing the objects and some of them are mentioned below:&lt;br /&gt;
&lt;br /&gt;
•	&amp;lt;, &amp;lt;=, &amp;gt;, &amp;gt;=, ==, and between?&lt;br /&gt;
&lt;br /&gt;
Given a class which implements spaceship operator and &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; method, the objects can make use of sort method which requires implementation of spaceship operator so that the objects are capable to comparing with each other and the &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; method in order to retrieve the objects one at a time. By mixing modules within a class, an object can perform various actions such as map, reduce, and many more. The classes do not matter unless they respond to certain methods. Consider an example of constructing a data structure, by implementing &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; method the data structure can implement various methods such as sort, map, reduce etc in its own way. Thus, mixing modules facilitate behavior reuse which is an added advantage.&lt;br /&gt;
&lt;br /&gt;
The video lecture presents an example of Account class which implements spaceship operator. &lt;br /&gt;
The spaceship operator is used to compare the balance of two different objects.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Account&lt;br /&gt;
   include Comparable&lt;br /&gt;
        def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
             self.balance &amp;lt;=&amp;gt; other.balance&lt;br /&gt;
        end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On defining the spaceship operator the objects of Account class can define of all the other methods Comparable module provides. In this manner the Account class takes advantage of behavior reuse.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;References&amp;lt;/h1&amp;gt;&lt;br /&gt;
*[1] http://www.youtube.com/watch?v=Q1h-W0sWVoM&lt;br /&gt;
*[2] http://en.wikipedia.org/wiki/Duck_typing&lt;br /&gt;
*[3] http://www.codecapers.com/post/Duck-Typing.aspx&lt;br /&gt;
*[4] http://en.wikipedia.org/wiki/Programming_language#Type_system&lt;br /&gt;
*[5] http://bigdingus.com/2007/12/08/just-what-is-this-javascript-object-you-handed-me/&lt;br /&gt;
*[6] http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html&lt;br /&gt;
*[7] http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Additional Reading&amp;lt;/h1&amp;gt;&lt;br /&gt;
* Sam Ruby, Dave Thomas, David Heinemeier Hansson.agile web development with rails (fourth edition)&lt;br /&gt;
* [http://beust.com/weblog/2005/04/15/the-perils-of-duck-typing/ Perils of Duck Typing]&lt;br /&gt;
* [http://stackoverflow.com/questions/4205130/what-is-duck-typing What is Duck Typing : Stack Overflow]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Type_system What actually do you mean by 'Type']&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_1w59_nm&amp;diff=70347</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 1w59 nm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_1w59_nm&amp;diff=70347"/>
		<updated>2012-11-18T21:31:11Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;SaaS - 3.7. Mixins and Duck Typing&amp;lt;/h1&amp;gt;&lt;br /&gt;
This wiki is aimed at acting as a complementary read to the [http://www.youtube.com/watch?v=Q1h-W0sWVoM SaaS - 3.7. Mixins and Duck Typing lecture][1]. This lecture describes three of the most important fundamentals of the Ruby language: Duck-Typing, Modules and Mixins. &lt;br /&gt;
&amp;lt;h1&amp;gt;Introduction&amp;lt;/h1&amp;gt;&lt;br /&gt;
*Duck-typing is one of the main features of many object-oriented languages. Duck-Typing describes how a language like Ruby, treats its objects as compared to a language like JAVA. We describe the beautiful features of Duck-typing and also its negative side-effects.&amp;lt;br&amp;gt;&lt;br /&gt;
*Modules define a namespace. It provides a mechanism to club several things together which naturally do-not form a common class. With modules, programmer does not have to worry of possible name-clashes of method name's with other methods written by different programmers. Modules handles this by making use of namespace.&amp;lt;br&amp;gt;&lt;br /&gt;
*Mixins extend from modules. modules are not classes and hence we cannot instantiate them. However, when modules are 'include'ed in a class, all its methods are available to that class. We say modules are 'mixed in' to the class or in other words 'mixins'. Modules which are mixed in behave as superclass and provide multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Scope&amp;lt;/h2&amp;gt;&lt;br /&gt;
The scope of this article is limited to the scope covered in video lecture. The lecture starts with an introduction of duck typing where each object takes care of the methods it can handle and modules which facilitate inheritance in Ruby classes. The video lecture illustrates how different classes respond to instance methods by including modules. &lt;br /&gt;
 &lt;br /&gt;
Hence for the purpose of this article it is assumed that the reader has already read the relevant material and watched the video lecture.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;[http://en.wikipedia.org/wiki/Duck_typing Duck Typing][2]&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;What is Duck Typing&amp;lt;/h2&amp;gt;&lt;br /&gt;
                     '''“If a object walks like a duck, quacks like a duck then treat it as a duck”'''&lt;br /&gt;
&lt;br /&gt;
[[File:Dtype.png|frame|left|[http://www.codecapers.com/post/Duck-Typing.aspx A cat or a duck or a cat walking like a duck?][3]]]&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;This is the ‘mantra’ of Ruby language and the most pleasing aspect of the language. What it means is that, whether an object responds to a method?, is only important and not the type of the object. &lt;br /&gt;
If a dragon responds to methods like swim and quacking, ruby is happy to treat dragon as a duck without having a concern of whether it actually is a duck or not. This gives powerful tools for a ruby programmer. &lt;br /&gt;
&lt;br /&gt;
This is possible in Ruby because we don’t declare the '[http://en.wikipedia.org/wiki/Programming_language#Type_system Type][4] of variables or methods like in JAVA; in Ruby, everything is an object and each object can be individually modified, irrespective of its implementation in the class to which it belongs. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the following example taken from the SAAS lecture:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
my_list.sort&lt;br /&gt;
&lt;br /&gt;
[5, 4, 3].sort&lt;br /&gt;
[&amp;quot;dog&amp;quot;, &amp;quot;cat&amp;quot;, &amp;quot;rat&amp;quot;].sort&lt;br /&gt;
[:a, :b, :c].sort&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here, the same function, sort, is responding to an array of integers, strings and literals. This is classic Ruby duck-typing at work. How sort works is that it compares the elements which are needed to be sorted. So as long as the elements can be compared, sort function works. In the above example, the array of integers, strings and literals implement the comparison operator and hence the same sort function works for all of them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; Why in Ruby and not in JAVA? &amp;lt;/h2&amp;gt;&lt;br /&gt;
The reason for duck-typing to be easily possible and implemented in language like Ruby and not in language like JAVA is that these 2 languages treat ‘type’ differently. &lt;br /&gt;
In JAVA or C++, type means class. When someone asks the type of “abc”, we say it is String. So, such language allows String objects to refer to only other String objects.&lt;br /&gt;
String a; a will never be allowed to refer to anything other than a string, like a=5.&lt;br /&gt;
&lt;br /&gt;
While in Ruby, type refers to how the object reacts to a method or in other words does the object have a particular capability. Type refers to this capability. While a class to which the object belongs provides the initial capabilities, from that point on, the object is free to extend its capabilities independently of its pre-defined class. So,&lt;br /&gt;
&lt;br /&gt;
 a= ”abc”&lt;br /&gt;
 a= 5 are two perfectly acceptable statements in Ruby.&lt;br /&gt;
&lt;br /&gt;
This is duck-typing.&lt;br /&gt;
[http://bigdingus.com/2007/12/08/just-what-is-this-javascript-object-you-handed-me/ A classic example][5] of how a scripting language, JavaScript, not implementing duck-typing can lead to some bad code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; Duck-Typing in action&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class American&lt;br /&gt;
  def football&lt;br /&gt;
    puts &amp;quot;We play it with hands&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class European&lt;br /&gt;
  def football&lt;br /&gt;
    puts &amp;quot;We play it with legs&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Martian&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
footballManiac = [American.new,European.new]&lt;br /&gt;
&lt;br /&gt;
footballManiac.each {|game| game.football}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output of this program is We play it with hands(next line) We play it with legs&lt;br /&gt;
&lt;br /&gt;
This is because of duck-typing. footballManiac first acted as an American class object and since that class responded to the football method, it executed it. Then it acted as an European class object and again that class responded to the method, it executed it. In static-typed languages, we would have to type cast each object and even then we might get compile time or run time error. Nothing of that sorts in Ruby. Now let us also create a Martian class object.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
footballManiac = [American.new,European.new,Martian.new]&lt;br /&gt;
&lt;br /&gt;
footballManiac.each {|game| game.football}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On executing it, we get the following error&lt;br /&gt;
&amp;lt;pre&amp;gt;undefined method `football' for #&amp;lt;Martian:0x3425358&amp;gt; (NoMethodError)&amp;lt;/pre&amp;gt;&lt;br /&gt;
This is obvious because martians do not play football. But the point is, Ruby still checked the martian class and when it found that the object does not respond to the method, it complained.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; The other side to Duck-Typing &amp;lt;/h2&amp;gt;&lt;br /&gt;
As with everything in life, there is always 2 perspectives about its usefulness. Following are some of the criticism against duck-typing :&lt;br /&gt;
&amp;lt;h3&amp;gt;Lack of compiler security&amp;lt;/h3&amp;gt;&lt;br /&gt;
There is no help from compiler for a programmer error like assigning a string to an integer and so on. Only when you run the code, the output might be funny and that’s when you realize a probable mistake in assignment. In a static-type language, there is always a compiler check and hence the problem does not arise. Obviously this problem can be alleviated by proper and exhaustive testing. But as we all know, no one can guarantee 100% code tests.&lt;br /&gt;
&amp;lt;h3&amp;gt;Contract between caller and the callee&amp;lt;/h3&amp;gt;&lt;br /&gt;
In a static type language, the caller knows the exact ‘type’ of parameters required by the callee and also the return-type from the callee. In dynamic type languages, no type is provided. So to understand what the callee expects, one has to look at the code and understand. This is difficult in most of the cases and mainly for third-party code. Again this problem can be solved by providing proper documentation of each API and the pre-requirements for execution. But who can guarantee that the documents correctly map the requirements?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Modules and Mixins&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;What are modules and mixins?&amp;lt;/h2&amp;gt;&lt;br /&gt;
In Ruby language modules facilitate grouping of classes, methods and constants. A module has been defined as collection of classes and instance methods but they are not a class so they can’t be instantiated. However, modules can be mixed-in a class. The implementation of module is intertwined with that of a class thus providing the functionality of a module to given class. Modules provide a namespace which prevents name clashes and implementation of mixin facility.&lt;br /&gt;
&lt;br /&gt;
The namespace facility is used when a class requires two methods from different classes having the same name. The class does not have the ability to recognize the methods independently which leads to name clashes. Consider the following example where Math module defines sin method and Action module defines sin method as well. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Math&lt;br /&gt;
PI = 3.141592654&lt;br /&gt;
     def Math.sin(x)&lt;br /&gt;
     #.....&lt;br /&gt;
     end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Action&lt;br /&gt;
BAD = -1&lt;br /&gt;
     def Action.sin(badness)&lt;br /&gt;
     #.....&lt;br /&gt;
     end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By implementing sin methods in different modules eliminates name clashes since the methods will now be referenced in a class which includes these modules. The modules can be included in a class using the require statement, this is shown as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require Math&lt;br /&gt;
require Action&lt;br /&gt;
&lt;br /&gt;
sinValue = Math.sin(Math::PI/2)&lt;br /&gt;
bad_action = Action.sin(Action::BAD)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Module methods][6] are invoked in the same manner as class methods by preceding its name with the module's name and a period, and by referencing a constant using the module name and two colons.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;[http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby Types of mixins][7]&amp;lt;/h2&amp;gt;&lt;br /&gt;
The following example demonstrates two types of mixins. Consider the following example of a module,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Example&lt;br /&gt;
&lt;br /&gt;
def example&lt;br /&gt;
     if @variable &amp;gt; 0&lt;br /&gt;
          return 1&lt;br /&gt;
     else&lt;br /&gt;
          return 0&lt;br /&gt;
     end &lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Example module makes use of @variable instance variable. The class which mixes this module has to define the @variable since the Example module makes use of @variable but does not define it. A module can make use of methods not defined within the module but the class in which it has been mixed in. Thus, this module exhibits dependency on the implementation of the class it is mixed with.&lt;br /&gt;
&lt;br /&gt;
The following example demonstrates an independent module ,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module AddSub&lt;br /&gt;
&lt;br /&gt;
#Class method&lt;br /&gt;
def add(var_one, var_two)&lt;br /&gt;
     SampleClass.new(var_one + var_two)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Addition module defines a class method as well as an instance method. Class methods are also known as static methods. The add method in the AddSub module defines accepts two integers and returns an instance of SampleClass. The mixin SampleClass class is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Base Integer class&lt;br /&gt;
Class Integer&lt;br /&gt;
  def value&lt;br /&gt;
    @variable&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# SampleClass extends Integer&lt;br /&gt;
class SampleClass &amp;lt; Integer&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
  # Add instance methods from Example&lt;br /&gt;
  include Stringify&lt;br /&gt;
 &lt;br /&gt;
  # Add class methods from AddSub&lt;br /&gt;
  extend AddSub&lt;br /&gt;
 &lt;br /&gt;
  # Add a constructor with one parameter&lt;br /&gt;
  def initialize(value)&lt;br /&gt;
    @variable = value&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The module methods have been mixed into the class using the include and extend methods respectively. The extend method will mix the methods as class/static methods.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Call class method from AddSub module&lt;br /&gt;
&lt;br /&gt;
object1 = SampleClass.add(2,3)&lt;br /&gt;
 puts object1.value	# -&amp;gt; 5&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The include method will mix the methods defined in the module as instance methods.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Call instance method from Example module&lt;br /&gt;
&lt;br /&gt;
puts object1.example	# -&amp;gt; 1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
However, care must be taken that including an instance method does not copy the module's instance methods into the class. A reference is generated from the class to the included module. This applies to all the classes which have included the module. If any change is made in the &lt;br /&gt;
instance method it will be reflected in the behavior of all the classes that include the &lt;br /&gt;
module.&lt;br /&gt;
The extend method comes with an additional feature. An object instance can be enhanced at run time by mixing it in a module at run time. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module  ScaleValue&lt;br /&gt;
     def multiply&lt;br /&gt;
          “#{@variable}*100”&lt;br /&gt;
     end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An object instance can be extended with a module using the following commands:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Adding module methods to this object instance only!&lt;br /&gt;
&lt;br /&gt;
object1.extend ScaleValue&lt;br /&gt;
puts object1.multiply # -&amp;gt; 500&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The extend method includes the module methods only on object1, the remaining objects will not this functionality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
puts object2.multiple # generate error&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Mix-in is a contract&amp;lt;/h2&amp;gt;&lt;br /&gt;
The Enumerable module provides various methods as a mixin to a given class. However, the module assumes that all the objects of a given class respond to &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; method. Apart from this, Enumerable provides methods which can be used to query an object whether it responds to a given method or not. Given the ability of an object to respond to &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; method which iterates over the collection of elements one can make use of following methods to find out the properties of an object.  Some of the methods Enumerable provides are as follows:&lt;br /&gt;
&lt;br /&gt;
•	all?, any?, collect, find, include?, inject, map, and partition&lt;br /&gt;
&lt;br /&gt;
In Java one can find the declaration “Array implements Enumerable”, no such declaration is found in Ruby. The reason being the methods in Enumerable module assume that the target objects are capable of responding to the methods invoked on them.&lt;br /&gt;
&lt;br /&gt;
Comparable is another module which assumes that objects of a given class respond to &amp;lt; = &amp;gt; (spaceship) operator. Similar to the Enumerable module, Comparable provides various methods for comparing the objects and some of them are mentioned below:&lt;br /&gt;
&lt;br /&gt;
•	&amp;lt;, &amp;lt;=, &amp;gt;, &amp;gt;=, ==, and between?&lt;br /&gt;
&lt;br /&gt;
Given a class which implements spaceship operator and &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; method, the objects can make use of sort method which requires implementation of spaceship operator so that the objects are capable to comparing with each other and the &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; method in order to retrieve the objects one at a time. By mixing modules within a class, an object can perform various actions such as map, reduce, and many more. The classes do not matter unless they respond to certain methods. Consider an example of constructing a data structure, by implementing &amp;lt;i&amp;gt;each&amp;lt;/i&amp;gt; method the data structure can implement various methods such as sort, map, reduce etc in its own way. Thus, mixing modules facilitate behavior reuse which is an added advantage.&lt;br /&gt;
&lt;br /&gt;
The video lecture presents an example of Account class which implements spaceship operator. &lt;br /&gt;
The spaceship operator is used to compare the balance of two different objects.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Account&lt;br /&gt;
   include Comparable&lt;br /&gt;
        def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
             self.balance &amp;lt;=&amp;gt; other.balance&lt;br /&gt;
        end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On defining the spaceship operator the objects of Account class can define of all the other methods Comparable module provides. In this manner the Account class takes advantage of behavior reuse.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;References&amp;lt;/h1&amp;gt;&lt;br /&gt;
*[1] http://www.youtube.com/watch?v=Q1h-W0sWVoM&lt;br /&gt;
*[2] http://en.wikipedia.org/wiki/Duck_typing&lt;br /&gt;
*[3] http://www.codecapers.com/post/Duck-Typing.aspx&lt;br /&gt;
*[4] http://en.wikipedia.org/wiki/Programming_language#Type_system&lt;br /&gt;
*[5] http://bigdingus.com/2007/12/08/just-what-is-this-javascript-object-you-handed-me/&lt;br /&gt;
*[6] http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html&lt;br /&gt;
*[7] http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Additional Reading&amp;lt;/h1&amp;gt;&lt;br /&gt;
* Sam Ruby, Dave Thomas, David Heinemeier Hansson.agile web development with rails (fourth edition)&lt;br /&gt;
* [http://beust.com/weblog/2005/04/15/the-perils-of-duck-typing/ Perils of Duck Typing]&lt;br /&gt;
* [http://stackoverflow.com/questions/4205130/what-is-duck-typing What is Duck Typing : Stack Overflow]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Type_system What actually do you mean by 'Type']&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_1w59_nm&amp;diff=70342</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 1w59 nm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_1w59_nm&amp;diff=70342"/>
		<updated>2012-11-18T20:58:32Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;SaaS - 3.7. Mixins and Duck Typing&amp;lt;/h1&amp;gt;&lt;br /&gt;
This wiki is aimed at acting as a complementary read to the [http://www.youtube.com/watch?v=Q1h-W0sWVoM SaaS - 3.7. Mixins and Duck Typing lecture]. This lecture describes three of the most important fundamentals of the Ruby language: Duck-Typing, Modules and Mixins. &lt;br /&gt;
&amp;lt;h1&amp;gt;Introduction&amp;lt;/h1&amp;gt;&lt;br /&gt;
*Duck-typing is one of the main features of many object-oriented languages. Duck-Typing describes how a language like Ruby, treats its objects as compared to a language like JAVA. We describe the beautiful features of Duck-typing and also its negative side-effects.&amp;lt;br&amp;gt;&lt;br /&gt;
*Modules define a namespace. It provides a mechanism to club several things together which naturally do-not form a common class. With modules, programmer does not have to worry of possible name-clashes of method name's with other methods written by different programmers. Modules handles this by making use of namespace.&amp;lt;br&amp;gt;&lt;br /&gt;
*Mixins extend from modules. modules are not classes and hence we cannot instantiate them. However, when modules are 'include'ed in a class, all its methods are available to that class. We say modules are 'mixed in' to the class or in other words 'mixins'. Modules which are mixed in behave as superclass and provide multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;[http://en.wikipedia.org/wiki/Duck_typing Duck Typing][1]&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;What is Duck Typing&amp;lt;/h2&amp;gt;&lt;br /&gt;
                     '''“If a object walks like a duck, quacks like a duck then treat it as a duck”'''&lt;br /&gt;
&lt;br /&gt;
[[File:Dtype.png|frame|left|[http://www.codecapers.com/post/Duck-Typing.aspx A cat or a duck or a cat walking like a duck?][2]]]&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;This is the ‘mantra’ of Ruby language and the most pleasing aspect of the language. What it means is that, whether an object responds to a method?, is only important and not the type of the object. &lt;br /&gt;
If a dragon responds to methods like swim and quacking, ruby is happy to treat dragon as a duck without having a concern of whether it actually is a duck or not. This gives powerful tools for a ruby programmer. &lt;br /&gt;
&lt;br /&gt;
This is possible in Ruby because we don’t declare the '[http://en.wikipedia.org/wiki/Programming_language#Type_system Type][3] of variables or methods like in JAVA; in Ruby, everything is an object and each object can be individually modified, irrespective of its implementation in the class to which it belongs. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the following example taken from the SAAS lecture:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
my_list.sort&lt;br /&gt;
&lt;br /&gt;
[5, 4, 3].sort&lt;br /&gt;
[&amp;quot;dog&amp;quot;, &amp;quot;cat&amp;quot;, &amp;quot;rat&amp;quot;].sort&lt;br /&gt;
[:a, :b, :c].sort&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here, the same function, sort, is responding to an array of integers, strings and literals. This is classic Ruby duck-typing at work. How sort works is that it compares the elements which are needed to be sorted. So as long as the elements can be compared, sort function works. In the above example, the array of integers, strings and literals implement the comparison operator and hence the same sort function works for all of them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; Why in Ruby and not in JAVA? &amp;lt;/h2&amp;gt;&lt;br /&gt;
The reason for duck-typing to be easily possible and implemented in language like Ruby and not in language like JAVA is that these 2 languages treat ‘type’ differently. &lt;br /&gt;
In JAVA or C++, type means class. When someone asks the type of “abc”, we say it is String. So, such language allows String objects to refer to only other String objects.&lt;br /&gt;
String a; a will never be allowed to refer to anything other than a string, like a=5.&lt;br /&gt;
&lt;br /&gt;
While in Ruby, type refers to how the object reacts to a method or in other words does the object have a particular capability. Type refers to this capability. While a class to which the object belongs provides the initial capabilities, from that point on, the object is free to extend its capabilities independently of its pre-defined class. So,&lt;br /&gt;
&lt;br /&gt;
 a= ”abc”&lt;br /&gt;
 a= 5 are two perfectly acceptable statements in Ruby.&lt;br /&gt;
&lt;br /&gt;
This is duck-typing.&lt;br /&gt;
[http://bigdingus.com/2007/12/08/just-what-is-this-javascript-object-you-handed-me/ A classic example][3] of how a scripting language, JavaScript, not implementing duck-typing can lead to some bad code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; Duck-Typing in action&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class American&lt;br /&gt;
  def football&lt;br /&gt;
    puts &amp;quot;We play it with hands&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class European&lt;br /&gt;
  def football&lt;br /&gt;
    puts &amp;quot;We play it with legs&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Martian&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
footballManiac = [American.new,European.new]&lt;br /&gt;
&lt;br /&gt;
footballManiac.each {|game| game.football}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output of this program is We play it with hands(next line) We play it with legs&lt;br /&gt;
&lt;br /&gt;
This is because of duck-typing. footballManiac first acted as an American class object and since that class responded to the football method, it executed it. Then it acted as an European class object and again that class responded to the method, it executed it. In static-typed languages, we would have to type cast each object and even then we might get compile time or run time error. Nothing of that sorts in Ruby. Now let us also create a Martian class object.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
footballManiac = [American.new,European.new,Martian.new]&lt;br /&gt;
&lt;br /&gt;
footballManiac.each {|game| game.football}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On executing it, we get the following error&lt;br /&gt;
&amp;lt;pre&amp;gt;undefined method `football' for #&amp;lt;Martian:0x3425358&amp;gt; (NoMethodError)&amp;lt;/pre&amp;gt;&lt;br /&gt;
This is obvious because martians do not play football. But the point is, Ruby still checked the martian class and when it found that the object does not respond to the method, it complained.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; The other side to Duck-Typing &amp;lt;/h2&amp;gt;&lt;br /&gt;
As with everything in life, there is always 2 perspectives about its usefulness. Following are some of the criticism against duck-typing :&lt;br /&gt;
&amp;lt;h3&amp;gt;Lack of compiler security&amp;lt;/h3&amp;gt;&lt;br /&gt;
There is no help from compiler for a programmer error like assigning a string to an integer and so on. Only when you run the code, the output might be funny and that’s when you realize a probable mistake in assignment. In a static-type language, there is always a compiler check and hence the problem does not arise. Obviously this problem can be alleviated by proper and exhaustive testing. But as we all know, no one can guarantee 100% code tests.&lt;br /&gt;
&amp;lt;h3&amp;gt;Contract between caller and the callee&amp;lt;/h3&amp;gt;&lt;br /&gt;
In a static type language, the caller knows the exact ‘type’ of parameters required by the callee and also the return-type from the callee. In dynamic type languages, no type is provided. So to understand what the callee expects, one has to look at the code and understand. This is difficult in most of the cases and mainly for third-party code. Again this problem can be solved by providing proper documentation of each API and the pre-requirements for execution. But who can guarantee that the documents correctly map the requirements?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;References&amp;lt;/h1&amp;gt;&lt;br /&gt;
*[1] http://www.youtube.com/watch?v=Q1h-W0sWVoM&lt;br /&gt;
*[2] http://en.wikipedia.org/wiki/Duck_typing&lt;br /&gt;
*[3] http://www.codecapers.com/post/Duck-Typing.aspx&lt;br /&gt;
*[4] http://en.wikipedia.org/wiki/Programming_language#Type_system&lt;br /&gt;
*[5] http://bigdingus.com/2007/12/08/just-what-is-this-javascript-object-you-handed-me/&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Additional Reading&amp;lt;/h1&amp;gt;&lt;br /&gt;
* Sam Ruby, Dave Thomas, David Heinemeier Hansson.agile web development with rails (fourth edition)&lt;br /&gt;
* [http://beust.com/weblog/2005/04/15/the-perils-of-duck-typing/ Perils of Duck Typing]&lt;br /&gt;
* [http://stackoverflow.com/questions/4205130/what-is-duck-typing What is Duck Typing : Stack Overflow]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Type_system What actually do you mean by 'Type']&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_1w59_nm&amp;diff=70341</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 1w59 nm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_1w59_nm&amp;diff=70341"/>
		<updated>2012-11-18T20:51:36Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;SaaS - 3.7. Mixins and Duck Typing&amp;lt;/h1&amp;gt;&lt;br /&gt;
This wiki is aimed at acting as a complementary read to the [http://www.youtube.com/watch?v=Q1h-W0sWVoM SaaS - 3.7. Mixins and Duck Typing lecture]. This lecture describes three of the most important fundamentals of the Ruby language: Duck-Typing, Modules and Mixins. &lt;br /&gt;
&amp;lt;h1&amp;gt;Introduction&amp;lt;/h1&amp;gt;&lt;br /&gt;
*Duck-typing is one of the main features of many object-oriented languages. Duck-Typing describes how a language like Ruby, treats its objects as compared to a language like JAVA. We describe the beautiful features of Duck-typing and also its negative side-effects.&amp;lt;br&amp;gt;&lt;br /&gt;
*Modules define a namespace. It provides a mechanism to club several things together which naturally do-not form a common class. With modules, programmer does not have to worry of possible name-clashes of method name's with other methods written by different programmers. Modules handles this by making use of namespace.&amp;lt;br&amp;gt;&lt;br /&gt;
*Mixins extend from modules. modules are not classes and hence we cannot instantiate them. However, when modules are 'include'ed in a class, all its methods are available to that class. We say modules are 'mixed in' to the class or in other words 'mixins'. Modules which are mixed in behave as superclass and provide multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;[http://en.wikipedia.org/wiki/Duck_typing Duck Typing][1]&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;What is Duck Typing&amp;lt;/h2&amp;gt;&lt;br /&gt;
                     '''“If a object walks like a duck, quacks like a duck then treat it as a duck”'''&lt;br /&gt;
&lt;br /&gt;
[[File:Dtype.png|frame|left|[http://www.codecapers.com/post/Duck-Typing.aspx A cat or a duck or a cat walking like a duck?][2]]]&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;This is the ‘mantra’ of Ruby language and the most pleasing aspect of the language. What it means is that, whether an object responds to a method?, is only important and not the type of the object. &lt;br /&gt;
If a dragon responds to methods like swim and quacking, ruby is happy to treat dragon as a duck without having a concern of whether it actually is a duck or not. This gives powerful tools for a ruby programmer. &lt;br /&gt;
&lt;br /&gt;
This is possible in Ruby because we don’t declare the '[http://en.wikipedia.org/wiki/Programming_language#Type_system Type][3] of variables or methods like in JAVA; in Ruby, everything is an object and each object can be individually modified, irrespective of its implementation in the class to which it belongs. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the following example taken from the SAAS lecture:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
my_list.sort&lt;br /&gt;
&lt;br /&gt;
[5, 4, 3].sort&lt;br /&gt;
[&amp;quot;dog&amp;quot;, &amp;quot;cat&amp;quot;, &amp;quot;rat&amp;quot;].sort&lt;br /&gt;
[:a, :b, :c].sort&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here, the same function, sort, is responding to an array of integers, strings and literals. This is classic Ruby duck-typing at work. How sort works is that it compares the elements which are needed to be sorted. So as long as the elements can be compared, sort function works. In the above example, the array of integers, strings and literals implement the comparison operator and hence the same sort function works for all of them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; Why in Ruby and not in JAVA? &amp;lt;/h2&amp;gt;&lt;br /&gt;
The reason for duck-typing to be easily possible and implemented in language like Ruby and not in language like JAVA is that these 2 languages treat ‘type’ differently. &lt;br /&gt;
In JAVA or C++, type means class. When someone asks the type of “abc”, we say it is String. So, such language allows String objects to refer to only other String objects.&lt;br /&gt;
String a; a will never be allowed to refer to anything other than a string, like a=5.&lt;br /&gt;
&lt;br /&gt;
While in Ruby, type refers to how the object reacts to a method or in other words does the object have a particular capability. Type refers to this capability. While a class to which the object belongs provides the initial capabilities, from that point on, the object is free to extend its capabilities independently of its pre-defined class. So,&lt;br /&gt;
&lt;br /&gt;
 a= ”abc”&lt;br /&gt;
 a= 5 are two perfectly acceptable statements in Ruby.&lt;br /&gt;
&lt;br /&gt;
This is duck-typing.&lt;br /&gt;
[http://bigdingus.com/2007/12/08/just-what-is-this-javascript-object-you-handed-me/ A classic example][3] of how a scripting language, JavaScript, not implementing duck-typing can lead to some bad code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; Duck-Typing in action&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class American&lt;br /&gt;
  def football&lt;br /&gt;
    puts &amp;quot;We play it with hands&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class European&lt;br /&gt;
  def football&lt;br /&gt;
    puts &amp;quot;We play it with legs&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Martian&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
footballManiac = [American.new,European.new]&lt;br /&gt;
&lt;br /&gt;
footballManiac.each {|game| game.football}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output of this program is We play it with hands(next line) We play it with legs&lt;br /&gt;
&lt;br /&gt;
This is because of duck-typing. footballManiac first acted as an American class object and since that class responded to the football method, it executed it. Then it acted as an European class object and again that class responded to the method, it executed it. In static-typed languages, we would have to type cast each object and even then we might get compile time or run time error. Nothing of that sorts in Ruby. Now let us also create a Martian class object.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
footballManiac = [American.new,European.new,Martian.new]&lt;br /&gt;
&lt;br /&gt;
footballManiac.each {|game| game.football}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On executing it, we get the following error&lt;br /&gt;
&amp;lt;pre&amp;gt;undefined method `football' for #&amp;lt;Martian:0x3425358&amp;gt; (NoMethodError)&amp;lt;/pre&amp;gt;&lt;br /&gt;
This is obvious because martians do not play football. But the point is, Ruby still checked the martian class and when it found that the object does not respond to the method, it complained.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; The other side to Duck-Typing &amp;lt;/h2&amp;gt;&lt;br /&gt;
As with everything in life, there is always 2 perspectives about its usefulness. Following are some of the criticism against duck-typing :&lt;br /&gt;
&amp;lt;h3&amp;gt;Lack of compiler security&amp;lt;/h3&amp;gt;&lt;br /&gt;
There is no help from compiler for a programmer error like assigning a string to an integer and so on. Only when you run the code, the output might be funny and that’s when you realize a probable mistake in assignment. In a static-type language, there is always a compiler check and hence the problem does not arise. Obviously this problem can be alleviated by proper and exhaustive testing. But as we all know, no one can guarantee 100% code tests.&lt;br /&gt;
&amp;lt;h3&amp;gt;Contract between caller and the callee&amp;lt;/h3&amp;gt;&lt;br /&gt;
In a static type language, the caller knows the exact ‘type’ of parameters required by the callee and also the return-type from the callee. In dynamic type languages, no type is provided. So to understand what the callee expects, one has to look at the code and understand. This is difficult in most of the cases and mainly for third-party code. Again this problem can be solved by providing proper documentation of each API and the pre-requirements for execution. But who can guarantee that the documents correctly map the requirements?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;References&amp;lt;/h1&amp;gt;&lt;br /&gt;
*[1] http://www.youtube.com/watch?v=Q1h-W0sWVoM&lt;br /&gt;
*[2] http://en.wikipedia.org/wiki/Duck_typing&lt;br /&gt;
*[3] http://www.codecapers.com/post/Duck-Typing.aspx&lt;br /&gt;
*[4] http://en.wikipedia.org/wiki/Programming_language#Type_system&lt;br /&gt;
*[5] http://bigdingus.com/2007/12/08/just-what-is-this-javascript-object-you-handed-me/&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_1w59_nm&amp;diff=70339</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 1w59 nm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_1w59_nm&amp;diff=70339"/>
		<updated>2012-11-18T20:47:49Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;SaaS - 3.7. Mixins and Duck Typing&amp;lt;/h1&amp;gt;&lt;br /&gt;
This wiki is aimed at acting as a complementary read to the [http://www.youtube.com/watch?v=Q1h-W0sWVoM SaaS - 3.7. Mixins and Duck Typing lecture]. This lecture describes three of the most important fundamentals of the Ruby language: Duck-Typing, Modules and Mixins. &lt;br /&gt;
&amp;lt;h1&amp;gt;Introduction&amp;lt;/h1&amp;gt;&lt;br /&gt;
*Duck-typing is one of the main features of many object-oriented languages. Duck-Typing describes how a language like Ruby, treats its objects as compared to a language like JAVA. We describe the beautiful features of Duck-typing and also its negative side-effects.&amp;lt;br&amp;gt;&lt;br /&gt;
*Modules define a namespace. It provides a mechanism to club several things together which naturally do-not form a common class. With modules, programmer does not have to worry of possible name-clashes of method name's with other methods written by different programmers. Modules handles this by making use of namespace.&amp;lt;br&amp;gt;&lt;br /&gt;
*Mixins extend from modules. modules are not classes and hence we cannot instantiate them. However, when modules are 'include'ed in a class, all its methods are available to that class. We say modules are 'mixed in' to the class or in other words 'mixins'. Modules which are mixed in behave as superclass and provide multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;[http://en.wikipedia.org/wiki/Duck_typing Duck Typing][1]&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;What is Duck Typing&amp;lt;/h2&amp;gt;&lt;br /&gt;
                     '''“If a object walks like a duck, quacks like a duck then treat it as a duck”'''&lt;br /&gt;
&lt;br /&gt;
[[File:Dtype.png|frame|left|[http://www.codecapers.com/post/Duck-Typing.aspx A cat or a duck or a cat walking like a duck?][2]]]&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;This is the ‘mantra’ of Ruby language and the most pleasing aspect of the language. What it means is that, whether an object responds to a method?, is only important and not the type of the object. &lt;br /&gt;
If a dragon responds to methods like swim and quacking, ruby is happy to treat dragon as a duck without having a concern of whether it actually is a duck or not. This gives powerful tools for a ruby programmer. &lt;br /&gt;
&lt;br /&gt;
This is possible in Ruby because we don’t declare the '[http://en.wikipedia.org/wiki/Programming_language#Type_system Type][3] of variables or methods like in JAVA; in Ruby, everything is an object and each object can be individually modified, irrespective of its implementation in the class to which it belongs. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the following example taken from the SAAS lecture:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
my_list.sort&lt;br /&gt;
&lt;br /&gt;
[5, 4, 3].sort&lt;br /&gt;
[&amp;quot;dog&amp;quot;, &amp;quot;cat&amp;quot;, &amp;quot;rat&amp;quot;].sort&lt;br /&gt;
[:a, :b, :c].sort&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here, the same function, sort, is responding to an array of integers, strings and literals. This is classic Ruby duck-typing at work. How sort works is that it compares the elements which are needed to be sorted. So as long as the elements can be compared, sort function works. In the above example, the array of integers, strings and literals implement the comparison operator and hence the same sort function works for all of them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; Why in Ruby and not in JAVA? &amp;lt;/h2&amp;gt;&lt;br /&gt;
The reason for duck-typing to be easily possible and implemented in language like Ruby and not in language like JAVA is that these 2 languages treat ‘type’ differently. &lt;br /&gt;
In JAVA or C++, type means class. When someone asks the type of “abc”, we say it is String. So, such language allows String objects to refer to only other String objects.&lt;br /&gt;
String a; a will never be allowed to refer to anything other than a string, like a=5.&lt;br /&gt;
&lt;br /&gt;
While in Ruby, type refers to how the object reacts to a method or in other words does the object have a particular capability. Type refers to this capability. While a class to which the object belongs provides the initial capabilities, from that point on, the object is free to extend its capabilities independently of its pre-defined class. So,&lt;br /&gt;
&lt;br /&gt;
 a= ”abc”&lt;br /&gt;
 a= 5 are two perfectly acceptable statements in Ruby.&lt;br /&gt;
&lt;br /&gt;
This is duck-typing.&lt;br /&gt;
[http://bigdingus.com/2007/12/08/just-what-is-this-javascript-object-you-handed-me/ A classic example][3] of how a scripting language, JavaScript, not implementing duck-typing can lead to some bad code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; Duck-Typing in action&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class American&lt;br /&gt;
  def football&lt;br /&gt;
    puts &amp;quot;We play it with hands&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class European&lt;br /&gt;
  def football&lt;br /&gt;
    puts &amp;quot;We play it with legs&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Martian&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
footballManiac = [American.new,European.new]&lt;br /&gt;
&lt;br /&gt;
footballManiac.each {|game| game.football}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output of this program is We play it with hands(next line) We play it with legs&lt;br /&gt;
&lt;br /&gt;
This is because of duck-typing. footballManiac first acted as an American class object and since that class responded to the football method, it executed it. Then it acted as an European class object and again that class responded to the method, it executed it. In static-typed languages, we would have to type cast each object and even then we might get compile time or run time error. Nothing of that sorts in Ruby. Now let us also create a Martian class object.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
footballManiac = [American.new,European.new,Martian.new]&lt;br /&gt;
&lt;br /&gt;
footballManiac.each {|game| game.football}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On executing it, we get the following error&lt;br /&gt;
&amp;lt;pre&amp;gt;undefined method `football' for #&amp;lt;Martian:0x3425358&amp;gt; (NoMethodError)&amp;lt;/pre&amp;gt;&lt;br /&gt;
This is obvious because martians do not play football. But the point is, Ruby still checked the martian class and when it found that the object does not respond to the method, it complained.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; The other side to Duck-Typing &amp;lt;/h2&amp;gt;&lt;br /&gt;
As with everything in life, there is always 2 perspectives about its usefulness. Following are some of the criticism against duck-typing :&lt;br /&gt;
&amp;lt;h3&amp;gt;Lack of compiler security&amp;lt;/h3&amp;gt;&lt;br /&gt;
There is no help from compiler for a programmer error like assigning a string to an integer and so on. Only when you run the code, the output might be funny and that’s when you realize a probable mistake in assignment. In a static-type language, there is always a compiler check and hence the problem does not arise. Obviously this problem can be alleviated by proper and exhaustive testing. But as we all know, no one can guarantee 100% code tests.&lt;br /&gt;
&amp;lt;h3&amp;gt;Contract between caller and the callee&amp;lt;/h3&amp;gt;&lt;br /&gt;
In a static type language, the caller knows the exact ‘type’ of parameters required by the callee and also the return-type from the callee. In dynamic type languages, no type is provided. So to understand what the callee expects, one has to look at the code and understand. This is difficult in most of the cases and mainly for third-party code. Again this problem can be solved by providing proper documentation of each API and the pre-requirements for execution. But who can guarantee that the documents correctly map the requirements?&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_1w59_nm&amp;diff=70337</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 1w59 nm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_1w59_nm&amp;diff=70337"/>
		<updated>2012-11-18T20:44:54Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;SaaS - 3.7. Mixins and Duck Typing&amp;lt;/h1&amp;gt;&lt;br /&gt;
This wiki is aimed at acting as a complementary read to the SaaS - 3.7. Mixins and Duck Typing lecture. This lecture describes three of the most important fundamentals of the Ruby language: Duck-Typing, Modules and Mixins. &lt;br /&gt;
&amp;lt;h1&amp;gt;Introduction&amp;lt;/h1&amp;gt;&lt;br /&gt;
*Duck-typing is one of the main features of many object-oriented languages. Duck-Typing describes how a language like Ruby, treats its objects as compared to a language like JAVA. We describe the beautiful features of Duck-typing and also its negative side-effects.&amp;lt;br&amp;gt;&lt;br /&gt;
*Modules define a namespace. It provides a mechanism to club several things together which naturally do-not form a common class. With modules, programmer does not have to worry of possible name-clashes of method name's with other methods written by different programmers. Modules handles this by making use of namespace.&amp;lt;br&amp;gt;&lt;br /&gt;
*Mixins extend from modules. modules are not classes and hence we cannot instantiate them. However, when modules are 'include'ed in a class, all its methods are available to that class. We say modules are 'mixed in' to the class or in other words 'mixins'. Modules which are mixed in behave as superclass and provide multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;[http://en.wikipedia.org/wiki/Duck_typing Duck Typing][1]&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;What is Duck Typing&amp;lt;/h2&amp;gt;&lt;br /&gt;
                     '''“If a object walks like a duck, quacks like a duck then treat it as a duck”'''&lt;br /&gt;
&lt;br /&gt;
[[File:Dtype.png|frame|left|[http://www.codecapers.com/post/Duck-Typing.aspx A cat or a duck or a cat walking like a duck?][2]]]&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;This is the ‘mantra’ of Ruby language and the most pleasing aspect of the language. What it means is that, whether an object responds to a method?, is only important and not the type of the object. &lt;br /&gt;
If a dragon responds to methods like swim and quacking, ruby is happy to treat dragon as a duck without having a concern of whether it actually is a duck or not. This gives powerful tools for a ruby programmer. &lt;br /&gt;
&lt;br /&gt;
This is possible in Ruby because we don’t declare the '[http://en.wikipedia.org/wiki/Programming_language#Type_system Type][3] of variables or methods like in JAVA; in Ruby, everything is an object and each object can be individually modified, irrespective of its implementation in the class to which it belongs. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the following example taken from the SAAS lecture:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
my_list.sort&lt;br /&gt;
&lt;br /&gt;
[5, 4, 3].sort&lt;br /&gt;
[&amp;quot;dog&amp;quot;, &amp;quot;cat&amp;quot;, &amp;quot;rat&amp;quot;].sort&lt;br /&gt;
[:a, :b, :c].sort&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here, the same function, sort, is responding to an array of integers, strings and literals. This is classic Ruby duck-typing at work. How sort works is that it compares the elements which are needed to be sorted. So as long as the elements can be compared, sort function works. In the above example, the array of integers, strings and literals implement the comparison operator and hence the same sort function works for all of them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; Why in Ruby and not in JAVA? &amp;lt;/h2&amp;gt;&lt;br /&gt;
The reason for duck-typing to be easily possible and implemented in language like Ruby and not in language like JAVA is that these 2 languages treat ‘type’ differently. &lt;br /&gt;
In JAVA or C++, type means class. When someone asks the type of “abc”, we say it is String. So, such language allows String objects to refer to only other String objects.&lt;br /&gt;
String a; a will never be allowed to refer to anything other than a string, like a=5.&lt;br /&gt;
&lt;br /&gt;
While in Ruby, type refers to how the object reacts to a method or in other words does the object have a particular capability. Type refers to this capability. While a class to which the object belongs provides the initial capabilities, from that point on, the object is free to extend its capabilities independently of its pre-defined class. So,&lt;br /&gt;
&lt;br /&gt;
 a= ”abc”&lt;br /&gt;
 a= 5 are two perfectly acceptable statements in Ruby.&lt;br /&gt;
&lt;br /&gt;
This is duck-typing.&lt;br /&gt;
[http://bigdingus.com/2007/12/08/just-what-is-this-javascript-object-you-handed-me/ A classic example][3] of how a scripting language, JavaScript, not implementing duck-typing can lead to some bad code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; Duck-Typing in action&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class American&lt;br /&gt;
  def football&lt;br /&gt;
    puts &amp;quot;We play it with hands&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class European&lt;br /&gt;
  def football&lt;br /&gt;
    puts &amp;quot;We play it with legs&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Martian&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
footballManiac = [American.new,European.new]&lt;br /&gt;
&lt;br /&gt;
footballManiac.each {|game| game.football}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output of this program is We play it with hands(next line) We play it with legs&lt;br /&gt;
&lt;br /&gt;
This is because of duck-typing. footballManiac first acted as an American class object and since that class responded to the football method, it executed it. Then it acted as an European class object and again that class responded to the method, it executed it. In static-typed languages, we would have to type cast each object and even then we might get compile time or run time error. Nothing of that sorts in Ruby. Now let us also create a Martian class object.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
footballManiac = [American.new,European.new,Martian.new]&lt;br /&gt;
&lt;br /&gt;
footballManiac.each {|game| game.football}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On executing it, we get the following error&lt;br /&gt;
&amp;lt;pre&amp;gt;undefined method `football' for #&amp;lt;Martian:0x3425358&amp;gt; (NoMethodError)&amp;lt;/pre&amp;gt;&lt;br /&gt;
This is obvious because martians do not play football. But the point is, Ruby still checked the martian class and when it found that the object does not respond to the method, it complained.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; The other side to Duck-Typing &amp;lt;/h2&amp;gt;&lt;br /&gt;
As with everything in life, there is always 2 perspectives about its usefulness. Following are some of the criticism against duck-typing :&lt;br /&gt;
&amp;lt;h3&amp;gt;Lack of compiler security&amp;lt;/h3&amp;gt;&lt;br /&gt;
There is no help from compiler for a programmer error like assigning a string to an integer and so on. Only when you run the code, the output might be funny and that’s when you realize a probable mistake in assignment. In a static-type language, there is always a compiler check and hence the problem does not arise. Obviously this problem can be alleviated by proper and exhaustive testing. But as we all know, no one can guarantee 100% code tests.&lt;br /&gt;
&amp;lt;h3&amp;gt;Contract between caller and the callee&amp;lt;/h3&amp;gt;&lt;br /&gt;
In a static type language, the caller knows the exact ‘type’ of parameters required by the callee and also the return-type from the callee. In dynamic type languages, no type is provided. So to understand what the callee expects, one has to look at the code and understand. This is difficult in most of the cases and mainly for third-party code. Again this problem can be solved by providing proper documentation of each API and the pre-requirements for execution. But who can guarantee that the documents correctly map the requirements?&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_1w59_nm&amp;diff=70335</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 1w59 nm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_1w59_nm&amp;diff=70335"/>
		<updated>2012-11-18T20:43:35Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;SaaS - 3.7. Mixins and Duck Typing&amp;lt;/h1&amp;gt;&lt;br /&gt;
This wiki is aimed at acting as a complementary read to the SaaS - 3.7. Mixins and Duck Typing lecture. This lecture describes three of the most important fundamentals of the Ruby language: Duck-Typing, Modules and Mixins. &lt;br /&gt;
&amp;lt;h1&amp;gt;Introduction&amp;lt;/h1&amp;gt;&lt;br /&gt;
*Duck-typing is one of the main features of many object-oriented languages. Duck-Typing describes how a language like Ruby, treats its objects as compared to a language like JAVA. We describe the beautiful features of Duck-typing and also its negative side-effects.&amp;lt;br&amp;gt;&lt;br /&gt;
*Modules define a namespace. It provides a mechanism to club several things together which naturally do-not form a common class. With modules, programmer does not have to worry of possible name-clashes of method name's with other methods written by different programmers. Modules handles this by making use of namespace.&amp;lt;br&amp;gt;&lt;br /&gt;
*Mixins extend from modules. modules are not classes and hence we cannot instantiate them. However, when modules are 'include'ed in a class, all its methods are available to that class. We say modules are 'mixed in' to the class or in other words 'mixins'. Modules which are mixed in behave as superclass and provide multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;[http://en.wikipedia.org/wiki/Duck_typing Duck Typing][1]&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;What is Duck Typing&amp;lt;/h2&amp;gt;&lt;br /&gt;
                     '''“If a object walks like a duck, quacks like a duck then treat it as a duck”'''&lt;br /&gt;
&lt;br /&gt;
[[File:Dtype.png|frame|left|[http://www.codecapers.com/post/Duck-Typing.aspx A cat or a duck?][2]]]&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;This is the ‘mantra’ of Ruby language and the most pleasing aspect of the language. What it means is that, whether an object responds to a method?, is only important and not the type of the object. &lt;br /&gt;
If a dragon responds to methods like swim and quacking, ruby is happy to treat dragon as a duck without having a concern of whether it actually is a duck or not. This gives powerful tools for a ruby programmer. &lt;br /&gt;
&lt;br /&gt;
This is possible in Ruby because we don’t declare the '[http://en.wikipedia.org/wiki/Programming_language#Type_system Type][3] of variables or methods like in JAVA; in Ruby, everything is an object and each object can be individually modified, irrespective of its implementation in the class to which it belongs. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the following example taken from the SAAS lecture:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
my_list.sort&lt;br /&gt;
&lt;br /&gt;
[5, 4, 3].sort&lt;br /&gt;
[&amp;quot;dog&amp;quot;, &amp;quot;cat&amp;quot;, &amp;quot;rat&amp;quot;].sort&lt;br /&gt;
[:a, :b, :c].sort&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here, the same function, sort, is responding to an array of integers, strings and literals. This is classic Ruby duck-typing at work. How sort works is that it compares the elements which are needed to be sorted. So as long as the elements can be compared, sort function works. In the above example, the array of integers, strings and literals implement the comparison operator and hence the same sort function works for all of them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; Why in Ruby and not in JAVA? &amp;lt;/h2&amp;gt;&lt;br /&gt;
The reason for duck-typing to be easily possible and implemented in language like Ruby and not in language like JAVA is that these 2 languages treat ‘type’ differently. &lt;br /&gt;
In JAVA or C++, type means class. When someone asks the type of “abc”, we say it is String. So, such language allows String objects to refer to only other String objects.&lt;br /&gt;
String a; a will never be allowed to refer to anything other than a string, like a=5.&lt;br /&gt;
&lt;br /&gt;
While in Ruby, type refers to how the object reacts to a method or in other words does the object have a particular capability. Type refers to this capability. While a class to which the object belongs provides the initial capabilities, from that point on, the object is free to extend its capabilities independently of its pre-defined class. So,&lt;br /&gt;
&lt;br /&gt;
 a= ”abc”&lt;br /&gt;
 a= 5 are two perfectly acceptable statements in Ruby.&lt;br /&gt;
&lt;br /&gt;
This is duck-typing.&lt;br /&gt;
[http://bigdingus.com/2007/12/08/just-what-is-this-javascript-object-you-handed-me/ A classic example][3] of how a scripting language, JavaScript, not implementing duck-typing can lead to some bad code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; Duck-Typing in action&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class American&lt;br /&gt;
  def football&lt;br /&gt;
    puts &amp;quot;We play it with hands&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class European&lt;br /&gt;
  def football&lt;br /&gt;
    puts &amp;quot;We play it with legs&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Martian&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
footballManiac = [American.new,European.new]&lt;br /&gt;
&lt;br /&gt;
footballManiac.each {|game| game.football}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output of this program is We play it with hands(next line) We play it with legs&lt;br /&gt;
&lt;br /&gt;
This is because of duck-typing. footballManiac first acted as an American class object and since that class responded to the football method, it executed it. Then it acted as an European class object and again that class responded to the method, it executed it. In static-typed languages, we would have to type cast each object and even then we might get compile time or run time error. Nothing of that sorts in Ruby. Now let us also create a Martian class object.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
footballManiac = [American.new,European.new,Martian.new]&lt;br /&gt;
&lt;br /&gt;
footballManiac.each {|game| game.football}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On executing it, we get the following error&lt;br /&gt;
&amp;lt;pre&amp;gt;undefined method `football' for #&amp;lt;Martian:0x3425358&amp;gt; (NoMethodError)&amp;lt;/pre&amp;gt;&lt;br /&gt;
This is obvious because martians do not play football. But the point is, Ruby still checked the martian class and when it found that the object does not respond to the method, it complained.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; The other side to Duck-Typing &amp;lt;/h2&amp;gt;&lt;br /&gt;
As with everything in life, there is always 2 perspectives about its usefulness. Following are some of the criticism against duck-typing :&lt;br /&gt;
&amp;lt;h3&amp;gt;Lack of compiler security&amp;lt;/h3&amp;gt;&lt;br /&gt;
There is no help from compiler for a programmer error like assigning a string to an integer and so on. Only when you run the code, the output might be funny and that’s when you realize a probable mistake in assignment. In a static-type language, there is always a compiler check and hence the problem does not arise. Obviously this problem can be alleviated by proper and exhaustive testing. But as we all know, no one can guarantee 100% code tests.&lt;br /&gt;
&amp;lt;h3&amp;gt;Contract between caller and the callee&amp;lt;/h3&amp;gt;&lt;br /&gt;
In a static type language, the caller knows the exact ‘type’ of parameters required by the callee and also the return-type from the callee. In dynamic type languages, no type is provided. So to understand what the callee expects, one has to look at the code and understand. This is difficult in most of the cases and mainly for third-party code. Again this problem can be solved by providing proper documentation of each API and the pre-requirements for execution. But who can guarantee that the documents correctly map the requirements?&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_1w59_nm&amp;diff=70332</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 1w59 nm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_1w59_nm&amp;diff=70332"/>
		<updated>2012-11-18T20:34:55Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;SaaS - 3.7. Mixins and Duck Typing&amp;lt;/h1&amp;gt;&lt;br /&gt;
This wiki is aimed at acting as a complementary read to the SaaS - 3.7. Mixins and Duck Typing lecture. This lecture describes three of the most important fundamentals of the Ruby language: Duck-Typing, Modules and Mixins. &lt;br /&gt;
&amp;lt;h1&amp;gt;Introduction&amp;lt;/h1&amp;gt;&lt;br /&gt;
*Duck-typing is one of the main features of many object-oriented languages. Duck-Typing describes how a language like Ruby, treats its objects as compared to a language like JAVA. We describe the beautiful features of Duck-typing and also its negative side-effects.&amp;lt;br&amp;gt;&lt;br /&gt;
*Modules define a namespace. It provides a mechanism to club several things together which naturally do-not form a common class. With modules, programmer does not have to worry of possible name-clashes of method name's with other methods written by different programmers. Modules handles this by making use of namespace.&amp;lt;br&amp;gt;&lt;br /&gt;
*Mixins extend from modules. modules are not classes and hence we cannot instantiate them. However, when modules are 'include'ed in a class, all its methods are available to that class. We say modules are 'mixed in' to the class or in other words 'mixins'. Modules which are mixed in behave as superclass and provide multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;[http://en.wikipedia.org/wiki/Duck_typing Duck Typing][1]&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;What is Duck Typing&amp;lt;/h2&amp;gt;&lt;br /&gt;
                     '''“If a object walks like a duck, quacks like a duck then treat it as a duck”'''&lt;br /&gt;
[http://www.codecapers.com/post/Duck-Typing.aspx[[File:Dtype.png]]][2]&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;This is the ‘mantra’ of Ruby language and the most pleasing aspect of the language. What it means is that, whether an object responds to a method?, is only important and not the type of the object. &lt;br /&gt;
If a dragon responds to methods like swim and quacking, ruby is happy to treat dragon as a duck without having a concern of whether it actually is a duck or not. This gives powerful tools for a ruby programmer. &lt;br /&gt;
&lt;br /&gt;
This is possible in Ruby because we don’t declare the '[http://en.wikipedia.org/wiki/Programming_language#Type_system Type][3] of variables or methods like in JAVA; in Ruby, everything is an object and each object can be individually modified, irrespective of its implementation in the class to which it belongs. &lt;br /&gt;
&lt;br /&gt;
Consider the following example taken from the SAAS lecture:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
my_list.sort&lt;br /&gt;
&lt;br /&gt;
[5, 4, 3].sort&lt;br /&gt;
[&amp;quot;dog&amp;quot;, &amp;quot;cat&amp;quot;, &amp;quot;rat&amp;quot;].sort&lt;br /&gt;
[:a, :b, :c].sort&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here, the same function, sort, is responding to an array of integers, strings and literals. This is classic Ruby duck-typing at work. How sort works is that it compares the elements which are needed to be sorted. So as long as the elements can be compared, sort function works. In the above example, the array of integers, strings and literals implement the comparison operator and hence the same sort function works for all of them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; Why in Ruby and not in JAVA? &amp;lt;/h2&amp;gt;&lt;br /&gt;
The reason for duck-typing to be easily possible and implemented in language like Ruby and not in language like JAVA is that these 2 languages treat ‘type’ differently. &lt;br /&gt;
In JAVA or C++, type means class. When someone asks the type of “abc”, we say it is String. So, such language allows String objects to refer to only other String objects.&lt;br /&gt;
String a; a will never be allowed to refer to anything other than a string, like a=5.&lt;br /&gt;
&lt;br /&gt;
While in Ruby, type refers to how the object reacts to a method or in other words does the object have a particular capability. Type refers to this capability. While a class to which the object belongs provides the initial capabilities, from that point on, the object is free to extend its capabilities independently of its pre-defined class. So,&lt;br /&gt;
&lt;br /&gt;
 a= ”abc”&lt;br /&gt;
 a= 5 are two perfectly acceptable statements in Ruby.&lt;br /&gt;
&lt;br /&gt;
This is duck-typing.&lt;br /&gt;
[http://bigdingus.com/2007/12/08/just-what-is-this-javascript-object-you-handed-me/ A classic example][3] of how a scripting language, JavaScript, not implementing duck-typing can lead to some bad code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; Duck-Typing in action&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class American&lt;br /&gt;
  def football&lt;br /&gt;
    puts &amp;quot;We play it with hands&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class European&lt;br /&gt;
  def football&lt;br /&gt;
    puts &amp;quot;We play it with legs&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Martian&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
footballManiac = [American.new,European.new]&lt;br /&gt;
&lt;br /&gt;
footballManiac.each {|game| game.football}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output of this program is We play it with hands(next line) We play it with legs&lt;br /&gt;
&lt;br /&gt;
This is because of duck-typing. footballManiac first acted as an American class object and since that class responded to the football method, it executed it. Then it acted as an European class object and again that class responded to the method, it executed it. In static-typed languages, we would have to type cast each object and even then we might get compile time or run time error. Nothing of that sorts in Ruby. Now let us also create a Martian class object.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
footballManiac = [American.new,European.new,Martian.new]&lt;br /&gt;
&lt;br /&gt;
footballManiac.each {|game| game.football}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On executing it, we get the following error&lt;br /&gt;
&amp;lt;pre&amp;gt;undefined method `football' for #&amp;lt;Martian:0x3425358&amp;gt; (NoMethodError)&amp;lt;/pre&amp;gt;&lt;br /&gt;
This is obvious because martians do not play football. But the point is, Ruby still checked the martian class and when it found that the object does not respond to the method, it complained.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; The other side to Duck-Typing &amp;lt;/h2&amp;gt;&lt;br /&gt;
As with everything in life, there is always 2 perspectives about its usefulness. Following are some of the criticism against duck-typing :&lt;br /&gt;
&amp;lt;h3&amp;gt;Lack of compiler security&amp;lt;/h3&amp;gt;&lt;br /&gt;
There is no help from compiler for a programmer error like assigning a string to an integer and so on. Only when you run the code, the output might be funny and that’s when you realize a probable mistake in assignment. In a static-type language, there is always a compiler check and hence the problem does not arise. Obviously this problem can be alleviated by proper and exhaustive testing. But as we all know, no one can guarantee 100% code tests.&lt;br /&gt;
&amp;lt;h3&amp;gt;Contract between caller and the callee&amp;lt;/h3&amp;gt;&lt;br /&gt;
In a static type language, the caller knows the exact ‘type’ of parameters required by the callee and also the return-type from the callee. In dynamic type languages, no type is provided. So to understand what the callee expects, one has to look at the code and understand. This is difficult in most of the cases and mainly for third-party code. Again this problem can be solved by providing proper documentation of each API and the pre-requirements for execution. But who can guarantee that the documents correctly map the requirements?&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Dtype.png&amp;diff=70330</id>
		<title>File:Dtype.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Dtype.png&amp;diff=70330"/>
		<updated>2012-11-18T20:32:03Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: Duck type in ruby&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Duck type in ruby&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_1w59_nm&amp;diff=70329</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 1w59 nm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_1w59_nm&amp;diff=70329"/>
		<updated>2012-11-18T20:25:17Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;SaaS - 3.7. Mixins and Duck Typing&amp;lt;/h1&amp;gt;&lt;br /&gt;
This wiki is aimed at acting as a complementary read to the SaaS - 3.7. Mixins and Duck Typing lecture. This lecture describes three of the most important fundamentals of the Ruby language: Duck-Typing, Modules and Mixins. &lt;br /&gt;
&amp;lt;h1&amp;gt;Introduction&amp;lt;/h1&amp;gt;&lt;br /&gt;
*Duck-typing is one of the main features of many object-oriented languages. Duck-Typing describes how a language like Ruby, treats its objects as compared to a language like JAVA. We describe the beautiful features of Duck-typing and also its negative side-effects.&amp;lt;br&amp;gt;&lt;br /&gt;
*Modules define a namespace. It provides a mechanism to club several things together which naturally do-not form a common class. With modules, programmer does not have to worry of possible name-clashes of method name's with other methods written by different programmers. Modules handles this by making use of namespace.&amp;lt;br&amp;gt;&lt;br /&gt;
*Mixins extend from modules. modules are not classes and hence we cannot instantiate them. However, when modules are 'include'ed in a class, all its methods are available to that class. We say modules are 'mixed in' to the class or in other words 'mixins'. Modules which are mixed in behave as superclass and provide multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;[http://en.wikipedia.org/wiki/Duck_typing Duck Typing][1]&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;What is Duck Typing&amp;lt;/h2&amp;gt;[http://threebrothers.org/brendan/blog/death-by-duck-typing/[[File:Duckt.gif]]][2]&lt;br /&gt;
                     '''“If a object walks like a duck, quacks like a duck then treat it as a duck”'''&lt;br /&gt;
This is the ‘mantra’ of Ruby language and the most pleasing aspect of the language. What it means is that, whether an object responds to a method?, is only important and not the type of the object. &lt;br /&gt;
If a dragon responds to methods like swim and quacking, ruby is happy to treat dragon as a duck without having a concern of whether it actually is a duck or not. This gives powerful tools for a ruby programmer. &lt;br /&gt;
&lt;br /&gt;
This is possible in Ruby because we don’t declare the '[http://en.wikipedia.org/wiki/Programming_language#Type_system Type][3] of variables or methods like in JAVA; in Ruby, everything is an object and each object can be individually modified, irrespective of its implementation in the class to which it belongs. &lt;br /&gt;
&lt;br /&gt;
Consider the following example taken from the SAAS lecture:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
my_list.sort&lt;br /&gt;
&lt;br /&gt;
[5, 4, 3].sort&lt;br /&gt;
[&amp;quot;dog&amp;quot;, &amp;quot;cat&amp;quot;, &amp;quot;rat&amp;quot;].sort&lt;br /&gt;
[:a, :b, :c].sort&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here, the same function, sort, is responding to an array of integers, strings and literals. This is classic Ruby duck-typing at work. How sort works is that it compares the elements which are needed to be sorted. So as long as the elements can be compared, sort function works. In the above example, the array of integers, strings and literals implement the comparison operator and hence the same sort function works for all of them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; Why in Ruby and not in JAVA? &amp;lt;/h2&amp;gt;&lt;br /&gt;
The reason for duck-typing to be easily possible and implemented in language like Ruby and not in language like JAVA is that these 2 languages treat ‘type’ differently. &lt;br /&gt;
In JAVA or C++, type means class. When someone asks the type of “abc”, we say it is String. So, such language allows String objects to refer to only other String objects.&lt;br /&gt;
String a; a will never be allowed to refer to anything other than a string, like a=5.&lt;br /&gt;
&lt;br /&gt;
While in Ruby, type refers to how the object reacts to a method or in other words does the object have a particular capability. Type refers to this capability. While a class to which the object belongs provides the initial capabilities, from that point on, the object is free to extend its capabilities independently of its pre-defined class. So,&lt;br /&gt;
&lt;br /&gt;
 a= ”abc”&lt;br /&gt;
 a= 5 are two perfectly acceptable statements in Ruby.&lt;br /&gt;
&lt;br /&gt;
This is duck-typing.&lt;br /&gt;
[http://bigdingus.com/2007/12/08/just-what-is-this-javascript-object-you-handed-me/ A classic example][3] of how a scripting language, JavaScript, not implementing duck-typing can lead to some bad code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; Duck-Typing in action&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class American&lt;br /&gt;
  def football&lt;br /&gt;
    puts &amp;quot;We play it with hands&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class European&lt;br /&gt;
  def football&lt;br /&gt;
    puts &amp;quot;We play it with legs&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Martian&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
footballManiac = [American.new,European.new]&lt;br /&gt;
&lt;br /&gt;
footballManiac.each {|game| game.football}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output of this program is We play it with hands(next line) We play it with legs&lt;br /&gt;
&lt;br /&gt;
This is because of duck-typing. footballManiac first acted as an American class object and since that class responded to the football method, it executed it. Then it acted as an European class object and again that class responded to the method, it executed it. In static-typed languages, we would have to type cast each object and even then we might get compile time or run time error. Nothing of that sorts in Ruby. Now let us also create a Martian class object.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
footballManiac = [American.new,European.new,Martian.new]&lt;br /&gt;
&lt;br /&gt;
footballManiac.each {|game| game.football}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On executing it, we get the following error&lt;br /&gt;
&amp;lt;pre&amp;gt;undefined method `football' for #&amp;lt;Martian:0x3425358&amp;gt; (NoMethodError)&amp;lt;/pre&amp;gt;&lt;br /&gt;
This is obvious because martians do not play football. But the point is, Ruby still checked the martian class and when it found that the object does not respond to the method, it complained.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; The other side to Duck-Typing &amp;lt;/h2&amp;gt;&lt;br /&gt;
As with everything in life, there is always 2 perspectives about its usefulness. Following are some of the criticism against duck-typing :&lt;br /&gt;
&amp;lt;h3&amp;gt;Lack of compiler security&amp;lt;/h3&amp;gt;&lt;br /&gt;
There is no help from compiler for a programmer error like assigning a string to an integer and so on. Only when you run the code, the output might be funny and that’s when you realize a probable mistake in assignment. In a static-type language, there is always a compiler check and hence the problem does not arise. Obviously this problem can be alleviated by proper and exhaustive testing. But as we all know, no one can guarantee 100% code tests.&lt;br /&gt;
&amp;lt;h3&amp;gt;Contract between caller and the callee&amp;lt;/h3&amp;gt;&lt;br /&gt;
In a static type language, the caller knows the exact ‘type’ of parameters required by the callee and also the return-type from the callee. In dynamic type languages, no type is provided. So to understand what the callee expects, one has to look at the code and understand. This is difficult in most of the cases and mainly for third-party code. Again this problem can be solved by providing proper documentation of each API and the pre-requirements for execution. But who can guarantee that the documents correctly map the requirements?&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_1w59_nm&amp;diff=70254</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 1w59 nm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_1w59_nm&amp;diff=70254"/>
		<updated>2012-11-18T18:26:30Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;SaaS - 3.7. Mixins and Duck Typing&amp;lt;/h1&amp;gt;&lt;br /&gt;
This wiki is aimed at acting as a complementary read to the SaaS - 3.7. Mixins and Duck Typing lecture. This lecture describes three of the most important fundamentals of the Ruby language: Duck-Typing, Modules and Mixins. &lt;br /&gt;
&amp;lt;h1&amp;gt;Introduction&amp;lt;/h1&amp;gt;&lt;br /&gt;
*Duck-typing is one of the main features of many object-oriented languages. Duck-Typing describes how a language like Ruby, treats its objects as compared to a language like JAVA. We describe the beautiful features of Duck-typing and also its negative side-effects.&amp;lt;br&amp;gt;&lt;br /&gt;
*Modules define a namespace. It provides a mechanism to club several things together which naturally do-not form a common class. With modules, programmer does not have to worry of possible name-clashes of method name's with other methods written by different programmers. Modules handles this by making use of namespace.&amp;lt;br&amp;gt;&lt;br /&gt;
*Mixins extend from modules. modules are not classes and hence we cannot instantiate them. However, when modules are 'include'ed in a class, all its methods are available to that class. We say modules are 'mixed in' to the class or in other words 'mixins'. Modules which are mixed in behave as superclass and provide multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;[http://en.wikipedia.org/wiki/Duck_typing Duck Typing][1]&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;What is Duck Typing&amp;lt;/h2&amp;gt;[http://threebrothers.org/brendan/blog/death-by-duck-typing/[[File:Duckt.gif]]][2]&lt;br /&gt;
                     '''“If a object walks like a duck, quacks like a duck then treat it as a duck”'''&lt;br /&gt;
This is the ‘mantra’ of Ruby language and the most pleasing aspect of the language. What it means is that, whether an object responds to a method?, is only important and not the type of the object. &lt;br /&gt;
If a dragon responds to methods like swim and quacking, ruby is happy to treat dragon as a duck without having a concern of whether it actually is a duck or not. This gives powerful tools for a ruby programmer. &lt;br /&gt;
&lt;br /&gt;
This is possible in Ruby because we don’t declare the '[http://en.wikipedia.org/wiki/Programming_language#Type_system Type][3] of variables or methods like in JAVA; in Ruby, everything is an object and each object can be individually modified, irrespective of its implementation in the class to which it belongs. &lt;br /&gt;
&lt;br /&gt;
Consider the following example taken from the SAAS lecture:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
my_list.sort&lt;br /&gt;
&lt;br /&gt;
[5, 4, 3].sort&lt;br /&gt;
[&amp;quot;dog&amp;quot;, &amp;quot;cat&amp;quot;, &amp;quot;rat&amp;quot;].sort&lt;br /&gt;
[:a, :b, :c].sort&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here, the same function, sort, is responding to an array of integers, strings and literals. This is classic Ruby duck-typing at work. How sort works is that it compares the elements which are needed to be sorted. So as long as the elements can be compared, sort function works. In the above example, the array of integers, strings and literals implement the comparison operator and hence the same sort function works for all of them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; Why in Ruby and not in JAVA? &amp;lt;/h2&amp;gt;&lt;br /&gt;
The reason for duck-typing to be easily possible and implemented in language like Ruby and not in language like JAVA is that these 2 languages treat ‘type’ differently. &lt;br /&gt;
In JAVA or C++, type means class. When someone asks the type of “abc”, we say it is String. So, such language allows String objects to refer to only other String objects.&lt;br /&gt;
String a; a will never be allowed to refer to anything other than a string, like a=5.&lt;br /&gt;
&lt;br /&gt;
While in Ruby, type refers to how the object reacts to a method or in other words does the object have a particular capability. Type refers to this capability. While a class to which the object belongs provides the initial capabilities, from that point on, the object is free to extend its capabilities independently of its pre-defined class. So,&lt;br /&gt;
&lt;br /&gt;
 a= ”abc”&lt;br /&gt;
 a= 5 are two perfectly acceptable statements in Ruby.&lt;br /&gt;
&lt;br /&gt;
This is duck-typing.&lt;br /&gt;
[http://bigdingus.com/2007/12/08/just-what-is-this-javascript-object-you-handed-me/ A classic example][3] of how a scripting language, JavaScript, not implementing duck-typing can lead to some bad code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; Duck-Typing in action&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class American&lt;br /&gt;
  def football&lt;br /&gt;
    puts &amp;quot;We play it with hands&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class European&lt;br /&gt;
  def football&lt;br /&gt;
    puts &amp;quot;We play it with legs&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Martian&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
footballManiac = [American.new,European.new]&lt;br /&gt;
&lt;br /&gt;
footballManiac.each {|game| game.football}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output of this program is We play it with hands(next line) We play it with legs&lt;br /&gt;
&lt;br /&gt;
This is because of duck-typing. footballManiac first acted as an American class object and since that class responded to the football method, it executed it. Then it acted as an European class object and again that class responded to the method, it executed it. In static-typed languages, we would have to type cast each object and even then we might get compile time or run time eeror. Nothing of that sorts in Ruby. Now let us also create a Martian class object.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
footballManiac = [American.new,European.new,Martian.new]&lt;br /&gt;
&lt;br /&gt;
footballManiac.each {|game| game.football}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On executing it, we get the following error&lt;br /&gt;
&amp;lt;pre&amp;gt;undefined method `football' for #&amp;lt;Martian:0x3425358&amp;gt; (NoMethodError)&amp;lt;/pre&amp;gt;&lt;br /&gt;
This is obvious because martians do not play football. But the point is, Ruby still checked the martian class and when it found that the object does not respond to the method, it complained.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; The other side to Duck-Typing &amp;lt;/h2&amp;gt;&lt;br /&gt;
As with everything in life, there is always 2 perspectives about its usefulness. Following are some of the criticism against duck-typing :&lt;br /&gt;
&amp;lt;h3&amp;gt;Lack of compiler security&amp;lt;/h3&amp;gt;&lt;br /&gt;
There is no help from compiler for a programmer error like assigning a string to an integer and so on. Only when you run the code, the output might be funny and that’s when you realize a probable mistake in assignment. In a static-type language, there is always a compiler check and hence the problem does not arise. Obviously this problem can be alleviated by proper and exhaustive testing. But as we all know, no one can guarantee 100% code tests.&lt;br /&gt;
&amp;lt;h3&amp;gt;Contract between caller and the callee&amp;lt;/h3&amp;gt;&lt;br /&gt;
In a static type language, the caller knows the exact ‘type’ of parameters required by the callee and also the return-type from the callee. In dynamic type languages, no type is provided. So to understand what the callee expects, one has to look at the code and understand. This is difficult in most of the cases and mainly for third-party code. Again this problem can be solved by providing proper documentation of each API and the pre-requirements for execution. But who can guarantee that the documents correctly map the requirements?&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_1w59_nm&amp;diff=70233</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 1w59 nm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_1w59_nm&amp;diff=70233"/>
		<updated>2012-11-18T17:59:28Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;SaaS - 3.7. Mixins and Duck Typing&amp;lt;/h1&amp;gt;&lt;br /&gt;
This wiki is aimed at acting as a complementary read to the SaaS - 3.7. Mixins and Duck Typing lecture. This lecture describes three of the most important fundamentals of the Ruby language: Duck-Typing, Modules and Mixins. &lt;br /&gt;
&amp;lt;h1&amp;gt;Introduction&amp;lt;/h1&amp;gt;&lt;br /&gt;
*Duck-typing is one of the main features of many object-oriented languages. Duck-Typing describes how a language like Ruby, treats its objects as compared to a language like JAVA. We describe the beautiful features of Duck-typing and also its negative side-effects.&amp;lt;br&amp;gt;&lt;br /&gt;
*Modules define a namespace. It provides a mechanism to club several things together which naturally do-not form a common class. With modules, programmer does not have to worry of possible name-clashes of method name's with other methods written by different programmers. Modules handles this by making use of namespace.&amp;lt;br&amp;gt;&lt;br /&gt;
*Mixins extend from modules. modules are not classes and hence we cannot instantiate them. However, when modules are 'include'ed in a class, all its methods are available to that class. We say modules are 'mixed in' to the class or in other words 'mixins'. Modules which are mixed in behave as superclass and provide multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;[http://en.wikipedia.org/wiki/Duck_typing Duck Typing][1]&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;What is Duck Typing&amp;lt;/h2&amp;gt;[http://threebrothers.org/brendan/blog/death-by-duck-typing/[[File:Duckt.gif]]][2]&lt;br /&gt;
                     '''“If a object walks like a duck, quacks like a duck then treat it as a duck”'''&lt;br /&gt;
This is the ‘mantra’ of Ruby language and the most pleasing aspect of the language. What it means is that, whether an object responds to a method?, is only important and not the type of the object. &lt;br /&gt;
If a dragon responds to methods like swim and quacking, ruby is happy to treat dragon as a duck without having a concern of whether it actually is a duck or not. This gives powerful tools for a ruby programmer. &lt;br /&gt;
&lt;br /&gt;
This is possible in Ruby because we don’t declare the '[http://en.wikipedia.org/wiki/Programming_language#Type_system Type][3] of variables or methods like in JAVA; in Ruby, everything is an object and each object can be individually modified, irrespective of its implementation in the class to which it belongs. &lt;br /&gt;
&lt;br /&gt;
Consider the following example taken from the SAAS lecture:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
my_list.sort&lt;br /&gt;
&lt;br /&gt;
[5, 4, 3].sort&lt;br /&gt;
[&amp;quot;dog&amp;quot;, &amp;quot;cat&amp;quot;, &amp;quot;rat&amp;quot;].sort&lt;br /&gt;
[:a, :b, :c].sort&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here, the same function, sort, is responding to an array of integers, strings and literals. This is classic Ruby duck-typing at work. How sort works is that it compares the elements which are needed to be sorted. So as long as the elements can be compared, sort function works. In the above example, the array of integers, strings and literals implement the comparison operator and hence the same sort function works for all of them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; Why in Ruby and not in JAVA? &amp;lt;/h2&amp;gt;&lt;br /&gt;
The reason for duck-typing to be easily possible and implemented in language like Ruby and not in language like JAVA is that these 2 languages treat ‘type’ differently. &lt;br /&gt;
In JAVA or C++, type means class. When someone asks the type of “abc”, we say it is String. So, such language allows String objects to refer to only other String objects.&lt;br /&gt;
String a; a will never be allowed to refer to anything other than a string, like a=5.&lt;br /&gt;
&lt;br /&gt;
While in Ruby, type refers to how the object reacts to a method or in other words does the object have a particular capability. Type refers to this capability. While a class to which the object belongs provides the initial capabilities, from that point on, the object is free to extend its capabilities independently of its pre-defined class. So,&lt;br /&gt;
&lt;br /&gt;
 a= ”abc”&lt;br /&gt;
 a= 5 are two perfectly acceptable statements in Ruby.&lt;br /&gt;
&lt;br /&gt;
This is duck-typing.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; Duck-Typing in action&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class American&lt;br /&gt;
  def football&lt;br /&gt;
    puts &amp;quot;We play it with hands&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class European&lt;br /&gt;
  def football&lt;br /&gt;
    puts &amp;quot;We play it with legs&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Martian&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
footballManiac = [American.new,European.new]&lt;br /&gt;
&lt;br /&gt;
footballManiac.each {|game| game.football}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://bigdingus.com/2007/12/08/just-what-is-this-javascript-object-you-handed-me/ A classic example][3] of how a scripting language, JavaScript, not implementing duck-typing can lead to some bad code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; The other side to Duck-Typing &amp;lt;/h2&amp;gt;&lt;br /&gt;
As with everything in life, there is always 2 perspectives about its usefulness. Following are some of the criticism against duck-typing :&lt;br /&gt;
&amp;lt;h3&amp;gt;Lack of compiler security&amp;lt;/h3&amp;gt;&lt;br /&gt;
There is no help from compiler for a programmer error like assigning a string to an integer and so on. Only when you run the code, the output might be funny and that’s when you realize a probable mistake in assignment. In a static-type language, there is always a compiler check and hence the problem does not arise. Obviously this problem can be alleviated by proper and exhaustive testing. But as we all know, no one can guarantee 100% code tests.&lt;br /&gt;
&amp;lt;h3&amp;gt;Contract between caller and the callee&amp;lt;/h3&amp;gt;&lt;br /&gt;
In a static type language, the caller knows the exact ‘type’ of parameters required by the callee and also the return-type from the callee. In dynamic type languages, no type is provided. So to understand what the callee expects, one has to look at the code and understand. This is difficult in most of the cases and mainly for third-party code. Again this problem can be solved by providing proper documentation of each API and the pre-requirements for execution. But who can guarantee that the documents correctly map the requirements?&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_1w59_nm&amp;diff=70194</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 1w59 nm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_1w59_nm&amp;diff=70194"/>
		<updated>2012-11-18T17:25:27Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;SaaS - 3.7. Mixins and Duck Typing&amp;lt;/h1&amp;gt;&lt;br /&gt;
This wiki is aimed at acting as a complementary read to the SaaS - 3.7. Mixins and Duck Typing lecture. This lecture describes three of the most important fundamentals of the Ruby language: Duck-Typing, Modules and Mixins. &lt;br /&gt;
&amp;lt;h1&amp;gt;Introduction&amp;lt;/h1&amp;gt;&lt;br /&gt;
*Duck-typing is one of the main features of many object-oriented languages. Duck-Typing describes how a language like Ruby, treats its objects as compared to a language like JAVA. We describe the beautiful features of Duck-typing and also its negative side-effects.&amp;lt;br&amp;gt;&lt;br /&gt;
*Modules define a namespace. It provides a mechanism to club several things together which naturally do-not form a common class. With modules, programmer does not have to worry of possible name-clashes of method name's with other methods written by different programmers. Modules handles this by making use of namespace.&amp;lt;br&amp;gt;&lt;br /&gt;
*Mixins extend from modules. modules are not classes and hence we cannot instantiate them. However, when modules are 'include'ed in a class, all its methods are available to that class. We say modules are 'mixed in' to the class or in other words 'mixins'. Modules which are mixed in behave as superclass and provide multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;[http://en.wikipedia.org/wiki/Duck_typing Duck Typing][1]&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;What is Duck Typing&amp;lt;/h2&amp;gt;[http://threebrothers.org/brendan/blog/death-by-duck-typing/[[File:Duckt.gif]]][2]&lt;br /&gt;
                     '''“If a object walks like a duck, quacks like a duck then treat it as a duck”'''&lt;br /&gt;
This is the ‘mantra’ of Ruby language and the most pleasing aspect of the language. What it means is that, whether an object responds to a method?, is only important and not the type of the object. &lt;br /&gt;
If a dragon responds to methods like swim and quacking, ruby is happy to treat dragon as a duck without having a concern of whether it actually is a duck or not. This gives powerful tools for a ruby programmer. &lt;br /&gt;
&lt;br /&gt;
This is possible in Ruby because we don’t declare the '[http://en.wikipedia.org/wiki/Programming_language#Type_system Type][3] of variables or methods like in JAVA; in Ruby, everything is an object and each object can be individually modified, irrespective of its implementation in the class to which it belongs. &lt;br /&gt;
&lt;br /&gt;
Consider the following example taken from the SAAS lecture:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
my_list.sort&lt;br /&gt;
&lt;br /&gt;
[5, 4, 3].sort&lt;br /&gt;
[&amp;quot;dog&amp;quot;, &amp;quot;cat&amp;quot;, &amp;quot;rat&amp;quot;].sort&lt;br /&gt;
[:a, :b, :c].sort&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here, the same function, sort, is responding to an array of integers, strings and literals. This is classic Ruby duck-typing at work. How sort works is that it compares the elements which are needed to be sorted. So as long as the elements can be compared, sort function works. In the above example, the array of integers, strings and literals implement the comparison operator and hence the same sort function works for all of them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; Why in Ruby and not in JAVA? &amp;lt;/h2&amp;gt;&lt;br /&gt;
The reason for duck-typing to be easily possible and implemented in language like Ruby and not in language like JAVA is that these 2 languages treat ‘type’ differently. &lt;br /&gt;
In JAVA or C++, type means class. When someone asks the type of “abc”, we say it is String. So, such language allows String objects to refer to only other String objects.&lt;br /&gt;
String a; a will never be allowed to refer to anything other than a string, like a=5.&lt;br /&gt;
&lt;br /&gt;
While in Ruby, type refers to how the object reacts to a method or in other words does the object have a particular capability. Type refers to this capability. While a class to which the object belongs provides the initial capabilities, from that point on, the object is free to extend its capabilities independently of its pre-defined class. So,&lt;br /&gt;
&lt;br /&gt;
 a= ”abc”&lt;br /&gt;
 a= 5 are two perfectly acceptable statements in Ruby.&lt;br /&gt;
&lt;br /&gt;
This is duck-typing.&lt;br /&gt;
&lt;br /&gt;
[http://bigdingus.com/2007/12/08/just-what-is-this-javascript-object-you-handed-me/ A classic example][3] of how a scripting language, JavaScript, not implementing duck-typing can lead to some bad code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; The other side to Duck-Typing &amp;lt;/h2&amp;gt;&lt;br /&gt;
As with everything in life, there is always 2 perspectives about its usefulness. Following are some of the criticism against duck-typing :&lt;br /&gt;
&amp;lt;h3&amp;gt;Lack of compiler security&amp;lt;/h3&amp;gt;&lt;br /&gt;
There is no help from compiler for a programmer error like assigning a string to an integer and so on. Only when you run the code, the output might be funny and that’s when you realize a probable mistake in assignment. In a static-type language, there is always a compiler check and hence the problem does not arise. Obviously this problem can be alleviated by proper and exhaustive testing. But as we all know, no one can guarantee 100% code tests.&lt;br /&gt;
&amp;lt;h3&amp;gt;Contract between caller and the callee&amp;lt;/h3&amp;gt;&lt;br /&gt;
In a static type language, the caller knows the exact ‘type’ of parameters required by the callee and also the return-type from the callee. In dynamic type languages, no type is provided. So to understand what the callee expects, one has to look at the code and understand. This is difficult in most of the cases and mainly for third-party code. Again this problem can be solved by providing proper documentation of each API and the pre-requirements for execution. But who can guarantee that the documents correctly map the requirements?&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_1w59_nm&amp;diff=70190</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 1w59 nm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_1w59_nm&amp;diff=70190"/>
		<updated>2012-11-18T17:21:53Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;SaaS - 3.7. Mixins and Duck Typing&amp;lt;/h1&amp;gt;&lt;br /&gt;
This wiki is aimed at acting as a complementary read to the SaaS - 3.7. Mixins and Duck Typing lecture. This lecture describes three of the most important fundamentals of the Ruby language: Duck-Typing, Modules and Mixins. &lt;br /&gt;
&amp;lt;h1&amp;gt;Introduction&amp;lt;/h1&amp;gt;&lt;br /&gt;
Duck-typing is one of the main features of many object-oriented languages. Duck-Typing describes how a language like Ruby, treats its objects as compared to a language like JAVA. We describe the beautiful features of Duck-typing and also its negative side-effects.&amp;lt;br&amp;gt;&lt;br /&gt;
Modules define a namespace. It provides a mechanism to club several things together which naturally do-not form a common class. With modules, programmer does not have to worry of possible name-clashes of method name's with other methods written by different programmers. Modules handles this by making use of namespace.&amp;lt;br&amp;gt;&lt;br /&gt;
Mixins extend from modules. modules are not classes and hence we cannot instantiate them. However, when modules are 'include'ed in a class, all its methods are available to that class. We say modules are 'mixed in' to the class or in other words 'mixins'. Modules which are mixed in behave as superclass and provide multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;[http://en.wikipedia.org/wiki/Duck_typing Duck Typing][1]&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;What is Duck Typing&amp;lt;/h2&amp;gt;[http://threebrothers.org/brendan/blog/death-by-duck-typing/[[File:Duckt.gif]]][2]&lt;br /&gt;
                     '''“If a object walks like a duck, quacks like a duck then treat it as a duck”'''&lt;br /&gt;
This is the ‘mantra’ of Ruby language and the most pleasing aspect of the language. What it means is that, whether an object responds to a method?, is only important and not the type of the object. &lt;br /&gt;
If a dragon responds to methods like swim and quacking, ruby is happy to treat dragon as a duck without having a concern of whether it actually is a duck or not. This gives powerful tools for a ruby programmer. &lt;br /&gt;
&lt;br /&gt;
This is possible in Ruby because we don’t declare the '[http://en.wikipedia.org/wiki/Programming_language#Type_system Type][3] of variables or methods like in JAVA; in Ruby, everything is an object and each object can be individually modified, irrespective of its implementation in the class to which it belongs. &lt;br /&gt;
&lt;br /&gt;
Consider the following example taken from the SAAS lecture:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
my_list.sort&lt;br /&gt;
&lt;br /&gt;
[5, 4, 3].sort&lt;br /&gt;
[&amp;quot;dog&amp;quot;, &amp;quot;cat&amp;quot;, &amp;quot;rat&amp;quot;].sort&lt;br /&gt;
[:a, :b, :c].sort&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here, the same function, sort, is responding to an array of integers, strings and literals. This is classic Ruby duck-typing at work. How sort works is that it compares the elements which are needed to be sorted. So as long as the elements can be compared, sort function works. In the above example, the array of integers, strings and literals implement the comparison operator and hence the same sort function works for all of them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; Why in Ruby and not in JAVA? &amp;lt;/h2&amp;gt;&lt;br /&gt;
The reason for duck-typing to be easily possible and implemented in language like Ruby and not in language like JAVA is that these 2 languages treat ‘type’ differently. &lt;br /&gt;
In JAVA or C++, type means class. When someone asks the type of “abc”, we say it is String. So, such language allows String objects to refer to only other String objects.&lt;br /&gt;
String a; a will never be allowed to refer to anything other than a string, like a=5.&lt;br /&gt;
&lt;br /&gt;
While in Ruby, type refers to how the object reacts to a method or in other words does the object have a particular capability. Type refers to this capability. While a class to which the object belongs provides the initial capabilities, from that point on, the object is free to extend its capabilities independently of its pre-defined class. So,&lt;br /&gt;
&lt;br /&gt;
 a= ”abc”&lt;br /&gt;
 a= 5 are two perfectly acceptable statements in Ruby.&lt;br /&gt;
&lt;br /&gt;
This is duck-typing.&lt;br /&gt;
&lt;br /&gt;
[http://bigdingus.com/2007/12/08/just-what-is-this-javascript-object-you-handed-me/ A classic example][3] of how a scripting language, JavaScript, not implementing duck-typing can lead to some bad code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; The other side to Duck-Typing &amp;lt;/h2&amp;gt;&lt;br /&gt;
As with everything in life, there is always 2 perspectives about its usefulness. Following are some of the criticism against duck-typing :&lt;br /&gt;
&amp;lt;h3&amp;gt;Lack of compiler security&amp;lt;/h3&amp;gt;&lt;br /&gt;
There is no help from compiler for a programmer error like assigning a string to an integer and so on. Only when you run the code, the output might be funny and that’s when you realize a probable mistake in assignment. In a static-type language, there is always a compiler check and hence the problem does not arise. Obviously this problem can be alleviated by proper and exhaustive testing. But as we all know, no one can guarantee 100% code tests.&lt;br /&gt;
&amp;lt;h3&amp;gt;Contract between caller and the callee&amp;lt;/h3&amp;gt;&lt;br /&gt;
In a static type language, the caller knows the exact ‘type’ of parameters required by the callee and also the return-type from the callee. In dynamic type languages, no type is provided. So to understand what the callee expects, one has to look at the code and understand. This is difficult in most of the cases and mainly for third-party code. Again this problem can be solved by providing proper documentation of each API and the pre-requirements for execution. But who can guarantee that the documents correctly map the requirements?&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_1w59_nm&amp;diff=70179</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 1w59 nm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_1w59_nm&amp;diff=70179"/>
		<updated>2012-11-18T17:05:29Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;SaaS - 3.7. Mixins and Duck Typing&amp;lt;/h1&amp;gt;&lt;br /&gt;
This wiki is aimed at acting as a complementary read to the SaaS - 3.7. Mixins and Duck Typing lecture. This lecture describes three of the most important fundamentals of the Ruby language, Duck-Typing, Modules and Mixins. &lt;br /&gt;
&amp;lt;h1&amp;gt;Introduction&amp;lt;/h1&amp;gt;&lt;br /&gt;
Lets look at these cocepts in much more detail in the following sections.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;[http://en.wikipedia.org/wiki/Duck_typing Duck Typing][1]&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;What is Duck Typing&amp;lt;/h2&amp;gt;[http://threebrothers.org/brendan/blog/death-by-duck-typing/[[File:Duckt.gif]]][2]&lt;br /&gt;
                     '''“If a object walks like a duck, quacks like a duck then treat it as a duck”'''&lt;br /&gt;
This is the ‘mantra’ of Ruby language and the most pleasing aspect of the language. What it means is that, whether an object responds to a method?, is only important and not the type of the object. &lt;br /&gt;
If a dragon responds to methods like swim and quacking, ruby is happy to treat dragon as a duck without having a concern of whether it actually is a duck or not. This gives powerful tools for a ruby programmer. &lt;br /&gt;
&lt;br /&gt;
This is possible in Ruby because we don’t declare the '[http://en.wikipedia.org/wiki/Programming_language#Type_system Type][3] of variables or methods like in JAVA; in Ruby, everything is an object and each object can be individually modified, irrespective of its implementation in the class to which it belongs. &lt;br /&gt;
&lt;br /&gt;
Consider the following example taken from the SAAS lecture:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
my_list.sort&lt;br /&gt;
&lt;br /&gt;
[5, 4, 3].sort&lt;br /&gt;
[&amp;quot;dog&amp;quot;, &amp;quot;cat&amp;quot;, &amp;quot;rat&amp;quot;].sort&lt;br /&gt;
[:a, :b, :c].sort&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here, the same function, sort, is responding to an array of integers, strings and literals. This is classic Ruby duck-typing at work. How sort works is that it compares the elements which are needed to be sorted. So as long as the elements can be compared, sort function works. In the above example, the array of integers, strings and literals implement the comparison operator and hence the same sort function works for all of them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; Why in Ruby and not in JAVA? &amp;lt;/h2&amp;gt;&lt;br /&gt;
The reason for duck-typing to be easily possible and implemented in language like Ruby and not in language like JAVA is that these 2 languages treat ‘type’ differently. &lt;br /&gt;
In JAVA or C++, type means class. When someone asks the type of “abc”, we say it is String. So, such language allows String objects to refer to only other String objects.&lt;br /&gt;
String a; a will never be allowed to refer to anything other than a string, like a=5.&lt;br /&gt;
&lt;br /&gt;
While in Ruby, type refers to how the object reacts to a method or in other words does the object have a particular capability. Type refers to this capability. While a class to which the object belongs provides the initial capabilities, from that point on, the object is free to extend its capabilities independently of its pre-defined class. So,&lt;br /&gt;
&lt;br /&gt;
 a= ”abc”&lt;br /&gt;
 a= 5 are two perfectly acceptable statements in Ruby.&lt;br /&gt;
&lt;br /&gt;
This is duck-typing.&lt;br /&gt;
&lt;br /&gt;
[http://bigdingus.com/2007/12/08/just-what-is-this-javascript-object-you-handed-me/ A classic example][3] of how a scripting language, JavaScript, not implementing duck-typing can lead to some bad code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; The other side to Duck-Typing &amp;lt;/h2&amp;gt;&lt;br /&gt;
As with everything in life, there is always 2 perspectives about its usefulness. Following are some of the criticism against duck-typing :&lt;br /&gt;
&amp;lt;h3&amp;gt;Lack of compiler security&amp;lt;/h3&amp;gt;&lt;br /&gt;
There is no help from compiler for a programmer error like assigning a string to an integer and so on. Only when you run the code, the output might be funny and that’s when you realize a probable mistake in assignment. In a static-type language, there is always a compiler check and hence the problem does not arise. Obviously this problem can be alleviated by proper and exhaustive testing. But as we all know, no one can guarantee 100% code tests.&lt;br /&gt;
&amp;lt;h3&amp;gt;Contract between caller and the callee&amp;lt;/h3&amp;gt;&lt;br /&gt;
In a static type language, the caller knows the exact ‘type’ of parameters required by the callee and also the return-type from the callee. In dynamic type languages, no type is provided. So to understand what the callee expects, one has to look at the code and understand. This is difficult in most of the cases and mainly for third-party code. Again this problem can be solved by providing proper documentation of each API and the pre-requirements for execution. But who can guarantee that the documents correctly map the requirements?&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_1w59_nm&amp;diff=70177</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 1w59 nm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_1w59_nm&amp;diff=70177"/>
		<updated>2012-11-18T17:00:00Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;SaaS - 3.7. Mixins and Duck Typing&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Introduction&amp;lt;/h2&amp;gt;&lt;br /&gt;
This wiki is aimed at acting as a complementary read to the SaaS - 3.7. Mixins and Duck Typing lecture. This lecture describes three of the most important fundamentals of the Ruby language. Duck-Typing, Modules and Mixins. Lets look at these cocepts in much more detail in the following sections.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;[http://en.wikipedia.org/wiki/Duck_typing Duck Typing][1]&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;h3&amp;gt;What is Duck Typing&amp;lt;/h3&amp;gt;[http://threebrothers.org/brendan/blog/death-by-duck-typing/[[File:Duckt.gif]]][2]&lt;br /&gt;
                     '''“If a object walks like a duck, quacks like a duck then treat it as a duck”'''&lt;br /&gt;
This is the ‘mantra’ of Ruby language and the most pleasing aspect of the language. What it means is that, whether an object responds to a method?, is only important and not the type of the object. &lt;br /&gt;
If a dragon responds to methods like swim and quacking, ruby is happy to treat dragon as a duck without having a concern of whether it actually is a duck or not. This gives powerful tools for a ruby programmer. &lt;br /&gt;
&lt;br /&gt;
This is possible in Ruby because we don’t declare the '[http://en.wikipedia.org/wiki/Programming_language#Type_system Type][3] of variables or methods like in JAVA; in Ruby, everything is an object and each object can be individually modified, irrespective of its implementation in the class to which it belongs. &lt;br /&gt;
&lt;br /&gt;
Consider the following example taken from the SAAS lecture:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
my_list.sort&lt;br /&gt;
&lt;br /&gt;
[5, 4, 3].sort&lt;br /&gt;
[&amp;quot;dog&amp;quot;, &amp;quot;cat&amp;quot;, &amp;quot;rat&amp;quot;].sort&lt;br /&gt;
[:a, :b, :c].sort&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here, the same function, sort, is responding to an array of integers, strings and literals. This is classic Ruby duck-typing at work. How sort works is that it compares the elements which are needed to be sorted. So as long as the elements can be compared, sort function works. In the above example, the array of integers, strings and literals implement the comparison operator and hence the same sort function works for all of them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h3&amp;gt; Why in Ruby and not in JAVA? &amp;lt;/h3&amp;gt;&lt;br /&gt;
The reason for duck-typing to be easily possible and implemented in language like Ruby and not in language like JAVA is that these 2 languages treat ‘type’ differently. &lt;br /&gt;
In JAVA or C++, type means class. When someone asks the type of “abc”, we say it is String. So, such language allows String objects to refer to only other String objects.&lt;br /&gt;
String a; a will never be allowed to refer to anything other than a string, like a=5.&lt;br /&gt;
&lt;br /&gt;
While in Ruby, type refers to how the object reacts to a method or in other words does the object have a particular capability. Type refers to this capability. While a class to which the object belongs provides the initial capabilities, from that point on, the object is free to extend its capabilities independently of its pre-defined class. So,&lt;br /&gt;
&lt;br /&gt;
 a= ”abc”&lt;br /&gt;
 a= 5 are two perfectly acceptable statements in Ruby.&lt;br /&gt;
&lt;br /&gt;
This is duck-typing.&lt;br /&gt;
&lt;br /&gt;
[http://bigdingus.com/2007/12/08/just-what-is-this-javascript-object-you-handed-me/ A classic example][3] of how a scripting language, JavaScript, not implementing duck-typing can lead to some bad code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h3&amp;gt; The other side to Duck-Typing &amp;lt;/h3&amp;gt;&lt;br /&gt;
As with everything in life, there is always 2 perspectives about its usefulness. Following are some of the criticism against duck-typing :&lt;br /&gt;
&amp;lt;h4&amp;gt;Lack of compiler security&amp;lt;/h4&amp;gt;&lt;br /&gt;
There is no help from compiler for a programmer error like assigning a string to an integer and so on. Only when you run the code, the output might be funny and that’s when you realize a probable mistake in assignment. In a static-type language, there is always a compiler check and hence the problem does not arise. Obviously this problem can be alleviated by proper and exhaustive testing. But as we all know, no one can guarantee 100% code tests.&lt;br /&gt;
&amp;lt;h4&amp;gt;Contract between caller and the callee&amp;lt;/h4&amp;gt;&lt;br /&gt;
In a static type language, the caller knows the exact ‘type’ of parameters required by the callee and also the return-type from the callee. In dynamic type languages, no type is provided. So to understand what the callee expects, one has to look at the code and understand. This is difficult in most of the cases and mainly for third-party code. Again this problem can be solved by providing proper documentation of each API and the pre-requirements for execution. But who can guarantee that the documents correctly map the requirements?&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_1w59_nm&amp;diff=70119</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 1w59 nm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_1w59_nm&amp;diff=70119"/>
		<updated>2012-11-18T08:11:53Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;SaaS - 3.7. Mixins and Duck Typing&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;[http://en.wikipedia.org/wiki/Duck_typing Duck Typing][1]&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;What is Duck Typing&amp;lt;/h2&amp;gt;[http://threebrothers.org/brendan/blog/death-by-duck-typing/[[File:Duckt.gif]]][2]&lt;br /&gt;
                     '''“If a object walks like a duck, quacks like a duck then treat it as a duck”'''&lt;br /&gt;
This is the ‘mantra’ of Ruby language and the most pleasing aspect of the language. What it means is that, whether an object responds to a method?, is only important and not the type of the object. &lt;br /&gt;
If a dragon responds to methods like swim and quacking, ruby is happy to treat dragon as a duck without having a concern of whether it actually is a duck or not. This gives powerful tools for a ruby programmer. &lt;br /&gt;
&lt;br /&gt;
This is possible in Ruby because we don’t declare the '[http://en.wikipedia.org/wiki/Programming_language#Type_system Type][3] of variables or methods like in JAVA; in Ruby, everything is an object and each object can be individually modified, irrespective of its implementation in the class to which it belongs. &lt;br /&gt;
&lt;br /&gt;
Consider the following example taken from the SAAS lecture:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
my_list.sort&lt;br /&gt;
&lt;br /&gt;
[5, 4, 3].sort&lt;br /&gt;
[&amp;quot;dog&amp;quot;, &amp;quot;cat&amp;quot;, &amp;quot;rat&amp;quot;].sort&lt;br /&gt;
[:a, :b, :c].sort&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here, the same function, sort, is responding to an array of integers, strings and literals. This is classic Ruby duck-typing at work. How sort works is that it compares the elements which are needed to be sorted. So as long as the elements can be compared, sort function works. In the above example, the array of integers, strings and literals implement the comparison operator and hence the same sort function works for all of them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; Why in Ruby and not in JAVA? &amp;lt;/h2&amp;gt;&lt;br /&gt;
The reason for duck-typing to be easily possible and implemented in language like Ruby and not in language like JAVA is that these 2 languages treat ‘type’ differently. &lt;br /&gt;
In JAVA or C++, type means class. When someone asks the type of “abc”, we say it is String. So, such language allows String objects to refer to only other String objects.&lt;br /&gt;
String a; a will never be allowed to refer to anything other than a string, like a=5.&lt;br /&gt;
&lt;br /&gt;
While in Ruby, type refers to how the object reacts to a method or in other words does the object have a particular capability. Type refers to this capability. While a class to which the object belongs provides the initial capabilities, from that point on, the object is free to extend its capabilities independently of its pre-defined class. So,&lt;br /&gt;
&lt;br /&gt;
 a= ”abc”&lt;br /&gt;
 a= 5 are two perfectly acceptable statements in Ruby.&lt;br /&gt;
&lt;br /&gt;
This is duck-typing.&lt;br /&gt;
&lt;br /&gt;
[http://bigdingus.com/2007/12/08/just-what-is-this-javascript-object-you-handed-me/ A classic example][3] of how a scripting language, JavaScript, not implementing duck-typing can lead to some bad code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; The other side to Duck-Typing &amp;lt;/h2&amp;gt;&lt;br /&gt;
As with everything in life, there is always 2 perspectives about its usefulness. Following are some of the criticism against duck-typing :&lt;br /&gt;
&amp;lt;h3&amp;gt;Lack of compiler security&amp;lt;/h3&amp;gt;&lt;br /&gt;
There is no help from compiler for a programmer error like assigning a string to an integer and so on. Only when you run the code, the output might be funny and that’s when you realize a probable mistake in assignment. In a static-type language, there is always a compiler check and hence the problem does not arise. Obviously this problem can be alleviated by proper and exhaustive testing. But as we all know, no one can guarantee 100% code tests.&lt;br /&gt;
&amp;lt;h3&amp;gt;Contract between caller and the callee&amp;lt;/h3&amp;gt;&lt;br /&gt;
In a static type language, the caller knows the exact ‘type’ of parameters required by the callee and also the return-type from the callee. In dynamic type languages, no type is provided. So to understand what the callee expects, one has to look at the code and understand. This is difficult in most of the cases and mainly for third-party code. Again this problem can be solved by providing proper documentation of each API and the pre-requirements for execution. But who can guarantee that the documents correctly map the requirements?&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_1w59_nm&amp;diff=70118</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 1w59 nm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_1w59_nm&amp;diff=70118"/>
		<updated>2012-11-18T08:11:24Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;SaaS - 3.7. Mixins and Duck Typing[1]&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;[http://en.wikipedia.org/wiki/Duck_typing Duck Typing]&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;What is Duck Typing&amp;lt;/h2&amp;gt;[http://threebrothers.org/brendan/blog/death-by-duck-typing/[[File:Duckt.gif]]][2]&lt;br /&gt;
                     '''“If a object walks like a duck, quacks like a duck then treat it as a duck”'''&lt;br /&gt;
This is the ‘mantra’ of Ruby language and the most pleasing aspect of the language. What it means is that, whether an object responds to a method?, is only important and not the type of the object. &lt;br /&gt;
If a dragon responds to methods like swim and quacking, ruby is happy to treat dragon as a duck without having a concern of whether it actually is a duck or not. This gives powerful tools for a ruby programmer. &lt;br /&gt;
&lt;br /&gt;
This is possible in Ruby because we don’t declare the '[http://en.wikipedia.org/wiki/Programming_language#Type_system Type][3] of variables or methods like in JAVA; in Ruby, everything is an object and each object can be individually modified, irrespective of its implementation in the class to which it belongs. &lt;br /&gt;
&lt;br /&gt;
Consider the following example taken from the SAAS lecture:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
my_list.sort&lt;br /&gt;
&lt;br /&gt;
[5, 4, 3].sort&lt;br /&gt;
[&amp;quot;dog&amp;quot;, &amp;quot;cat&amp;quot;, &amp;quot;rat&amp;quot;].sort&lt;br /&gt;
[:a, :b, :c].sort&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here, the same function, sort, is responding to an array of integers, strings and literals. This is classic Ruby duck-typing at work. How sort works is that it compares the elements which are needed to be sorted. So as long as the elements can be compared, sort function works. In the above example, the array of integers, strings and literals implement the comparison operator and hence the same sort function works for all of them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; Why in Ruby and not in JAVA? &amp;lt;/h2&amp;gt;&lt;br /&gt;
The reason for duck-typing to be easily possible and implemented in language like Ruby and not in language like JAVA is that these 2 languages treat ‘type’ differently. &lt;br /&gt;
In JAVA or C++, type means class. When someone asks the type of “abc”, we say it is String. So, such language allows String objects to refer to only other String objects.&lt;br /&gt;
String a; a will never be allowed to refer to anything other than a string, like a=5.&lt;br /&gt;
&lt;br /&gt;
While in Ruby, type refers to how the object reacts to a method or in other words does the object have a particular capability. Type refers to this capability. While a class to which the object belongs provides the initial capabilities, from that point on, the object is free to extend its capabilities independently of its pre-defined class. So,&lt;br /&gt;
&lt;br /&gt;
 a= ”abc”&lt;br /&gt;
 a= 5 are two perfectly acceptable statements in Ruby.&lt;br /&gt;
&lt;br /&gt;
This is duck-typing.&lt;br /&gt;
&lt;br /&gt;
[http://bigdingus.com/2007/12/08/just-what-is-this-javascript-object-you-handed-me/ A classic example][3] of how a scripting language, JavaScript, not implementing duck-typing can lead to some bad code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; The other side to Duck-Typing &amp;lt;/h2&amp;gt;&lt;br /&gt;
As with everything in life, there is always 2 perspectives about its usefulness. Following are some of the criticism against duck-typing :&lt;br /&gt;
&amp;lt;h3&amp;gt;Lack of compiler security&amp;lt;/h3&amp;gt;&lt;br /&gt;
There is no help from compiler for a programmer error like assigning a string to an integer and so on. Only when you run the code, the output might be funny and that’s when you realize a probable mistake in assignment. In a static-type language, there is always a compiler check and hence the problem does not arise. Obviously this problem can be alleviated by proper and exhaustive testing. But as we all know, no one can guarantee 100% code tests.&lt;br /&gt;
&amp;lt;h3&amp;gt;Contract between caller and the callee&amp;lt;/h3&amp;gt;&lt;br /&gt;
In a static type language, the caller knows the exact ‘type’ of parameters required by the callee and also the return-type from the callee. In dynamic type languages, no type is provided. So to understand what the callee expects, one has to look at the code and understand. This is difficult in most of the cases and mainly for third-party code. Again this problem can be solved by providing proper documentation of each API and the pre-requirements for execution. But who can guarantee that the documents correctly map the requirements?&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Duckt.gif&amp;diff=70117</id>
		<title>File:Duckt.gif</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Duckt.gif&amp;diff=70117"/>
		<updated>2012-11-18T08:07:27Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: Duck&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Duck&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_1w59_nm&amp;diff=70116</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 1w59 nm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_1w59_nm&amp;diff=70116"/>
		<updated>2012-11-18T08:03:45Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;SaaS - 3.7. Mixins and Duck Typing[1]&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;[http://en.wikipedia.org/wiki/Duck_typing Duck Typing]&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;What is Duck Typing&amp;lt;/h2&amp;gt;&lt;br /&gt;
                     '''“If a object walks like a duck, quacks like a duck then treat it as a duck”'''&lt;br /&gt;
This is the ‘mantra’ of Ruby language and the most pleasing aspect of the language. What it means is that, whether an object responds to a method?, is only important and not the type of the object. &lt;br /&gt;
If a dragon responds to methods like swim and quacking, ruby is happy to treat dragon as a duck without having a concern of whether it actually is a duck or not. This gives powerful tools for a ruby programmer. &lt;br /&gt;
&lt;br /&gt;
This is possible in Ruby because we don’t declare the '[http://en.wikipedia.org/wiki/Programming_language#Type_system Type][2] of variables or methods like in JAVA; in Ruby, everything is an object and each object can be individually modified, irrespective of its implementation in the class to which it belongs. &lt;br /&gt;
&lt;br /&gt;
Consider the following example taken from the SAAS lecture:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
my_list.sort&lt;br /&gt;
&lt;br /&gt;
[5, 4, 3].sort&lt;br /&gt;
[&amp;quot;dog&amp;quot;, &amp;quot;cat&amp;quot;, &amp;quot;rat&amp;quot;].sort&lt;br /&gt;
[:a, :b, :c].sort&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here, the same function, sort, is responding to an array of integers, strings and literals. This is classic Ruby duck-typing at work. How sort works is that it compares the elements which are needed to be sorted. So as long as the elements can be compared, sort function works. In the above example, the array of integers, strings and literals implement the comparison operator and hence the same sort function works for all of them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; Why in Ruby and not in JAVA? &amp;lt;/h2&amp;gt;&lt;br /&gt;
The reason for duck-typing to be easily possible and implemented in language like Ruby and not in language like JAVA is that these 2 languages treat ‘type’ differently. &lt;br /&gt;
In JAVA or C++, type means class. When someone asks the type of “abc”, we say it is String. So, such language allows String objects to refer to only other String objects.&lt;br /&gt;
String a; a will never be allowed to refer to anything other than a string, like a=5.&lt;br /&gt;
&lt;br /&gt;
While in Ruby, type refers to how the object reacts to a method or in other words does the object have a particular capability. Type refers to this capability. While a class to which the object belongs provides the initial capabilities, from that point on, the object is free to extend its capabilities independently of its pre-defined class. So,&lt;br /&gt;
&lt;br /&gt;
 a= ”abc”&lt;br /&gt;
 a= 5 are two perfectly acceptable statements in Ruby.&lt;br /&gt;
&lt;br /&gt;
This is duck-typing.&lt;br /&gt;
&lt;br /&gt;
[http://bigdingus.com/2007/12/08/just-what-is-this-javascript-object-you-handed-me/ A classic example][3] of how a scripting language, JavaScript, not implementing duck-typing can lead to some bad code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; The other side to Duck-Typing &amp;lt;/h2&amp;gt;&lt;br /&gt;
As with everything in life, there is always 2 perspectives about its usefulness. Following are some of the criticism against duck-typing :&lt;br /&gt;
&amp;lt;h3&amp;gt;Lack of compiler security&amp;lt;/h3&amp;gt;&lt;br /&gt;
There is no help from compiler for a programmer error like assigning a string to an integer and so on. Only when you run the code, the output might be funny and that’s when you realize a probable mistake in assignment. In a static-type language, there is always a compiler check and hence the problem does not arise. Obviously this problem can be alleviated by proper and exhaustive testing. But as we all know, no one can guarantee 100% code tests.&lt;br /&gt;
&amp;lt;h3&amp;gt;Contract between caller and the callee&amp;lt;/h3&amp;gt;&lt;br /&gt;
In a static type language, the caller knows the exact ‘type’ of parameters required by the callee and also the return-type from the callee. In dynamic type languages, no type is provided. So to understand what the callee expects, one has to look at the code and understand. This is difficult in most of the cases and mainly for third-party code. Again this problem can be solved by providing proper documentation of each API and the pre-requirements for execution. But who can guarantee that the documents correctly map the requirements?&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_1w59_nm&amp;diff=70115</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 1w59 nm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_1w59_nm&amp;diff=70115"/>
		<updated>2012-11-18T07:13:52Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;SaaS - 3.7. Mixins and Duck Typing[1]&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;[http://en.wikipedia.org/wiki/Duck_typing Duck Typing]&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;What is Duck Typing&amp;lt;/h2&amp;gt;&lt;br /&gt;
                     '''“If a object walks like a duck, quacks like a duck then treat it as a duck”'''&lt;br /&gt;
This is the ‘mantra’ of Ruby language and the most pleasing aspect of the language. What it means is that, whether an object responds to a method?, is only important and not the type of the object. &lt;br /&gt;
If a dragon responds to methods like swim and quacking, ruby is happy to treat dragon as a duck without having a concern of whether it actually is a duck or not. This gives powerful tools for a ruby programmer. &lt;br /&gt;
&lt;br /&gt;
This is possible in Ruby because we don’t declare the '[http://en.wikipedia.org/wiki/Programming_language#Type_system Type][2] of variables or methods like in JAVA; in Ruby, everything is an object and each object can be individually modified, irrespective of its implementation in the class to which it belongs. &lt;br /&gt;
&lt;br /&gt;
Consider the following example taken from the SAAS lecture:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
my_list.sort&lt;br /&gt;
&lt;br /&gt;
[5, 4, 3].sort&lt;br /&gt;
[&amp;quot;dog&amp;quot;, &amp;quot;cat&amp;quot;, &amp;quot;rat&amp;quot;].sort&lt;br /&gt;
[:a, :b, :c].sort&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here, the same function, sort, is responding to an array of integers, strings and literals. This is classic Ruby duck-typing at work. How sort works is that it compares the elements which are needed to be sorted. So as long as the elements can be compared, sort function works. In the above example, the array of integers, strings and literals implement the comparison operator and hence the same sort function works for all of them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; Why in Ruby and not in JAVA? &amp;lt;/h2&amp;gt;&lt;br /&gt;
The reason for duck-typing to be easily possible and implemented in language like Ruby and not in language like JAVA is that these 2 languages treat ‘type’ differently. &lt;br /&gt;
In JAVA or C++, type means class. When someone asks the type of “abc”, we say it is String. So, such language allows String objects to refer to only other String objects.&lt;br /&gt;
String a; a will never be allowed to refer to anything other than a string, like a=5.&lt;br /&gt;
&lt;br /&gt;
While in Ruby, type refers to how the object reacts to a method or in other words does the object have a particular capability. Type refers to this capability. While a class to which the object belongs provides the initial capabilities, from that point on, the object is free to extend its capabilities independently of its pre-defined class. So,&lt;br /&gt;
&lt;br /&gt;
 a= ”abc”&lt;br /&gt;
 a= 5 are two perfectly acceptable statements in Ruby.&lt;br /&gt;
&lt;br /&gt;
This is duck-typing.&lt;br /&gt;
&lt;br /&gt;
[http://bigdingus.com/2007/12/08/just-what-is-this-javascript-object-you-handed-me/ A classic example][3] of how a scripting language, JavaScript, not implementing duck-typing can lead to some bad code.&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_1w59_nm&amp;diff=70114</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 1w59 nm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_1w59_nm&amp;diff=70114"/>
		<updated>2012-11-18T07:12:27Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;SaaS - 3.7. Mixins and Duck Typing&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;[http://en.wikipedia.org/wiki/Duck_typing Duck Typing]&amp;lt;/h1&amp;gt;[1]&lt;br /&gt;
&amp;lt;h2&amp;gt;What is Duck Typing&amp;lt;/h2&amp;gt;&lt;br /&gt;
                     '''“If a object walks like a duck, quacks like a duck then treat it as a duck”'''&lt;br /&gt;
This is the ‘mantra’ of Ruby language and the most pleasing aspect of the language. What it means is that, whether an object responds to a method?, is only important and not the type of the object. &lt;br /&gt;
If a dragon responds to methods like swim and quacking, ruby is happy to treat dragon as a duck without having a concern of whether it actually is a duck or not. This gives powerful tools for a ruby programmer. &lt;br /&gt;
&lt;br /&gt;
This is possible in Ruby because we don’t declare the '[http://en.wikipedia.org/wiki/Programming_language#Type_system Type][2] of variables or methods like in JAVA; in Ruby, everything is an object and each object can be individually modified, irrespective of its implementation in the class to which it belongs. &lt;br /&gt;
&lt;br /&gt;
Consider the following example taken from the SAAS lecture:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
my_list.sort&lt;br /&gt;
&lt;br /&gt;
[5, 4, 3].sort&lt;br /&gt;
[&amp;quot;dog&amp;quot;, &amp;quot;cat&amp;quot;, &amp;quot;rat&amp;quot;].sort&lt;br /&gt;
[:a, :b, :c].sort&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here, the same function, sort, is responding to an array of integers, strings and literals. This is classic Ruby duck-typing at work. How sort works is that it compares the elements which are needed to be sorted. So as long as the elements can be compared, sort function works. In the above example, the array of integers, strings and literals implement the comparison operator and hence the same sort function works for all of them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; Why in Ruby and not in JAVA? &amp;lt;/h2&amp;gt;&lt;br /&gt;
The reason for duck-typing to be easily possible and implemented in language like Ruby and not in language like JAVA is that these 2 languages treat ‘type’ differently. &lt;br /&gt;
In JAVA or C++, type means class. When someone asks the type of “abc”, we say it is String. So, such language allows String objects to refer to only other String objects.&lt;br /&gt;
String a; a will never be allowed to refer to anything other than a string, like a=5.&lt;br /&gt;
&lt;br /&gt;
While in Ruby, type refers to how the object reacts to a method or in other words does the object have a particular capability. Type refers to this capability. While a class to which the object belongs provides the initial capabilities, from that point on, the object is free to extend its capabilities independently of its pre-defined class. So,&lt;br /&gt;
&lt;br /&gt;
 a= ”abc”&lt;br /&gt;
 a= 5 are two perfectly acceptable statements in Ruby.&lt;br /&gt;
&lt;br /&gt;
This is duck-typing.&lt;br /&gt;
&lt;br /&gt;
[http://bigdingus.com/2007/12/08/just-what-is-this-javascript-object-you-handed-me/ A classic example][3] of how a scripting language, JavaScript, not implementing duck-typing can lead to some bad code.&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_1w59_nm&amp;diff=70113</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 1w59 nm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_1w59_nm&amp;diff=70113"/>
		<updated>2012-11-18T07:11:46Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;SaaS - 3.7. Mixins and Duck Typing&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;[http://en.wikipedia.org/wiki/Duck_typing Duck Typing][1]&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;What is Duck Typing&amp;lt;/h2&amp;gt;&lt;br /&gt;
                     '''“If a object walks like a duck, quacks like a duck then treat it as a duck”'''&lt;br /&gt;
This is the ‘mantra’ of Ruby language and the most pleasing aspect of the language. What it means is that, whether an object responds to a method?, is only important and not the type of the object. &lt;br /&gt;
If a dragon responds to methods like swim and quacking, ruby is happy to treat dragon as a duck without having a concern of whether it actually is a duck or not. This gives powerful tools for a ruby programmer. &lt;br /&gt;
&lt;br /&gt;
This is possible in Ruby because we don’t declare the '[http://en.wikipedia.org/wiki/Programming_language#Type_system Type][2] of variables or methods like in JAVA; in Ruby, everything is an object and each object can be individually modified, irrespective of its implementation in the class to which it belongs. &lt;br /&gt;
&lt;br /&gt;
Consider the following example taken from the SAAS lecture:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
my_list.sort&lt;br /&gt;
&lt;br /&gt;
[5, 4, 3].sort&lt;br /&gt;
[&amp;quot;dog&amp;quot;, &amp;quot;cat&amp;quot;, &amp;quot;rat&amp;quot;].sort&lt;br /&gt;
[:a, :b, :c].sort&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here, the same function, sort, is responding to an array of integers, strings and literals. This is classic Ruby duck-typing at work. How sort works is that it compares the elements which are needed to be sorted. So as long as the elements can be compared, sort function works. In the above example, the array of integers, strings and literals implement the comparison operator and hence the same sort function works for all of them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt; Why in Ruby and not in JAVA? &amp;lt;/h2&amp;gt;&lt;br /&gt;
The reason for duck-typing to be easily possible and implemented in language like Ruby and not in language like JAVA is that these 2 languages treat ‘type’ differently. &lt;br /&gt;
In JAVA or C++, type means class. When someone asks the type of “abc”, we say it is String. So, such language allows String objects to refer to only other String objects.&lt;br /&gt;
String a; a will never be allowed to refer to anything other than a string, like a=5.&lt;br /&gt;
&lt;br /&gt;
While in Ruby, type refers to how the object reacts to a method or in other words does the object have a particular capability. Type refers to this capability. While a class to which the object belongs provides the initial capabilities, from that point on, the object is free to extend its capabilities independently of its pre-defined class. So,&lt;br /&gt;
&lt;br /&gt;
 a= ”abc”&lt;br /&gt;
 a= 5 are two perfectly acceptable statements in Ruby.&lt;br /&gt;
&lt;br /&gt;
This is duck-typing.&lt;br /&gt;
&lt;br /&gt;
[http://bigdingus.com/2007/12/08/just-what-is-this-javascript-object-you-handed-me/ A classic example][3] of how a scripting language, JavaScript, not implementing duck-typing can lead to some bad code.&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_1w59_nm&amp;diff=70107</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 1w59 nm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_1w59_nm&amp;diff=70107"/>
		<updated>2012-11-18T06:56:08Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: Created page with &amp;quot;&amp;lt;h1&amp;gt;SaaS - 3.7. Mixins and Duck Typing&amp;lt;/h1&amp;gt;&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;SaaS - 3.7. Mixins and Duck Typing&amp;lt;/h1&amp;gt;&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w38_nm&amp;diff=67188</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w38 nm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w38_nm&amp;diff=67188"/>
		<updated>2012-10-04T02:20:24Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;Introduction&amp;lt;/h1&amp;gt;&lt;br /&gt;
This page is aimed at asserting the importance of Model View Controller Architecture and how MVC is implemented in Cookbook application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Model View Controller (MVC) Architecture&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The purpose of creating MVC architecture is to have clear separation between the responsibilities of business logic and representation. In a nutshell the responsibilities are mentioned as below:&lt;br /&gt;
&lt;br /&gt;
[[File:MVC.png|frame|none|MVC Responsibilities[8]]]&lt;br /&gt;
&lt;br /&gt;
Model-View-Controller (MVC)[1] is an architectural pattern used in software engineering.In complex computer applications that present a large amount of data to the user, a developer often wishes to separate data (model) and user interface (view) concerns, so that changes to the user interface will not affect data handling, and that the data can be reorganized without changing the user interface.&amp;lt;br&amp;gt;&lt;br /&gt;
The model-view-controller solves this problem by decoupling data access and business logic from data presentation and user interaction, by introducing an intermediate component: the controller. Having described the responsibilities of three components let’s take a look how these components interact with each other.&lt;br /&gt;
&lt;br /&gt;
[[File:MVC_architecture.PNG|frame|none|MVC Architecture[9]]] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Ruby on Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby on Rails[2] is an MVC framework. Rails enforces a structure for application, it helps to knit the models, views, and controllers as separate chunks of functionality while the program executes. No more external configuration needed. Rails is packed with features that make you more productive, with many of the following features building on one other.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Metaprogramming[7]:&amp;lt;/b&amp;gt; Other frameworks use extensive code generation from scratch. Metaprogramming techniques use programs to write programs. Ruby is one of the best languages for metaprogramming, and Rails uses this capability well. Rails also uses code generation but relies much more on metaprogramming for the heavy lifting.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Active Record[6]:&amp;lt;/b&amp;gt; Rails introduces the Active Record framework, which saves objects to the database. The Rails version of Active Record discovers the columns in a database schema and automatically attaches them to your domain objects using metaprogramming.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Convention over configuration:&amp;lt;/b&amp;gt; Most web development frameworks for .NET or Java force you to write pages of configuration code. If you follow suggested naming conventions, Rails doesn't need much configuration.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Scaffolding:&amp;lt;/b&amp;gt; You often create temporary code in the early stages of development to help get an application up quickly and see how major components work together. Rails automatically creates much of the scaffolding you'll need.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Built-in testing:&amp;lt;/b&amp;gt; Rails creates simple automated tests which you can extend. Rails also provides supporting code called harnesses and fixtures that make test cases easier to write and run. Ruby can then execute all your automated tests with the rake utility.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Three environments:&amp;lt;/b&amp;gt; Rails gives you three default environments: development, testing, and production. Each behaves slightly differently, making your entire software development cycle easier. For example, Rails creates a fresh copy of the Test database for each test run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Cookbook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
Cookbook Application is a very basic Ruby on Rails application. It is a great way to understand the nuances that ‘Ruby’ has to offer. It is built keeping in mind that the reader is new to the world of ruby and is developing his/her first Ruby on Rails application.&lt;br /&gt;
The motive of this wiki page is to make the reader understand how the Cookbook application was built. And on the way to building this application, user will be introduced to some key concepts like Model, Views and Controllers. At the end of this page, reader must fairly be ready to start building his/her basic Ruby on Rails application.&lt;br /&gt;
&lt;br /&gt;
In the Cookbook application we are considering a Cookbook which contains recipes for various dishes. The recipes have been divided into different categories, for example there is a recipe for ice tea which belongs to the category of beverages. There is a relationship which is created between a recipe and a category which can be expressed as an ice tea belongs to beverages. We can have many such recipes belonging to a single category, in this case many to one relationship is established. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;A simple application to start -- Cookbook application&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Start a new project. While creating a new project, remember to select Rails Application option as follows&lt;br /&gt;
&lt;br /&gt;
[[File:Start_application.png|frame|none|Start a new Application]] &lt;br /&gt;
&lt;br /&gt;
When we create a Rails application, it automatically creates a bunch of files and folders as follows:&lt;br /&gt;
&lt;br /&gt;
[[File:Listing_of_cookbook_application.png|frame|none|Listing_of_cookbook_application]] &lt;br /&gt;
&lt;br /&gt;
We mainly use the app directory. This is where the heart of the application is stored i.e. Models, Views and controllers.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Using scaffold to create framework&amp;lt;/h2&amp;gt;&lt;br /&gt;
Rails scaffolding is a quick way to generate some of the major pieces of an application. If you want to create the models, views, and controllers for a new resource in a single operation, scaffolding is the tool for the job.&amp;lt;br&amp;gt;&lt;br /&gt;
In Rubymine, we can create a scaffold by right clicking on the current Project-&amp;gt;New-&amp;gt;Run Rails Generator -&amp;gt;scaffold. This will open a dialog box to create a scaffold where we can enter the name and the attributes along with their data type. Once the scaffold has been created RubyMine will generate the model, view and controller files respectively. These files do not contain any specific methods but they provide a framework which is useful for general applications. The user can modify the MVC behavior according to his/her application requirements. Each scaffold has create, read, update and delete methods declared in the controller class. A user can modify the behavior of the controller through these methods. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Create_Scaffold.png|frame|none|Creating a new scaffold]]&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Scaffold.png|frame|left|Scaffold]] [[File:New_Scaffold_Categories.png|frame|center|Creating scaffold for categories]]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Working without scaffold&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or you don't want to create a fully standard framework with scaffold, then you can use RubyMine generate command to create model, controller and view individually. There are additional commands included in the generate command to create migration, assets, intergration_test and performance_test. &lt;br /&gt;
[[File:Generating_controller.png|frame|none|Generating Controller]] &lt;br /&gt;
[[File:Generating_controller_recipes.png|frame|none|Generating Recipes controller]]&lt;br /&gt;
&lt;br /&gt;
By clicking on the 'Add' button, we can add the required actions in the controller&lt;br /&gt;
Similarly models and views can be created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Controllers for Cookbook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
The Cookbook application starts by entering a URL in the browser which sends the request to the web server. Since our web server supports Rails application, it will hand over the control to a Rails controller.&lt;br /&gt;
A controller ‘controls’ the entire application by responding to the incoming requests as needed. For example, consider the following URL http ://localhost:3000/categories/show which is decoded by the controller to know that its action named show has been invoked.&lt;br /&gt;
All the controllers inherit only the ApplicationController which in turn inherits the ActionController::Base class. ActionContoller module provides the basic support for the controllers in Rails.&lt;br /&gt;
In our Cookbook application we generate Categories controller using the scaffold mechanism which populates the controller with the basic methods such as index, create, show, new, edit, update and destroy.&lt;br /&gt;
A simple example of index method is as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    @categories = Category.all&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # index.html.erb&lt;br /&gt;
      format.json { render json: @categories }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In ruby, @ operator is used to declare instance variables. The index method makes use of @categories instance variable which is populated with all the records in the Category table. The respond_to method is used for responding to particular type of request. It contains a block and a variable |format| which determines whether the incoming request should be responded with HTML format (when we are interacting with a user) or JSON format (if we are returning an object). &lt;br /&gt;
After executing the code, the controller will render a template of the same name. (For this example, the template is views/categories/index.html.erb).&lt;br /&gt;
Similarly the show method is defined as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def show&lt;br /&gt;
    @category = Category.find(params[:id])&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # show.html.erb&lt;br /&gt;
      format.json { render json: @category }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The show method is used to display a particular object. The show method needs to access database to search for the given object. This is done using the find method which takes params[:id] as argument. The params[:id] contains the id or primary key of the object which is passed as a parameter to look up in the database and the result is stored in the instance variable. The result is displayed by rendering the show.html.erb file.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Active Record&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, a program deals with objects which are mapped into database relational databases. This kind of mapping is facilitated by the Active Record.&amp;lt;br&amp;gt;Active Record maps the Rails classes in to database tables and rails objects into database records.&lt;br /&gt;
This makes it easy to perform actions on database tables by invoking the class methods. Another benefit of Active Record is database migration. Migrations can manage the evolution of a schema used by several physical databases. It’s a solution to the common problem of adding a field to make a new feature work in your local database, but being unsure of how to push that change to other developers and to the production server. With migrations, you can describe the transformations in self-contained classes that can be checked into version control systems and executed against another database that might be one, two, or five versions behind. You can find a lot more details about rails migration here[8].&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Method Pairs&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;There are couple of method pairs such as new-create and edit-update which are executed in sequence. In method pair one method is responsible for display preparation and the other method validates the data and attempts it to save in the database.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def new&lt;br /&gt;
    @category = Category.new&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # new.html.erb&lt;br /&gt;
      format.json { render json: @category }&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def create&lt;br /&gt;
    @category = Category.new(params[:category])&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      if @category.save&lt;br /&gt;
        format.html { redirect_to @category, notice: 'Category was successfully created.' }&lt;br /&gt;
        format.json { render json: @category, status: :created, location: @category }&lt;br /&gt;
      else&lt;br /&gt;
        format.html { render action: &amp;quot;new&amp;quot; }&lt;br /&gt;
        format.json { render json: @category.errors, status: :unprocessable_entity }&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The new method is used to instantiate a new Category object whereas create method is used to successfully save the object to the database. After execution of the new method, the controller renders the new.html.erb file which displays a form to create a new Category object. The user enters the required information in the form and the object id is returned in the URL on when the form is submitted. Consider the following URL which is generated on submitting the form: http: //localhost:3000/categories/3.&lt;br /&gt;
This means that a Category object with id = 3 will be created. The details of the object will be processed by the create method. &lt;br /&gt;
The controller invokes the create method to store the object in the database. An observation can be made about the @category variable in the create method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@category = Category.new(params[:category])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The parameters passed in the URL after creating a new object is stored in the params hash (denoted by params[]). In this case the params[] contains an object with category_id = 3. The params[] assigns the data of object with category_id = 3 to the instance variable. The data entered in the instance variable is validated using the @category.save method. If no errors are found then the object stored in the database and a message on successful creation of the object is displayed to the user. However, if the data entered in the object contains an error, the user is redirected to the new.html.erb page to resubmit the information and the errors are displayed. &lt;br /&gt;
Another example of such method pair is the edit and update methods. The edit method displays the table entry for an existing object, while the update method is invoked when the changes have been submitted.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Models of Cookbook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
Model handles the data processing in a web application. Actions interact with the model to manipulate the data. Rails is capable of handling databases and this is facilitated by the model. You can base models on the Rails ActiveRecord module which contains support databases.&lt;br /&gt;
In our Cookbook application we have two models namely: Category and Recipe. They are represented by files category.rb and recipe.rb and each file is used to describe the relation between two classes. Let’s take a look at the two models&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt; category.rb&amp;lt;/b&amp;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;
&amp;lt;b&amp;gt;recipe.rb&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;Relationships in models&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;one-to-one&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Syllabus &amp;lt; ActiveRecord::Base&lt;br /&gt;
	belongs_to :course¬&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Course &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_one :syllabus&lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;many-many&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Student &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_and_belongs_to_many :courses&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Course &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_and_belongs_to_many :students&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Cookbook, we have many-one relationship between recipes and categories.&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Views of Cookbook application&amp;lt;/h1&amp;gt;&lt;br /&gt;
What is V in MVC? ‘V’ refers to views. Views are the actual interface that users can see. It is through views, that a user may request some data to be viewed or submits some data for processing.&amp;lt;br&amp;gt;&lt;br /&gt;
Views in rails are of the form file_name.html.erb. Here erb means embedded ruby. What html.erb denotes is some ruby code is embedded in HTML code. Like HTML, embedded ruby also uses tags to represent data. However, to distinguish it from the HTML code, ruby is embedded between &amp;lt;% %&amp;gt; if some non-assignment ruby operation is to be performed. If something needs to be displayed on screen, &amp;lt;%= %&amp;gt; is used.&lt;br /&gt;
Views are present in “app/views” folder in the rails application.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Name of the view files is same as the name of their respective controller. For e.g.:- if I have a controller movie with an action new, the corresponding view page will be new.html.erb in the app/views/movie directory.&amp;lt;br&amp;gt;&lt;br /&gt;
The Cookbook application views are as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Recipe Controller Views&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
1. index.html.erb&lt;br /&gt;
This page is used to display all the recipes that exist in the database.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;nowiki&amp;gt;&amp;lt;h1&amp;gt;Listing categories&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
  &amp;lt;tr&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;Name&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;% @categories.each do |category| %&amp;gt;&lt;br /&gt;
  &amp;lt;tr&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= category.name %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Show', category %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Edit', edit_category_path(category) %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Destroy', category, method: :delete, data: { confirm: 'Are you sure?' } %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%= link_to 'New Category', new_category_path %&amp;gt;&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;% @recipes.each do |recipe| %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
Here @recipes is an instance variable of model recipe defined in index action of the controller ‘recipes’. It contains all the recipes currently present in our application. Then each recipe is taken from this instance variable and is assigned to the local variable recipe.  All the subsequent lines display the recipe details (name, description and instruction).&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%= link_to ‘Show’, recipe %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
link_to is an ruby operator which is used to redirect the control to some other page. ‘Show’ is displayed as it is on the page and is an hyperlink. It links to a page pointed by recipe.  The path is defined in the routes.rb file. &lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
2. _form.html.erb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is a partial. Partials are devices used for breaking the code in smaller, more manageable chunks. This helps in modularity and also maintain DRY principle[4]. With a partial, code for rendering a particular response is moved to its own file. So for e.g. we can use this same partial for creating and editing information, because the information involved is essentially the same.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= form_for(@recipe) do |f| %&amp;gt;&lt;br /&gt;
  &amp;lt;% if @recipe.errors.any? %&amp;gt;&lt;br /&gt;
    &amp;lt;div id=&amp;quot;error_explanation&amp;quot;&amp;gt;&lt;br /&gt;
      &amp;lt;h2&amp;gt;&amp;lt;%= pluralize(@recipe.errors.count, &amp;quot;error&amp;quot;) %&amp;gt; prohibited this recipe from being saved:&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      &amp;lt;ul&amp;gt;&lt;br /&gt;
      &amp;lt;% @recipe.errors.full_messages.each do |msg| %&amp;gt;&lt;br /&gt;
        &amp;lt;li&amp;gt;&amp;lt;%= msg %&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
      &amp;lt;% end %&amp;gt;&lt;br /&gt;
      &amp;lt;/ul&amp;gt;&lt;br /&gt;
    &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;% end %&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :name %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_field :name %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
    &amp;lt;%=  f.label :category %&amp;gt;&lt;br /&gt;
    &amp;lt;%=    select('recipe','category_id', Category.all.collect {|c| [c[:name],c[:id]]}) %&amp;gt;&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :description %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_field :description %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :instructions %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_area :instructions %&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.submit %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;&amp;lt;%= form_for(@recipe) do |f| %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
This statement means to render a form for the instance variable @recipe.&lt;br /&gt;
If this page is being used for creating a new recipe, then the values for each key in the @recipe hash will be empty. If it is used for editing a recipe, @recipe will contain information corresponding to the recipe being edited.&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;% if @recipe.errors.any? %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
While saving an recipe or updating a recipe in the database, some violations may occur. Like the recipe name is left empty or its description is empty and so on. So while saving this information, database will signal some error. The errors are sent back to this page and are caught in @recipe.errors.any? statement.&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%=    select('recipe','category_id', Category.all.collect {|c| [c[:name],c[:id]]}) %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
This statement creates a dropdown box.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;‘recipe’ : refers to the object to which the selected value is to be assigned. In this case , it is the recipe object&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;‘category_id’ : create a ‘category_id’ key in the recipe hash.&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;Category.all.collect {|c| [c[:name],c[:id]]} : recover all categories from the database and list them  in the dropdown box. The value for each name is c[:id]. Whenever a category is selected by the user, its corresponding id is assigned to the ‘category_id’ key in the ‘recipe’ object.&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
3. new.html.erb&amp;lt;br&amp;gt;&lt;br /&gt;
This page is used to create a new recipe. This page simply renders the partial described above.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;New recipe&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;%= render 'form' %&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%= render ‘form’ %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
This statement renders the partial.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
4. edit.html.erb&lt;br /&gt;
This page is used to edit a existing recipe. This page also renders the partial.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Editing recipe&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;%= render 'form' %&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Show', @recipe %&amp;gt; |&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
5. show.html.erb&lt;br /&gt;
This page is used to show a particular recipe requested by the user. The recipe requested is captured by the controller in the @recipe instance variable and is passed to this view.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;p id=&amp;quot;notice&amp;quot;&amp;gt;&amp;lt;%= notice %&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Name:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.name %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Description:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.description %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Instructions:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.instructions %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Edit', edit_recipe_path(@recipe) %&amp;gt; |&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;The Category Views are very similar to the recipe Views and can be created similarly.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Conclusion&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
MVC was first proposed by Trygve Reenskaug in 1979. He broke applications into three types of components: models, which is responsible for maintaining the state of the application; views, which are responsible for generating a user interface; and controllers, which orchestrate the applications by receiving events from outside world, interact with model, and display an appropriate view to user. This separation of concerns led to far less computing, and in turn made the code easier to write and maintain.&amp;lt;br&amp;gt;&lt;br /&gt;
Using Ruby on Rails, one can implement MVC to build various types of web applications. The Cookbook is an easy starter. Readers can learn the basics of Ruby on Rails from this page.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;References&amp;lt;/h1&amp;gt;&lt;br /&gt;
*[1] http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller&lt;br /&gt;
*[2] http://en.wikipedia.org/wiki/Ruby_on_Rails &lt;br /&gt;
*[3] Sam Ruby, Dave Thomas, David Heinemeier Hansson.agile web development with rails (fourth edition)&lt;br /&gt;
*[4] http://en.wikipedia.org/wiki/Don't_repeat_yourself&lt;br /&gt;
*[5] http://guides.rubyonrails.org/getting_started.html&lt;br /&gt;
*[6] http://api.rubyonrails.org/classes/ActiveRecord/Migration.html&lt;br /&gt;
*[7] http://en.wikipedia.org/wiki/Metaprogramming&lt;br /&gt;
*[8] http://net.tutsplus.com/tutorials/other/mvc-for-noobs/&lt;br /&gt;
*[9] Hotzner, S. (2007). Beginnning ruby on rail, Indianapolis: Wiley Publishing Inc&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The old wiki can be found here http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch4_4d_ch.&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w38_nm&amp;diff=67162</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w38 nm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w38_nm&amp;diff=67162"/>
		<updated>2012-10-04T02:08:30Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;Introduction&amp;lt;/h1&amp;gt;&lt;br /&gt;
This page is aimed at asserting the importance of Model View Controller Architecture and how MVC is implemented in Cookbook application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Model View Controller (MVC) Architecture&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The purpose of creating MVC architecture is to have clear separation between the responsibilities of business logic and representation. In a nutshell the responsibilities are mentioned as below:&lt;br /&gt;
&lt;br /&gt;
[[File:MVC.png|frame|none|MVC Responsibilities[8]]]&lt;br /&gt;
&lt;br /&gt;
Model-View-Controller (MVC)[1] is an architectural pattern used in software engineering.In complex computer applications that present a large amount of data to the user, a developer often wishes to separate data (model) and user interface (view) concerns, so that changes to the user interface will not affect data handling, and that the data can be reorganized without changing the user interface.&amp;lt;br&amp;gt;&lt;br /&gt;
The model-view-controller solves this problem by decoupling data access and business logic from data presentation and user interaction, by introducing an intermediate component: the controller. Having described the responsibilities of three components let’s take a look how these components interact with each other.&lt;br /&gt;
&lt;br /&gt;
[[File:MVC_architecture.PNG|frame|none|MVC Architecture[9]]] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Ruby on Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby on Rails[2] is an MVC framework. Rails enforces a structure for application, it helps to knit the models, views, and controllers as separate chunks of functionality while the program executes. No more external configuration needed. Rails is packed with features that make you more productive, with many of the following features building on one other.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Metaprogramming[7]:&amp;lt;/b&amp;gt; Other frameworks use extensive code generation from scratch. Metaprogramming techniques use programs to write programs. Ruby is one of the best languages for metaprogramming, and Rails uses this capability well. Rails also uses code generation but relies much more on metaprogramming for the heavy lifting.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Active Record[6]:&amp;lt;/b&amp;gt; Rails introduces the Active Record framework, which saves objects to the database. The Rails version of Active Record discovers the columns in a database schema and automatically attaches them to your domain objects using metaprogramming.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Convention over configuration:&amp;lt;/b&amp;gt; Most web development frameworks for .NET or Java force you to write pages of configuration code. If you follow suggested naming conventions, Rails doesn't need much configuration.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Scaffolding:&amp;lt;/b&amp;gt; You often create temporary code in the early stages of development to help get an application up quickly and see how major components work together. Rails automatically creates much of the scaffolding you'll need.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Built-in testing:&amp;lt;/b&amp;gt; Rails creates simple automated tests which you can extend. Rails also provides supporting code called harnesses and fixtures that make test cases easier to write and run. Ruby can then execute all your automated tests with the rake utility.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Three environments:&amp;lt;/b&amp;gt; Rails gives you three default environments: development, testing, and production. Each behaves slightly differently, making your entire software development cycle easier. For example, Rails creates a fresh copy of the Test database for each test run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Cookbook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
Cookbook Application is a very basic Ruby on Rails application. It is a great way to understand the nuances that ‘Ruby’ has to offer. It is built keeping in mind that the reader is new to the world of ruby and is developing his/her first Ruby on Rails application.&lt;br /&gt;
The motive of this wiki page is to make the reader understand how the Cookbook application was built. And on the way to building this application, user will be introduced to some key concepts like Model, Views and Controllers. At the end of this page, reader must fairly be ready to start building his/her basic Ruby on Rails application.&lt;br /&gt;
&lt;br /&gt;
In the Cookbook application we are considering a Cookbook which contains recipes for various dishes. The recipes have been divided into different categories, for example there is a recipe for ice tea which belongs to the category of beverages. There is a relationship which is created between a recipe and a category which can be expressed as an ice tea belongs to beverages. We can have many such recipes belonging to a single category, in this case many to one relationship is established. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;A simple application to start -- Cookbook application&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Start a new project. While creating a new project, remember to select Rails Application option as follows&lt;br /&gt;
&lt;br /&gt;
[[File:Start_application.png|frame|none|Start a new Application]] &lt;br /&gt;
&lt;br /&gt;
When we create a Rails application, it automatically creates a bunch of files and folders as follows:&lt;br /&gt;
&lt;br /&gt;
[[File:Listing_of_cookbook_application.png|frame|none|Listing_of_cookbook_application]] &lt;br /&gt;
&lt;br /&gt;
We mainly use the app directory. This is where the heart of the application is stored i.e. Models, Views and controllers.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Using scaffold to create framework&amp;lt;/h2&amp;gt;&lt;br /&gt;
Rails scaffolding is a quick way to generate some of the major pieces of an application. If you want to create the models, views, and controllers for a new resource in a single operation, scaffolding is the tool for the job.&amp;lt;br&amp;gt;&lt;br /&gt;
In Rubymine, we can create a scaffold by right clicking on the current Project-&amp;gt;New-&amp;gt;Run Rails Generator -&amp;gt;scaffold. This will open a dialog box to create a scaffold where we can enter the name and the attributes along with their data type. Once the scaffold has been created RubyMine will generate the model, view and controller files respectively. These files do not contain any specific methods but they provide a framework which is useful for general applications. The user can modify the MVC behavior according to his/her application requirements. Each scaffold has create, read, update and delete methods declared in the controller class. A user can modify the behavior of the controller through these methods. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Create_Scaffold.png|frame|none|Creating a new scaffold]]&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Scaffold.png|frame|left|Scaffold]] [[File:New_Scaffold_Categories.png|frame|center|Creating scaffold for categories]]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Working without scaffold&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or you don't want to create a fully standard framework with scaffold, then you can use RubyMine generate command to create model, controller and view individually. There are additional commands included in the generate command to create migration, assets, intergration_test and performance_test. &lt;br /&gt;
[[File:Generating_controller.png|frame|none|Generating Controller]] &lt;br /&gt;
[[File:Generating_controller_recipes.png|frame|none|Generating Recipes controller]]&lt;br /&gt;
&lt;br /&gt;
By clicking on the 'Add' button, we can add the required actions in the controller&lt;br /&gt;
Similarly models and views can be created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Controllers for Cookbook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
The Cookbook application starts by entering a URL in the browser which sends the request to the web server. Since our web server supports Rails application, it will hand over the control to a Rails controller.&lt;br /&gt;
A controller ‘controls’ the entire application by responding to the incoming requests as needed. For example, consider the following URL http ://localhost:3000/categories/show which is decoded by the controller to know that its action named show has been invoked.&lt;br /&gt;
All the controllers inherit only the ApplicationController which in turn inherits the ActionController::Base class. ActionContoller module provides the basic support for the controllers in Rails.&lt;br /&gt;
In our Cookbook application we generate Categories controller using the scaffold mechanism which populates the controller with the basic methods such as index, create, show, new, edit, update and destroy.&lt;br /&gt;
A simple example of index method is as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    @categories = Category.all&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # index.html.erb&lt;br /&gt;
      format.json { render json: @categories }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In ruby, @ operator is used to declare instance variables. The index method makes use of @categories instance variable which is populated with all the records in the Category table. The respond_to method is used for responding to particular type of request. It contains a block and a variable |format| which determines whether the incoming request should be responded with HTML (when we are interacting with a user) or JSON format (if we are returning an object). &lt;br /&gt;
After executing the code, the controller will render a template of the same name. (For this example, the template is views/categories/index.html.erb).&lt;br /&gt;
Similarly the show method is defined as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def show&lt;br /&gt;
    @category = Category.find(params[:id])&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # show.html.erb&lt;br /&gt;
      format.json { render json: @category }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The show method is used to display a particular object. The show method needs to access database to search for the given object. This is done using the find method which takes params[:id] as argument. The params[:id] contains the id or primary key of the object which is passed as a parameter to look up in the database and the result is stored in the instance variable. The result is displayed by rendering the show.html.erb file.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Active Record&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, a program deals with objects which are mapped into database relational databases. This kind of mapping is facilitated by the Active Record.&amp;lt;br&amp;gt;Active Record maps the Rails classes in to database tables and rails objects into database records.&lt;br /&gt;
This makes it easy to perform actions on database tables by invoking the class methods. Another benefit of Active Record is database migration. Migrations can manage the evolution of a schema used by several physical databases. It’s a solution to the common problem of adding a field to make a new feature work in your local database, but being unsure of how to push that change to other developers and to the production server. With migrations, you can describe the transformations in self-contained classes that can be checked into version control systems and executed against another database that might be one, two, or five versions behind.You can find a lot more details about rails migration here[8].&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Method Pairs&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;There are couple of method pairs such as new-create and edit-update which are executed in sequence. In method pair one method is responsible for display preparation and the other method validates the data and attempts it to save in the database.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def new&lt;br /&gt;
    @category = Category.new&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # new.html.erb&lt;br /&gt;
      format.json { render json: @category }&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def create&lt;br /&gt;
    @category = Category.new(params[:category])&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      if @category.save&lt;br /&gt;
        format.html { redirect_to @category, notice: 'Category was successfully created.' }&lt;br /&gt;
        format.json { render json: @category, status: :created, location: @category }&lt;br /&gt;
      else&lt;br /&gt;
        format.html { render action: &amp;quot;new&amp;quot; }&lt;br /&gt;
        format.json { render json: @category.errors, status: :unprocessable_entity }&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The new and create methods are known as method pairs which execute in sequence. The new method is used to instantiate a new Category object whereas create method is used to successfully save the object to the database. After execution of the new method, the controller renders the new.html.erb file which displays a form to create a new Category object. The user enters the required information in the form and the object id is returned in the URL on submitting the form. Consider the following URL is generated on submitting the form: http: //localhost:3000/categories/3.&lt;br /&gt;
This means that Category object with id =3 will be created. The details of the object will be processed by the create method. &lt;br /&gt;
The controller invokes the create method to store the object in the database. An observation can be made about the @category variable in the create method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@category = Category.new(params[:category])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The parameters passed in the URL after creating a new object is stored in the params hash (denoted by params[]). In this case the params[] contains an object with category_id = 3. The params[] assigns the data of object with category_id =3 to the instance variable. The data entered in the instance variable is validated using the @category.save method. If no errors are found then the object stored in the database and a message on successful creation of the object is displayed to the user. However, if the data entered in the object contains an error, the user is redirected to the new.html.erb page to resubmit the information and the errors are displayed. &lt;br /&gt;
Another example of such method pair is the edit and update methods. The edit method displays the table entry for an existing object, while the update method is invoked when the changes have been submitted.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Models of Cookbook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
Model handles the data processing in a web application. Actions interact with the model to manipulate the data. Rails is capable of handling databases and this is facilitated by the model. You can base models on the Rails ActiveRecord module which contains support databases.&lt;br /&gt;
In our Cookbook application we have two models namely: Category and Recipe. They are represented by files category.rb and recipe.rb and each file is used to describe the relation between two classes. Let’s take a look at the two models&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt; category.rb&amp;lt;/b&amp;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;
&amp;lt;b&amp;gt;recipe.rb&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;Relationships in models&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;one-to-one&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Syllabus &amp;lt; ActiveRecord::Base&lt;br /&gt;
	belongs_to :course¬&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Course &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_one :syllabus&lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;many-many&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Student &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_and_belongs_to_many :courses&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Course &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_and_belongs_to_many :students&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Cookbook, we have many-one relationship between recipes and categories.&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Views of Cookbook application&amp;lt;/h1&amp;gt;&lt;br /&gt;
What is V in MVC? ‘V’ refers to views. Views are the actual interface that users can see. It is through views, that a user may request some data to be viewed or submits some data for processing.&amp;lt;br&amp;gt;&lt;br /&gt;
Views in rails are of the form file_name.html.erb. Here erb means embedded ruby. What html.erb denotes is some ruby code is embedded in HTML code. Like HTML, embedded ruby also uses tags to represent data. However, to distinguish it from the HTML code, ruby is embedded between &amp;lt;% %&amp;gt; if some non-assignment ruby operation is to be performed. If something needs to be displayed on screen, &amp;lt;%= %&amp;gt; is used.&lt;br /&gt;
Views are present in “app/views” folder in the rails application.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Name of the view files is same as the name of their respective controller. For e.g.:- if I have a controller movie with an action new, the corresponding view page will be new.html.erb in the app/views/movie directory.&amp;lt;br&amp;gt;&lt;br /&gt;
The Cookbook application views are as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Recipe Controller Views&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
1. index.html.erb&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;nowiki&amp;gt;&amp;lt;h1&amp;gt;Listing categories&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
  &amp;lt;tr&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;Name&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;% @categories.each do |category| %&amp;gt;&lt;br /&gt;
  &amp;lt;tr&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= category.name %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Show', category %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Edit', edit_category_path(category) %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Destroy', category, method: :delete, data: { confirm: 'Are you sure?' } %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%= link_to 'New Category', new_category_path %&amp;gt;&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;% @recipes.each do |recipe| %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
Here @recipes is an instance variable of model recipe defined in index action of the controller ‘recipes’. It contains all the recipes currently present in our application. Then each recipe is taken from this instance variable and is assigned to the local variable recipe.  All the subsequent lines display the recipe details (name, description and instruction).&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%= link_to ‘Show’, recipe %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
link_to is an ruby operator which is used to redirect the control to some other page. ‘Show’ is displayed as it is on the page and is an hyperlink. It links to a page pointed by recipe.  The path is defined in the routes.rb file. &lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
2. _form.html.erb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is a partial. Partials are devices used for breaking the code in smaller, more manageable chunks. This helps in modularity and also maintain DRY principle[4]. With a partial, code for rendering a particular response is moved to its own file. So for e.g. we can use this same partial for creating and editing information, because the information involved is essentially the same.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= form_for(@recipe) do |f| %&amp;gt;&lt;br /&gt;
  &amp;lt;% if @recipe.errors.any? %&amp;gt;&lt;br /&gt;
    &amp;lt;div id=&amp;quot;error_explanation&amp;quot;&amp;gt;&lt;br /&gt;
      &amp;lt;h2&amp;gt;&amp;lt;%= pluralize(@recipe.errors.count, &amp;quot;error&amp;quot;) %&amp;gt; prohibited this recipe from being saved:&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      &amp;lt;ul&amp;gt;&lt;br /&gt;
      &amp;lt;% @recipe.errors.full_messages.each do |msg| %&amp;gt;&lt;br /&gt;
        &amp;lt;li&amp;gt;&amp;lt;%= msg %&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
      &amp;lt;% end %&amp;gt;&lt;br /&gt;
      &amp;lt;/ul&amp;gt;&lt;br /&gt;
    &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;% end %&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :name %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_field :name %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
    &amp;lt;%=  f.label :category %&amp;gt;&lt;br /&gt;
    &amp;lt;%=    select('recipe','category_id', Category.all.collect {|c| [c[:name],c[:id]]}) %&amp;gt;&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :description %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_field :description %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :instructions %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_area :instructions %&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.submit %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;&amp;lt;%= form_for(@recipe) do |f| %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
This statement means to render a form for the instance variable @recipe.&lt;br /&gt;
If this page is being used for creating a new recipe, then the values for each key in the @recipe hash will be empty. If it is used for editing a recipe, @recipe will contain information corresponding to the recipe being edited.&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;% if @recipe.errors.any? %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
While saving an recipe or updating a recipe in the database, some violations may occur. Like the recipe name is left empty or its description is empty and so on. So while saving this information, database will signal some error. The errors are sent back to this page and are caught in @recipe.errors.any? statement.&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%=    select('recipe','category_id', Category.all.collect {|c| [c[:name],c[:id]]}) %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
This statement creates a dropdown box.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;‘recipe’ : refers to the object to which the selected value is to be assigned. In this case , it is the recipe object&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;‘category_id’ : create a ‘category_id’ key in the recipe hash.&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;Category.all.collect {|c| [c[:name],c[:id]]} : recover all categories from the database and list them  in the dropdown box. The value for each name is c[:id]. Whenever a category is selected by the user, its corresponding id is assigned to the ‘category_id’ key in the ‘recipe’ object.&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
3. new.html.erb&amp;lt;br&amp;gt;&lt;br /&gt;
This page is used to create a new recipe. This page simply renders the partial described above.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;New recipe&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;%= render 'form' %&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%= render ‘form’ %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
This statement renders the partial.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
4. edit.html.erb&lt;br /&gt;
This page is used to edit a existing recipe. This page also renders the partial.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Editing recipe&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;%= render 'form' %&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Show', @recipe %&amp;gt; |&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
5. show.html.erb&lt;br /&gt;
This page is used to show a particular recipe requested by the user. The recipe requested is captured by the controller in the @recipe instance variable and is passed to this view.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;p id=&amp;quot;notice&amp;quot;&amp;gt;&amp;lt;%= notice %&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Name:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.name %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Description:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.description %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Instructions:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.instructions %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Edit', edit_recipe_path(@recipe) %&amp;gt; |&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;The Category Views are very similar to the recipe Views and can be created similarly.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Conclusion&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
MVC was first proposed by Trygve Reenskaug in 1979. He broke applications into three types of components: models, which is responsible for maintaining the state of the application; views, which are responsible for generating a user interface; and controllers, which orchestrate the applications by receiving events from outside world, interact with model, and display an appropriate view to user. This separation of concerns led to far less computing, and in turn made the code easier to write and maintain.&amp;lt;br&amp;gt;&lt;br /&gt;
Use Ruby on Rails can implement MVC to build various types of web applications. The Cookbook is an easy starter. Readers can learn the basics of Ruby on Rails from this page.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;References&amp;lt;/h1&amp;gt;&lt;br /&gt;
*[1] http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller&lt;br /&gt;
*[2] http://en.wikipedia.org/wiki/Ruby_on_Rails &lt;br /&gt;
*[3] Sam Ruby, Dave Thomas, David Heinemeier Hansson.agile web development with rails (fourth edition)&lt;br /&gt;
*[4] http://en.wikipedia.org/wiki/Don't_repeat_yourself&lt;br /&gt;
*[5] http://guides.rubyonrails.org/getting_started.html&lt;br /&gt;
*[6] http://api.rubyonrails.org/classes/ActiveRecord/Migration.html&lt;br /&gt;
*[7] http://en.wikipedia.org/wiki/Metaprogramming&lt;br /&gt;
*[8] http://net.tutsplus.com/tutorials/other/mvc-for-noobs/&lt;br /&gt;
*[9] Hotzner, S. (2007). Beginnning ruby on rail, Indianapolis: Wiley Publishing Inc&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The old wiki can be found here http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch4_4d_ch.&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Generating_controller_recipes.png&amp;diff=67149</id>
		<title>File:Generating controller recipes.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Generating_controller_recipes.png&amp;diff=67149"/>
		<updated>2012-10-04T02:04:43Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Generating_controller.png&amp;diff=67146</id>
		<title>File:Generating controller.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Generating_controller.png&amp;diff=67146"/>
		<updated>2012-10-04T02:04:01Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: uploaded a new version of &amp;amp;quot;File:Generating controller.png&amp;amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Generating_controller.png&amp;diff=67140</id>
		<title>File:Generating controller.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Generating_controller.png&amp;diff=67140"/>
		<updated>2012-10-04T02:02:07Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w38_nm&amp;diff=67138</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w38 nm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w38_nm&amp;diff=67138"/>
		<updated>2012-10-04T02:01:46Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;Introduction&amp;lt;/h1&amp;gt;&lt;br /&gt;
This page is aimed at asserting the importance of Model View Controller Architecture and how MVC is implemented in Cookbook application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Model View Controller (MVC) Architecture&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The purpose of creating MVC architecture is to have clear separation between the responsibilities of business logic and representation. In a nutshell the responsibilities are mentioned as below:&lt;br /&gt;
&lt;br /&gt;
[[File:MVC.png|frame|none|MVC Responsibilities[8]]]&lt;br /&gt;
&lt;br /&gt;
Model-View-Controller (MVC)[1] is an architectural pattern used in software engineering.In complex computer applications that present a large amount of data to the user, a developer often wishes to separate data (model) and user interface (view) concerns, so that changes to the user interface will not affect data handling, and that the data can be reorganized without changing the user interface.&amp;lt;br&amp;gt;&lt;br /&gt;
The model-view-controller solves this problem by decoupling data access and business logic from data presentation and user interaction, by introducing an intermediate component: the controller. Having described the responsibilities of three components let’s take a look how these components interact with each other.&lt;br /&gt;
&lt;br /&gt;
[[File:MVC_architecture.PNG|frame|none|MVC Architecture[9]]] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Ruby on Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby on Rails[2] is an MVC framework. Rails enforces a structure for application, it helps to knit the models, views, and controllers as separate chunks of functionality while the program executes. No more external configuration needed. Rails is packed with features that make you more productive, with many of the following features building on one other.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Metaprogramming[7]:&amp;lt;/b&amp;gt; Other frameworks use extensive code generation from scratch. Metaprogramming techniques use programs to write programs. Ruby is one of the best languages for metaprogramming, and Rails uses this capability well. Rails also uses code generation but relies much more on metaprogramming for the heavy lifting.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Active Record[6]:&amp;lt;/b&amp;gt; Rails introduces the Active Record framework, which saves objects to the database. The Rails version of Active Record discovers the columns in a database schema and automatically attaches them to your domain objects using metaprogramming.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Convention over configuration:&amp;lt;/b&amp;gt; Most web development frameworks for .NET or Java force you to write pages of configuration code. If you follow suggested naming conventions, Rails doesn't need much configuration.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Scaffolding:&amp;lt;/b&amp;gt; You often create temporary code in the early stages of development to help get an application up quickly and see how major components work together. Rails automatically creates much of the scaffolding you'll need.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Built-in testing:&amp;lt;/b&amp;gt; Rails creates simple automated tests which you can extend. Rails also provides supporting code called harnesses and fixtures that make test cases easier to write and run. Ruby can then execute all your automated tests with the rake utility.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Three environments:&amp;lt;/b&amp;gt; Rails gives you three default environments: development, testing, and production. Each behaves slightly differently, making your entire software development cycle easier. For example, Rails creates a fresh copy of the Test database for each test run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Cookbook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
Cookbook Application is a very basic Ruby on Rails application. It is a great way to understand the nuances that ‘Ruby’ has to offer. It is built keeping in mind that the reader is new to the world of ruby and is developing his/her first Ruby on Rails application.&lt;br /&gt;
The motive of this wiki page is to make the reader understand how the Cookbook application was built. And on the way to building this application, user will be introduced to some key concepts like Model, Views and Controllers. At the end of this page, reader must fairly be ready to start building his/her basic Ruby on Rails application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;A simple application to start -- Cookbook application&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Start a new project. While creating a new project, remember to select Rails Application option as follows&lt;br /&gt;
&lt;br /&gt;
[[File:Start_application.png|frame|none|Start a new Application]] &lt;br /&gt;
&lt;br /&gt;
When we create a Rails application, it automatically creates a bunch of files and folders as follows:&lt;br /&gt;
&lt;br /&gt;
[[File:Listing_of_cookbook_application.png|frame|none|Listing_of_cookbook_application]] &lt;br /&gt;
&lt;br /&gt;
We mainly use the app directory. This is where the heart of the application is stored i.e. Models, Views and controllers.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Using scaffold to create framework&amp;lt;/h2&amp;gt;&lt;br /&gt;
Rails scaffolding is a quick way to generate some of the major pieces of an application. If you want to create the models, views, and controllers for a new resource in a single operation, scaffolding is the tool for the job.&amp;lt;br&amp;gt;&lt;br /&gt;
In Rubymine, we can create a scaffold by right clicking on the current Project-&amp;gt;New-&amp;gt;Run Rails Generator -&amp;gt;scaffold. This will open a dialog box to create a scaffold where we can enter the name and the attributes along with their data type. Once the scaffold has been created RubyMine will generate the model, view and controller files respectively. These files do not contain any specific methods but they provide a framework which is useful for general applications. The user can modify the MVC behavior according to his/her application requirements. Each scaffold has create, read, update and delete methods declared in the controller class. A user can modify the behavior of the controller through these methods. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Create_Scaffold.png|frame|none|Creating a new scaffold]]&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Scaffold.png|frame|left|Scaffold]] [[File:New_Scaffold_Categories.png|frame|center|Creating scaffold for categories]]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Working without scaffold&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or you don't want to create a fully standard framework with scaffold, then you can use RubyMine generate command to create model, controller and view individually. There are additional commands included in the generate command to create migration, assets, intergration_test and performance_test. &lt;br /&gt;
&lt;br /&gt;
[[File:Generating_controller.png|frame|left|Generating Controller]] [[File:Generating_controller_recipes.png|frame|left|Generating Recipes controller]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the Cookbook application we are considering a Cookbook which contains recipes for various dishes. The recipes have been divided into different categories, for example there is a recipe for ice tea which belongs to the category of beverages. There is a relationship which is created between a recipe and a category which can be expressed as an ice tea belongs to beverages. We can have many such recipes belonging to a single category, in this case many to one relationship is established. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Controllers for Cookbook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
The Cookbook application starts by entering a URL in the browser which sends the request to the web server. Since our web server supports Rails application, it will hand over the control to a Rails controller.&lt;br /&gt;
A controller ‘controls’ the entire application by responding to the incoming requests as needed. For example, consider the following URL http ://localhost:3000/categories/show which is decoded by the controller to know that its action named show has been invoked.&lt;br /&gt;
All the controllers inherit only the ApplicationController which in turn inherits the ActionController::Base class. ActionContoller module provides the basic support for the controllers in Rails.&lt;br /&gt;
In our Cookbook application we generate Categories controller using the scaffold mechanism which populates the controller with the basic methods such as index, create, show, new, edit, update and destroy.&lt;br /&gt;
A simple example of index method is as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    @categories = Category.all&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # index.html.erb&lt;br /&gt;
      format.json { render json: @categories }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In ruby, @ operator is used to declare instance variables. The index method makes use of @categories instance variable which is populated with all the records in the Category table. The respond_to method is used for responding to particular type of request. It contains a block and a variable |format| which determines whether the incoming request should be responded with HTML (when we are interacting with a user) or JSON format (if we are returning an object). &lt;br /&gt;
After executing the code, the controller will render a template of the same name. (For this example, the template is views/categories/index.html.erb).&lt;br /&gt;
Similarly the show method is defined as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def show&lt;br /&gt;
    @category = Category.find(params[:id])&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # show.html.erb&lt;br /&gt;
      format.json { render json: @category }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The show method is used to display a particular object. The show method needs to access database to search for the given object. This is done using the find method which takes params[:id] as argument. The params[:id] contains the id or primary key of the object which is passed as a parameter to look up in the database and the result is stored in the instance variable. The result is displayed by rendering the show.html.erb file.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Active Record&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, a program deals with objects which are mapped into database relational databases. This kind of mapping is facilitated by the Active Record.&amp;lt;br&amp;gt;Active Record maps the Rails classes in to database tables and rails objects into database records.&lt;br /&gt;
This makes it easy to perform actions on database tables by invoking the class methods. Another benefit of Active Record is database migration. Migrations can manage the evolution of a schema used by several physical databases. It’s a solution to the common problem of adding a field to make a new feature work in your local database, but being unsure of how to push that change to other developers and to the production server. With migrations, you can describe the transformations in self-contained classes that can be checked into version control systems and executed against another database that might be one, two, or five versions behind.You can find a lot more details about rails migration here[8].&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Method Pairs&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;There are couple of method pairs such as new-create and edit-update which are executed in sequence. In method pair one method is responsible for display preparation and the other method validates the data and attempts it to save in the database.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def new&lt;br /&gt;
    @category = Category.new&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # new.html.erb&lt;br /&gt;
      format.json { render json: @category }&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def create&lt;br /&gt;
    @category = Category.new(params[:category])&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      if @category.save&lt;br /&gt;
        format.html { redirect_to @category, notice: 'Category was successfully created.' }&lt;br /&gt;
        format.json { render json: @category, status: :created, location: @category }&lt;br /&gt;
      else&lt;br /&gt;
        format.html { render action: &amp;quot;new&amp;quot; }&lt;br /&gt;
        format.json { render json: @category.errors, status: :unprocessable_entity }&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The new and create methods are known as method pairs which execute in sequence. The new method is used to instantiate a new Category object whereas create method is used to successfully save the object to the database. After execution of the new method, the controller renders the new.html.erb file which displays a form to create a new Category object. The user enters the required information in the form and the object id is returned in the URL on submitting the form. Consider the following URL is generated on submitting the form: http: //localhost:3000/categories/3.&lt;br /&gt;
This means that Category object with id =3 will be created. The details of the object will be processed by the create method. &lt;br /&gt;
The controller invokes the create method to store the object in the database. An observation can be made about the @category variable in the create method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@category = Category.new(params[:category])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The parameters passed in the URL after creating a new object is stored in the params hash (denoted by params[]). In this case the params[] contains an object with category_id = 3. The params[] assigns the data of object with category_id =3 to the instance variable. The data entered in the instance variable is validated using the @category.save method. If no errors are found then the object stored in the database and a message on successful creation of the object is displayed to the user. However, if the data entered in the object contains an error, the user is redirected to the new.html.erb page to resubmit the information and the errors are displayed. &lt;br /&gt;
Another example of such method pair is the edit and update methods. The edit method displays the table entry for an existing object, while the update method is invoked when the changes have been submitted.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Models of Cookbook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
Model handles the data processing in a web application. Actions interact with the model to manipulate the data. Rails is capable of handling databases and this is facilitated by the model. You can base models on the Rails ActiveRecord module which contains support databases.&lt;br /&gt;
In our Cookbook application we have two models namely: Category and Recipe. They are represented by files category.rb and recipe.rb and each file is used to describe the relation between two classes. Let’s take a look at the two models&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt; category.rb&amp;lt;/b&amp;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;
&amp;lt;b&amp;gt;recipe.rb&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;Relationships in models&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;one-to-one&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Syllabus &amp;lt; ActiveRecord::Base&lt;br /&gt;
	belongs_to :course¬&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Course &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_one :syllabus&lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;many-many&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Student &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_and_belongs_to_many :courses&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Course &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_and_belongs_to_many :students&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Cookbook, we have many-one relationship between recipes and categories.&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Views of Cookbook application&amp;lt;/h1&amp;gt;&lt;br /&gt;
What is V in MVC? ‘V’ refers to views. Views are the actual interface that users can see. It is through views, that a user may request some data to be viewed or submits some data for processing.&amp;lt;br&amp;gt;&lt;br /&gt;
Views in rails are of the form file_name.html.erb. Here erb means embedded ruby. What html.erb denotes is some ruby code is embedded in HTML code. Like HTML, embedded ruby also uses tags to represent data. However, to distinguish it from the HTML code, ruby is embedded between &amp;lt;% %&amp;gt; if some non-assignment ruby operation is to be performed. If something needs to be displayed on screen, &amp;lt;%= %&amp;gt; is used.&lt;br /&gt;
Views are present in “app/views” folder in the rails application.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Name of the view files is same as the name of their respective controller. For e.g.:- if I have a controller movie with an action new, the corresponding view page will be new.html.erb in the app/views/movie directory.&amp;lt;br&amp;gt;&lt;br /&gt;
The Cookbook application views are as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Recipe Controller Views&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
1. index.html.erb&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;nowiki&amp;gt;&amp;lt;h1&amp;gt;Listing categories&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
  &amp;lt;tr&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;Name&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;% @categories.each do |category| %&amp;gt;&lt;br /&gt;
  &amp;lt;tr&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= category.name %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Show', category %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Edit', edit_category_path(category) %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Destroy', category, method: :delete, data: { confirm: 'Are you sure?' } %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%= link_to 'New Category', new_category_path %&amp;gt;&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;% @recipes.each do |recipe| %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
Here @recipes is an instance variable of model recipe defined in index action of the controller ‘recipes’. It contains all the recipes currently present in our application. Then each recipe is taken from this instance variable and is assigned to the local variable recipe.  All the subsequent lines display the recipe details (name, description and instruction).&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%= link_to ‘Show’, recipe %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
link_to is an ruby operator which is used to redirect the control to some other page. ‘Show’ is displayed as it is on the page and is an hyperlink. It links to a page pointed by recipe.  The path is defined in the routes.rb file. &lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
2. _form.html.erb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is a partial. Partials are devices used for breaking the code in smaller, more manageable chunks. This helps in modularity and also maintain DRY principle[4]. With a partial, code for rendering a particular response is moved to its own file. So for e.g. we can use this same partial for creating and editing information, because the information involved is essentially the same.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= form_for(@recipe) do |f| %&amp;gt;&lt;br /&gt;
  &amp;lt;% if @recipe.errors.any? %&amp;gt;&lt;br /&gt;
    &amp;lt;div id=&amp;quot;error_explanation&amp;quot;&amp;gt;&lt;br /&gt;
      &amp;lt;h2&amp;gt;&amp;lt;%= pluralize(@recipe.errors.count, &amp;quot;error&amp;quot;) %&amp;gt; prohibited this recipe from being saved:&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      &amp;lt;ul&amp;gt;&lt;br /&gt;
      &amp;lt;% @recipe.errors.full_messages.each do |msg| %&amp;gt;&lt;br /&gt;
        &amp;lt;li&amp;gt;&amp;lt;%= msg %&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
      &amp;lt;% end %&amp;gt;&lt;br /&gt;
      &amp;lt;/ul&amp;gt;&lt;br /&gt;
    &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;% end %&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :name %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_field :name %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
    &amp;lt;%=  f.label :category %&amp;gt;&lt;br /&gt;
    &amp;lt;%=    select('recipe','category_id', Category.all.collect {|c| [c[:name],c[:id]]}) %&amp;gt;&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :description %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_field :description %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :instructions %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_area :instructions %&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.submit %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;&amp;lt;%= form_for(@recipe) do |f| %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
This statement means to render a form for the instance variable @recipe.&lt;br /&gt;
If this page is being used for creating a new recipe, then the values for each key in the @recipe hash will be empty. If it is used for editing a recipe, @recipe will contain information corresponding to the recipe being edited.&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;% if @recipe.errors.any? %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
While saving an recipe or updating a recipe in the database, some violations may occur. Like the recipe name is left empty or its description is empty and so on. So while saving this information, database will signal some error. The errors are sent back to this page and are caught in @recipe.errors.any? statement.&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%=    select('recipe','category_id', Category.all.collect {|c| [c[:name],c[:id]]}) %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
This statement creates a dropdown box.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;‘recipe’ : refers to the object to which the selected value is to be assigned. In this case , it is the recipe object&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;‘category_id’ : create a ‘category_id’ key in the recipe hash.&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;Category.all.collect {|c| [c[:name],c[:id]]} : recover all categories from the database and list them  in the dropdown box. The value for each name is c[:id]. Whenever a category is selected by the user, its corresponding id is assigned to the ‘category_id’ key in the ‘recipe’ object.&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
3. new.html.erb&amp;lt;br&amp;gt;&lt;br /&gt;
This page is used to create a new recipe. This page simply renders the partial described above.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;New recipe&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;%= render 'form' %&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%= render ‘form’ %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
This statement renders the partial.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
4. edit.html.erb&lt;br /&gt;
This page is used to edit a existing recipe. This page also renders the partial.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Editing recipe&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;%= render 'form' %&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Show', @recipe %&amp;gt; |&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
5. show.html.erb&lt;br /&gt;
This page is used to show a particular recipe requested by the user. The recipe requested is captured by the controller in the @recipe instance variable and is passed to this view.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;p id=&amp;quot;notice&amp;quot;&amp;gt;&amp;lt;%= notice %&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Name:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.name %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Description:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.description %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Instructions:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.instructions %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Edit', edit_recipe_path(@recipe) %&amp;gt; |&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;The Category Views are very similar to the recipe Views and can be created similarly.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Conclusion&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
MVC was first proposed by Trygve Reenskaug in 1979. He broke applications into three types of components: models, which is responsible for maintaining the state of the application; views, which are responsible for generating a user interface; and controllers, which orchestrate the applications by receiving events from outside world, interact with model, and display an appropriate view to user. This separation of concerns led to far less computing, and in turn made the code easier to write and maintain.&amp;lt;br&amp;gt;&lt;br /&gt;
Use Ruby on Rails can implement MVC to build various types of web applications. The Cookbook is an easy starter. Readers can learn the basics of Ruby on Rails from this page.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;References&amp;lt;/h1&amp;gt;&lt;br /&gt;
*[1] http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller&lt;br /&gt;
*[2] http://en.wikipedia.org/wiki/Ruby_on_Rails &lt;br /&gt;
*[3] Sam Ruby, Dave Thomas, David Heinemeier Hansson.agile web development with rails (fourth edition)&lt;br /&gt;
*[4] http://en.wikipedia.org/wiki/Don't_repeat_yourself&lt;br /&gt;
*[5] http://guides.rubyonrails.org/getting_started.html&lt;br /&gt;
*[6] http://api.rubyonrails.org/classes/ActiveRecord/Migration.html&lt;br /&gt;
*[7] http://en.wikipedia.org/wiki/Metaprogramming&lt;br /&gt;
*[8] http://net.tutsplus.com/tutorials/other/mvc-for-noobs/&lt;br /&gt;
*[9] Hotzner, S. (2007). Beginnning ruby on rail, Indianapolis: Wiley Publishing Inc&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The old wiki can be found here http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch4_4d_ch.&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w38_nm&amp;diff=67129</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w38 nm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w38_nm&amp;diff=67129"/>
		<updated>2012-10-04T01:56:32Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;Introduction&amp;lt;/h1&amp;gt;&lt;br /&gt;
This page is aimed at asserting the importance of Model View Controller Architecture and how MVC is implemented in Cookbook application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Model View Controller (MVC) Architecture&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The purpose of creating MVC architecture is to have clear separation between the responsibilities of business logic and representation. In a nutshell the responsibilities are mentioned as below:&lt;br /&gt;
&lt;br /&gt;
[[File:MVC.png|frame|none|MVC Responsibilities[8]]]&lt;br /&gt;
&lt;br /&gt;
Model-View-Controller (MVC)[1] is an architectural pattern used in software engineering.In complex computer applications that present a large amount of data to the user, a developer often wishes to separate data (model) and user interface (view) concerns, so that changes to the user interface will not affect data handling, and that the data can be reorganized without changing the user interface.&amp;lt;br&amp;gt;&lt;br /&gt;
The model-view-controller solves this problem by decoupling data access and business logic from data presentation and user interaction, by introducing an intermediate component: the controller. Having described the responsibilities of three components let’s take a look how these components interact with each other.&lt;br /&gt;
&lt;br /&gt;
[[File:MVC_architecture.PNG|frame|none|MVC Architecture[9]]] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Ruby on Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby on Rails[2] is an MVC framework. Rails enforces a structure for application, it helps to knit the models, views, and controllers as separate chunks of functionality while the program executes. No more external configuration needed. Rails is packed with features that make you more productive, with many of the following features building on one other.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Metaprogramming[7]:&amp;lt;/b&amp;gt; Other frameworks use extensive code generation from scratch. Metaprogramming techniques use programs to write programs. Ruby is one of the best languages for metaprogramming, and Rails uses this capability well. Rails also uses code generation but relies much more on metaprogramming for the heavy lifting.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Active Record[6]:&amp;lt;/b&amp;gt; Rails introduces the Active Record framework, which saves objects to the database. The Rails version of Active Record discovers the columns in a database schema and automatically attaches them to your domain objects using metaprogramming.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Convention over configuration:&amp;lt;/b&amp;gt; Most web development frameworks for .NET or Java force you to write pages of configuration code. If you follow suggested naming conventions, Rails doesn't need much configuration.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Scaffolding:&amp;lt;/b&amp;gt; You often create temporary code in the early stages of development to help get an application up quickly and see how major components work together. Rails automatically creates much of the scaffolding you'll need.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Built-in testing:&amp;lt;/b&amp;gt; Rails creates simple automated tests which you can extend. Rails also provides supporting code called harnesses and fixtures that make test cases easier to write and run. Ruby can then execute all your automated tests with the rake utility.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Three environments:&amp;lt;/b&amp;gt; Rails gives you three default environments: development, testing, and production. Each behaves slightly differently, making your entire software development cycle easier. For example, Rails creates a fresh copy of the Test database for each test run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Cookbook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
Cookbook Application is a very basic Ruby on Rails application. It is a great way to understand the nuances that ‘Ruby’ has to offer. It is built keeping in mind that the reader is new to the world of ruby and is developing his/her first Ruby on Rails application.&lt;br /&gt;
The motive of this wiki page is to make the reader understand how the Cookbook application was built. And on the way to building this application, user will be introduced to some key concepts like Model, Views and Controllers. At the end of this page, reader must fairly be ready to start building his/her basic Ruby on Rails application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;A simple application to start -- Cookbook application&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Start a new project. While creating a new project, remember to select Rails Application option as follows&lt;br /&gt;
&lt;br /&gt;
[[File:Start_application.png|frame|none|Start a new Application]] &lt;br /&gt;
&lt;br /&gt;
When we create a Rails application, it automatically creates a bunch of files and folders as follows:&lt;br /&gt;
&lt;br /&gt;
[[File:Listing_of_cookbook_application.png|frame|none|Listing_of_cookbook_application]] &lt;br /&gt;
&lt;br /&gt;
We mainly use the app directory. This is where the heart of the application is stored i.e. Models, Views and controllers.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Using scaffold to create framework&amp;lt;/h2&amp;gt;&lt;br /&gt;
Rails scaffolding is a quick way to generate some of the major pieces of an application. If you want to create the models, views, and controllers for a new resource in a single operation, scaffolding is the tool for the job.&amp;lt;br&amp;gt;&lt;br /&gt;
In Rubymine, we can create a scaffold by right clicking on the current Project-&amp;gt;New-&amp;gt;Run Rails Generator -&amp;gt;scaffold. This will open a dialog box to create a scaffold where we can enter the name and the attributes along with their data type. Once the scaffold has been created RubyMine will generate the model, view and controller files respectively. These files do not contain any specific methods but they provide a framework which is useful for general applications. The user can modify the MVC behavior according to his/her application requirements. Each scaffold has create, read, update and delete methods declared in the controller class. A user can modify the behavior of the controller through these methods. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Create_Scaffold.png|frame|none|Creating a new scaffold]]&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Scaffold.png|frame|left|Scaffold]] [[File:New_Scaffold_Categories.png|frame|center|Creating scaffold for categories]]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Working without scaffold&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or you don't want to create a fully standard framework with scaffold, then you can use RubyMine generate command to create model, controller and view individually. There are additional commands included in the generate command to create migration, assets, intergration_test and performance_test. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the Cookbook application we are considering a Cookbook which contains recipes for various dishes. The recipes have been divided into different categories, for example there is a recipe for ice tea which belongs to the category of beverages. There is a relationship which is created between a recipe and a category which can be expressed as an ice tea belongs to beverages. We can have many such recipes belonging to a single category, in this case many to one relationship is established. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Controllers for Cookbook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
The Cookbook application starts by entering a URL in the browser which sends the request to the web server. Since our web server supports Rails application, it will hand over the control to a Rails controller.&lt;br /&gt;
A controller ‘controls’ the entire application by responding to the incoming requests as needed. For example, consider the following URL http ://localhost:3000/categories/show which is decoded by the controller to know that its action named show has been invoked.&lt;br /&gt;
All the controllers inherit only the ApplicationController which in turn inherits the ActionController::Base class. ActionContoller module provides the basic support for the controllers in Rails.&lt;br /&gt;
In our Cookbook application we generate Categories controller using the scaffold mechanism which populates the controller with the basic methods such as index, create, show, new, edit, update and destroy.&lt;br /&gt;
A simple example of index method is as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    @categories = Category.all&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # index.html.erb&lt;br /&gt;
      format.json { render json: @categories }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In ruby, @ operator is used to declare instance variables. The index method makes use of @categories instance variable which is populated with all the records in the Category table. The respond_to method is used for responding to particular type of request. It contains a block and a variable |format| which determines whether the incoming request should be responded with HTML (when we are interacting with a user) or JSON format (if we are returning an object). &lt;br /&gt;
After executing the code, the controller will render a template of the same name. (For this example, the template is views/categories/index.html.erb).&lt;br /&gt;
Similarly the show method is defined as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def show&lt;br /&gt;
    @category = Category.find(params[:id])&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # show.html.erb&lt;br /&gt;
      format.json { render json: @category }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The show method is used to display a particular object. The show method needs to access database to search for the given object. This is done using the find method which takes params[:id] as argument. The params[:id] contains the id or primary key of the object which is passed as a parameter to look up in the database and the result is stored in the instance variable. The result is displayed by rendering the show.html.erb file.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Active Record&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, a program deals with objects which are mapped into database relational databases. This kind of mapping is facilitated by the Active Record.&amp;lt;br&amp;gt;Active Record maps the Rails classes in to database tables and rails objects into database records.&lt;br /&gt;
This makes it easy to perform actions on database tables by invoking the class methods. Another benefit of Active Record is database migration. Migrations can manage the evolution of a schema used by several physical databases. It’s a solution to the common problem of adding a field to make a new feature work in your local database, but being unsure of how to push that change to other developers and to the production server. With migrations, you can describe the transformations in self-contained classes that can be checked into version control systems and executed against another database that might be one, two, or five versions behind.You can find a lot more details about rails migration here[8].&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Method Pairs&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;There are couple of method pairs such as new-create and edit-update which are executed in sequence. In method pair one method is responsible for display preparation and the other method validates the data and attempts it to save in the database.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def new&lt;br /&gt;
    @category = Category.new&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # new.html.erb&lt;br /&gt;
      format.json { render json: @category }&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def create&lt;br /&gt;
    @category = Category.new(params[:category])&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      if @category.save&lt;br /&gt;
        format.html { redirect_to @category, notice: 'Category was successfully created.' }&lt;br /&gt;
        format.json { render json: @category, status: :created, location: @category }&lt;br /&gt;
      else&lt;br /&gt;
        format.html { render action: &amp;quot;new&amp;quot; }&lt;br /&gt;
        format.json { render json: @category.errors, status: :unprocessable_entity }&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The new and create methods are known as method pairs which execute in sequence. The new method is used to instantiate a new Category object whereas create method is used to successfully save the object to the database. After execution of the new method, the controller renders the new.html.erb file which displays a form to create a new Category object. The user enters the required information in the form and the object id is returned in the URL on submitting the form. Consider the following URL is generated on submitting the form: http: //localhost:3000/categories/3.&lt;br /&gt;
This means that Category object with id =3 will be created. The details of the object will be processed by the create method. &lt;br /&gt;
The controller invokes the create method to store the object in the database. An observation can be made about the @category variable in the create method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@category = Category.new(params[:category])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The parameters passed in the URL after creating a new object is stored in the params hash (denoted by params[]). In this case the params[] contains an object with category_id = 3. The params[] assigns the data of object with category_id =3 to the instance variable. The data entered in the instance variable is validated using the @category.save method. If no errors are found then the object stored in the database and a message on successful creation of the object is displayed to the user. However, if the data entered in the object contains an error, the user is redirected to the new.html.erb page to resubmit the information and the errors are displayed. &lt;br /&gt;
Another example of such method pair is the edit and update methods. The edit method displays the table entry for an existing object, while the update method is invoked when the changes have been submitted.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Models of Cookbook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
Model handles the data processing in a web application. Actions interact with the model to manipulate the data. Rails is capable of handling databases and this is facilitated by the model. You can base models on the Rails ActiveRecord module which contains support databases.&lt;br /&gt;
In our Cookbook application we have two models namely: Category and Recipe. They are represented by files category.rb and recipe.rb and each file is used to describe the relation between two classes. Let’s take a look at the two models&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt; category.rb&amp;lt;/b&amp;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;
&amp;lt;b&amp;gt;recipe.rb&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;Relationships in models&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;one-to-one&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Syllabus &amp;lt; ActiveRecord::Base&lt;br /&gt;
	belongs_to :course¬&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Course &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_one :syllabus&lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;many-many&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Student &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_and_belongs_to_many :courses&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Course &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_and_belongs_to_many :students&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Cookbook, we have many-one relationship between recipes and categories.&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Views of Cookbook application&amp;lt;/h1&amp;gt;&lt;br /&gt;
What is V in MVC? ‘V’ refers to views. Views are the actual interface that users can see. It is through views, that a user may request some data to be viewed or submits some data for processing.&amp;lt;br&amp;gt;&lt;br /&gt;
Views in rails are of the form file_name.html.erb. Here erb means embedded ruby. What html.erb denotes is some ruby code is embedded in HTML code. Like HTML, embedded ruby also uses tags to represent data. However, to distinguish it from the HTML code, ruby is embedded between &amp;lt;% %&amp;gt; if some non-assignment ruby operation is to be performed. If something needs to be displayed on screen, &amp;lt;%= %&amp;gt; is used.&lt;br /&gt;
Views are present in “app/views” folder in the rails application.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Name of the view files is same as the name of their respective controller. For e.g.:- if I have a controller movie with an action new, the corresponding view page will be new.html.erb in the app/views/movie directory.&amp;lt;br&amp;gt;&lt;br /&gt;
The Cookbook application views are as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Recipe Controller Views&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
1. index.html.erb&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;nowiki&amp;gt;&amp;lt;h1&amp;gt;Listing categories&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
  &amp;lt;tr&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;Name&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;% @categories.each do |category| %&amp;gt;&lt;br /&gt;
  &amp;lt;tr&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= category.name %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Show', category %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Edit', edit_category_path(category) %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Destroy', category, method: :delete, data: { confirm: 'Are you sure?' } %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%= link_to 'New Category', new_category_path %&amp;gt;&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;% @recipes.each do |recipe| %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
Here @recipes is an instance variable of model recipe defined in index action of the controller ‘recipes’. It contains all the recipes currently present in our application. Then each recipe is taken from this instance variable and is assigned to the local variable recipe.  All the subsequent lines display the recipe details (name, description and instruction).&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%= link_to ‘Show’, recipe %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
link_to is an ruby operator which is used to redirect the control to some other page. ‘Show’ is displayed as it is on the page and is an hyperlink. It links to a page pointed by recipe.  The path is defined in the routes.rb file. &lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
2. _form.html.erb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is a partial. Partials are devices used for breaking the code in smaller, more manageable chunks. This helps in modularity and also maintain DRY principle[4]. With a partial, code for rendering a particular response is moved to its own file. So for e.g. we can use this same partial for creating and editing information, because the information involved is essentially the same.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= form_for(@recipe) do |f| %&amp;gt;&lt;br /&gt;
  &amp;lt;% if @recipe.errors.any? %&amp;gt;&lt;br /&gt;
    &amp;lt;div id=&amp;quot;error_explanation&amp;quot;&amp;gt;&lt;br /&gt;
      &amp;lt;h2&amp;gt;&amp;lt;%= pluralize(@recipe.errors.count, &amp;quot;error&amp;quot;) %&amp;gt; prohibited this recipe from being saved:&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      &amp;lt;ul&amp;gt;&lt;br /&gt;
      &amp;lt;% @recipe.errors.full_messages.each do |msg| %&amp;gt;&lt;br /&gt;
        &amp;lt;li&amp;gt;&amp;lt;%= msg %&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
      &amp;lt;% end %&amp;gt;&lt;br /&gt;
      &amp;lt;/ul&amp;gt;&lt;br /&gt;
    &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;% end %&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :name %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_field :name %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
    &amp;lt;%=  f.label :category %&amp;gt;&lt;br /&gt;
    &amp;lt;%=    select('recipe','category_id', Category.all.collect {|c| [c[:name],c[:id]]}) %&amp;gt;&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :description %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_field :description %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :instructions %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_area :instructions %&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.submit %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;&amp;lt;%= form_for(@recipe) do |f| %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
This statement means to render a form for the instance variable @recipe.&lt;br /&gt;
If this page is being used for creating a new recipe, then the values for each key in the @recipe hash will be empty. If it is used for editing a recipe, @recipe will contain information corresponding to the recipe being edited.&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;% if @recipe.errors.any? %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
While saving an recipe or updating a recipe in the database, some violations may occur. Like the recipe name is left empty or its description is empty and so on. So while saving this information, database will signal some error. The errors are sent back to this page and are caught in @recipe.errors.any? statement.&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%=    select('recipe','category_id', Category.all.collect {|c| [c[:name],c[:id]]}) %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
This statement creates a dropdown box.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;‘recipe’ : refers to the object to which the selected value is to be assigned. In this case , it is the recipe object&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;‘category_id’ : create a ‘category_id’ key in the recipe hash.&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;Category.all.collect {|c| [c[:name],c[:id]]} : recover all categories from the database and list them  in the dropdown box. The value for each name is c[:id]. Whenever a category is selected by the user, its corresponding id is assigned to the ‘category_id’ key in the ‘recipe’ object.&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
3. new.html.erb&amp;lt;br&amp;gt;&lt;br /&gt;
This page is used to create a new recipe. This page simply renders the partial described above.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;New recipe&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;%= render 'form' %&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%= render ‘form’ %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
This statement renders the partial.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
4. edit.html.erb&lt;br /&gt;
This page is used to edit a existing recipe. This page also renders the partial.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Editing recipe&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;%= render 'form' %&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Show', @recipe %&amp;gt; |&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
5. show.html.erb&lt;br /&gt;
This page is used to show a particular recipe requested by the user. The recipe requested is captured by the controller in the @recipe instance variable and is passed to this view.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;p id=&amp;quot;notice&amp;quot;&amp;gt;&amp;lt;%= notice %&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Name:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.name %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Description:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.description %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Instructions:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.instructions %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Edit', edit_recipe_path(@recipe) %&amp;gt; |&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;The Category Views are very similar to the recipe Views and can be created similarly.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Conclusion&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
MVC was first proposed by Trygve Reenskaug in 1979. He broke applications into three types of components: models, which is responsible for maintaining the state of the application; views, which are responsible for generating a user interface; and controllers, which orchestrate the applications by receiving events from outside world, interact with model, and display an appropriate view to user. This separation of concerns led to far less computing, and in turn made the code easier to write and maintain.&amp;lt;br&amp;gt;&lt;br /&gt;
Use Ruby on Rails can implement MVC to build various types of web applications. The Cookbook is an easy starter. Readers can learn the basics of Ruby on Rails from this page.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;References&amp;lt;/h1&amp;gt;&lt;br /&gt;
*[1] http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller&lt;br /&gt;
*[2] http://en.wikipedia.org/wiki/Ruby_on_Rails &lt;br /&gt;
*[3] Sam Ruby, Dave Thomas, David Heinemeier Hansson.agile web development with rails (fourth edition)&lt;br /&gt;
*[4] http://en.wikipedia.org/wiki/Don't_repeat_yourself&lt;br /&gt;
*[5] http://guides.rubyonrails.org/getting_started.html&lt;br /&gt;
*[6] http://api.rubyonrails.org/classes/ActiveRecord/Migration.html&lt;br /&gt;
*[7] http://en.wikipedia.org/wiki/Metaprogramming&lt;br /&gt;
*[8] http://net.tutsplus.com/tutorials/other/mvc-for-noobs/&lt;br /&gt;
*[9] Hotzner, S. (2007). Beginnning ruby on rail, Indianapolis: Wiley Publishing Inc&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The old wiki can be found here http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch4_4d_ch.&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w38_nm&amp;diff=67106</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w38 nm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w38_nm&amp;diff=67106"/>
		<updated>2012-10-04T01:46:17Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;Introduction&amp;lt;/h1&amp;gt;&lt;br /&gt;
This page is aimed at asserting the importance of Model View Controller Architecture and how MVC is implemented in Cookbook application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Model View Controller (MVC) Architecture&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The purpose of creating MVC architecture is to have clear separation between the responsibilities of business logic and representation. In a nutshell the responsibilities are mentioned as below:&lt;br /&gt;
&lt;br /&gt;
[[File:MVC.png|frame|none|MVC Responsibilities[8]]]&lt;br /&gt;
&lt;br /&gt;
Model-View-Controller (MVC)[1] is an architectural pattern used in software engineering.In complex computer applications that present a large amount of data to the user, a developer often wishes to separate data (model) and user interface (view) concerns, so that changes to the user interface will not affect data handling, and that the data can be reorganized without changing the user interface.&amp;lt;br&amp;gt;&lt;br /&gt;
The model-view-controller solves this problem by decoupling data access and business logic from data presentation and user interaction, by introducing an intermediate component: the controller. Having described the responsibilities of three components let’s take a look how these components interact with each other.&lt;br /&gt;
&lt;br /&gt;
[[File:MVC_architecture.PNG|frame|none|MVC Architecture[9]]] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Ruby on Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby on Rails[2] is an MVC framework. Rails enforces a structure for application, it helps to knit the models, views, and controllers as separate chunks of functionality while the program executes. No more external configuration needed. Rails is packed with features that make you more productive, with many of the following features building on one other.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Metaprogramming[7]:&amp;lt;/b&amp;gt; Other frameworks use extensive code generation from scratch. Metaprogramming techniques use programs to write programs. Ruby is one of the best languages for metaprogramming, and Rails uses this capability well. Rails also uses code generation but relies much more on metaprogramming for the heavy lifting.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Active Record[6]:&amp;lt;/b&amp;gt; Rails introduces the Active Record framework, which saves objects to the database. The Rails version of Active Record discovers the columns in a database schema and automatically attaches them to your domain objects using metaprogramming.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Convention over configuration:&amp;lt;/b&amp;gt; Most web development frameworks for .NET or Java force you to write pages of configuration code. If you follow suggested naming conventions, Rails doesn't need much configuration.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Scaffolding:&amp;lt;/b&amp;gt; You often create temporary code in the early stages of development to help get an application up quickly and see how major components work together. Rails automatically creates much of the scaffolding you'll need.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Built-in testing:&amp;lt;/b&amp;gt; Rails creates simple automated tests which you can extend. Rails also provides supporting code called harnesses and fixtures that make test cases easier to write and run. Ruby can then execute all your automated tests with the rake utility.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Three environments:&amp;lt;/b&amp;gt; Rails gives you three default environments: development, testing, and production. Each behaves slightly differently, making your entire software development cycle easier. For example, Rails creates a fresh copy of the Test database for each test run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Cookbook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
Cookbook Application is a very basic Ruby on Rails application. It is a great way to understand the nuances that ‘Ruby’ has to offer. It is built keeping in mind that the reader is new to the world of ruby and is developing his/her first Ruby on Rails application.&lt;br /&gt;
The motive of this wiki page is to make the reader understand how the Cookbook application was built. And on the way to building this application, user will be introduced to some key concepts like Model, Views and Controllers. At the end of this page, reader must fairly be ready to start building his/her basic Ruby on Rails application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;A simple application to start -- Cookbook application&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Start a new project. While creating a new project, remember to select Rails Application option as follows&lt;br /&gt;
&lt;br /&gt;
[[File:Start_application.png|frame|none|Start a new Application]] &lt;br /&gt;
&lt;br /&gt;
When we create a Rails application, rails automatically help us create a brunch of files and folders as follows:&lt;br /&gt;
&lt;br /&gt;
[[File:Listing_of_cookbook_application.png|frame|none|Listing_of_cookbook_application]] &lt;br /&gt;
&lt;br /&gt;
We mainly use the app directory. This is where the heart of the application is stored i.e. Models, Views and controllers.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Using scaffold to create framework&amp;lt;/h2&amp;gt;&lt;br /&gt;
Rails scaffolding is a quick way to generate some of the major pieces of an application. If you want to create the models, views, and controllers for a new resource in a single operation, scaffolding is the tool for the job.&amp;lt;br&amp;gt;&lt;br /&gt;
In Rubymine, we can create a scaffold by right clicking on the current Project-&amp;gt;New-&amp;gt;Run Rails Generator -&amp;gt;scaffold. This will open a dialog box to create a scaffold where we can enter the name and the attributes along with their data type. Once the scaffold has been created RubyMine will generate the model, view and controller files respectively. These files do not contain any specific methods but they provide a framework which is useful for general applications. The user can modify the MVC behavior according to his application requirements. Each scaffold has create, read, update and delete methods declared in the controller class. A user can modify the behavior of the controller through these methods. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Create_Scaffold.png|frame|none|Creating a new scaffold]]&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Scaffold.png|frame|left|Scaffold]] [[File:New_Scaffold_Categories.png|frame|center|Creating scaffold for categories]]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Working without scaffold&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or you don't want to create a fully standard framework with scaffold, then you can use RubyMine generate command, you can create merely model, controller and view using generate command, there are many command included in the generate that you can use, expect those MVC, you can also crate migration, assets, intergration_test and performance_test. Or you don't want to create a empty controller and writing code all yourself you can also use scaffold_controller to create controller&lt;br /&gt;
Here is an example. You have create the Model and everything on your own without using the scaffolding in Ruby on Rails. You create the following class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Dog &amp;lt; ActiveRecord::Base &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
You can add a table Dogs. by&lt;br /&gt;
&amp;lt;pre&amp;gt;rails g migration CreateDogs&amp;lt;/pre&amp;gt;&lt;br /&gt;
which also a generate command&lt;br /&gt;
&lt;br /&gt;
In the Cookbook application we are considering a Cookbook which contains recipes for various dishes. The recipes have been divided into different categories, for example there is a recipe for ice tea which belongs to the category of beverages. There is a relationship which is created between a recipe and a category which can be expressed as an ice tea belongs to beverages. We can have many such recipes belonging to a single category, in this case many to one relationship is established. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Controllers for Cookbook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
The Cookbook application starts by entering a URL in the browser which sends the request to the web server. Since our web server supports Rails application, it will hand over the control to a Rails controller.&lt;br /&gt;
A controller ‘controls’ the entire application by responding to the incoming requests as needed. For example, consider the following URL http ://localhost:3000/categories/show which is decoded by the controller to know that its action named show has been invoked.&lt;br /&gt;
All the controllers inherit only the ApplicationController which in turn inherits the ActionController::Base class. ActionContoller module provides the basic support for the controllers in Rails.&lt;br /&gt;
In our Cookbook application we generate Categories controller using the scaffold mechanism which populates the controller with the basic methods such as index, create, show, new, edit, update and destroy.&lt;br /&gt;
A simple example of index method is as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    @categories = Category.all&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # index.html.erb&lt;br /&gt;
      format.json { render json: @categories }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In ruby, @ operator is used to declare instance variables. The index method makes use of @categories instance variable which is populated with all the records in the Category table. The respond_to method is used for responding to particular type of request. It contains a block and a variable |format| which determines whether the incoming request should be responded with HTML (when we are interacting with a user) or JSON format (if we are returning an object). &lt;br /&gt;
After executing the code, the controller will render a template of the same name. (For this example, the template is views/categories/index.html.erb).&lt;br /&gt;
Similarly the show method is defined as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def show&lt;br /&gt;
    @category = Category.find(params[:id])&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # show.html.erb&lt;br /&gt;
      format.json { render json: @category }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The show method is used to display a particular object. The show method needs to access database to search for the given object. This is done using the find method which takes params[:id] as argument. The params[:id] contains the id or primary key of the object which is passed as a parameter to look up in the database and the result is stored in the instance variable. The result is displayed by rendering the show.html.erb file.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Active Record&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, a program deals with objects which are mapped into database relational databases. This kind of mapping is facilitated by the Active Record.&amp;lt;br&amp;gt;Active Record maps the Rails classes in to database tables and rails objects into database records.&lt;br /&gt;
This makes it easy to perform actions on database tables by invoking the class methods. Another benefit of Active Record is database migration. Migrations can manage the evolution of a schema used by several physical databases. It’s a solution to the common problem of adding a field to make a new feature work in your local database, but being unsure of how to push that change to other developers and to the production server. With migrations, you can describe the transformations in self-contained classes that can be checked into version control systems and executed against another database that might be one, two, or five versions behind.You can find a lot more details about rails migration here[8].&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Method Pairs&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;There are couple of method pairs such as new-create and edit-update which are executed in sequence. In method pair one method is responsible for display preparation and the other method validates the data and attempts it to save in the database.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def new&lt;br /&gt;
    @category = Category.new&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # new.html.erb&lt;br /&gt;
      format.json { render json: @category }&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def create&lt;br /&gt;
    @category = Category.new(params[:category])&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      if @category.save&lt;br /&gt;
        format.html { redirect_to @category, notice: 'Category was successfully created.' }&lt;br /&gt;
        format.json { render json: @category, status: :created, location: @category }&lt;br /&gt;
      else&lt;br /&gt;
        format.html { render action: &amp;quot;new&amp;quot; }&lt;br /&gt;
        format.json { render json: @category.errors, status: :unprocessable_entity }&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The new and create methods are known as method pairs which execute in sequence. The new method is used to instantiate a new Category object whereas create method is used to successfully save the object to the database. After execution of the new method, the controller renders the new.html.erb file which displays a form to create a new Category object. The user enters the required information in the form and the object id is returned in the URL on submitting the form. Consider the following URL is generated on submitting the form: http: //localhost:3000/categories/3.&lt;br /&gt;
This means that Category object with id =3 will be created. The details of the object will be processed by the create method. &lt;br /&gt;
The controller invokes the create method to store the object in the database. An observation can be made about the @category variable in the create method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@category = Category.new(params[:category])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The parameters passed in the URL after creating a new object is stored in the params hash (denoted by params[]). In this case the params[] contains an object with category_id = 3. The params[] assigns the data of object with category_id =3 to the instance variable. The data entered in the instance variable is validated using the @category.save method. If no errors are found then the object stored in the database and a message on successful creation of the object is displayed to the user. However, if the data entered in the object contains an error, the user is redirected to the new.html.erb page to resubmit the information and the errors are displayed. &lt;br /&gt;
Another example of such method pair is the edit and update methods. The edit method displays the table entry for an existing object, while the update method is invoked when the changes have been submitted.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Models of Cookbook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
Model handles the data processing in a web application. Actions interact with the model to manipulate the data. Rails is capable of handling databases and this is facilitated by the model. You can base models on the Rails ActiveRecord module which contains support databases.&lt;br /&gt;
In our Cookbook application we have two models namely: Category and Recipe. They are represented by files category.rb and recipe.rb and each file is used to describe the relation between two classes. Let’s take a look at the two models&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt; category.rb&amp;lt;/b&amp;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;
&amp;lt;b&amp;gt;recipe.rb&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;Relationships in models&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;one-to-one&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Syllabus &amp;lt; ActiveRecord::Base&lt;br /&gt;
	belongs_to :course¬&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Course &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_one :syllabus&lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;many-many&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Student &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_and_belongs_to_many :courses&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Course &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_and_belongs_to_many :students&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Cookbook, we have many-one relationship between recipes and categories.&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Views of Cookbook application&amp;lt;/h1&amp;gt;&lt;br /&gt;
What is V in MVC? ‘V’ refers to views. Views are the actual interface that users can see. It is through views, that a user may request some data to be viewed or submits some data for processing.&amp;lt;br&amp;gt;&lt;br /&gt;
Views in rails are of the form file_name.html.erb. Here erb means embedded ruby. What html.erb denotes is some ruby code is embedded in HTML code. Like HTML, embedded ruby also uses tags to represent data. However, to distinguish it from the HTML code, ruby is embedded between &amp;lt;% %&amp;gt; if some non-assignment ruby operation is to be performed. If something needs to be displayed on screen, &amp;lt;%= %&amp;gt; is used.&lt;br /&gt;
Views are present in “app/views” folder in the rails application.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Name of the view files is same as the name of their respective controller. For e.g.:- if I have a controller movie with an action new, the corresponding view page will be new.html.erb in the app/views/movie directory.&amp;lt;br&amp;gt;&lt;br /&gt;
The Cookbook application views are as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Recipe Controller Views&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
1. index.html.erb&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;nowiki&amp;gt;&amp;lt;h1&amp;gt;Listing categories&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
  &amp;lt;tr&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;Name&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;% @categories.each do |category| %&amp;gt;&lt;br /&gt;
  &amp;lt;tr&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= category.name %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Show', category %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Edit', edit_category_path(category) %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Destroy', category, method: :delete, data: { confirm: 'Are you sure?' } %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%= link_to 'New Category', new_category_path %&amp;gt;&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;% @recipes.each do |recipe| %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
Here @recipes is an instance variable of model recipe defined in index action of the controller ‘recipes’. It contains all the recipes currently present in our application. Then each recipe is taken from this instance variable and is assigned to the local variable recipe.  All the subsequent lines display the recipe details (name, description and instruction).&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%= link_to ‘Show’, recipe %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
link_to is an ruby operator which is used to redirect the control to some other page. ‘Show’ is displayed as it is on the page and is an hyperlink. It links to a page pointed by recipe.  The path is defined in the routes.rb file. &lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
2. _form.html.erb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is a partial. Partials are devices used for breaking the code in smaller, more manageable chunks. This helps in modularity and also maintain DRY principle[4]. With a partial, code for rendering a particular response is moved to its own file. So for e.g. we can use this same partial for creating and editing information, because the information involved is essentially the same.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= form_for(@recipe) do |f| %&amp;gt;&lt;br /&gt;
  &amp;lt;% if @recipe.errors.any? %&amp;gt;&lt;br /&gt;
    &amp;lt;div id=&amp;quot;error_explanation&amp;quot;&amp;gt;&lt;br /&gt;
      &amp;lt;h2&amp;gt;&amp;lt;%= pluralize(@recipe.errors.count, &amp;quot;error&amp;quot;) %&amp;gt; prohibited this recipe from being saved:&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      &amp;lt;ul&amp;gt;&lt;br /&gt;
      &amp;lt;% @recipe.errors.full_messages.each do |msg| %&amp;gt;&lt;br /&gt;
        &amp;lt;li&amp;gt;&amp;lt;%= msg %&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
      &amp;lt;% end %&amp;gt;&lt;br /&gt;
      &amp;lt;/ul&amp;gt;&lt;br /&gt;
    &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;% end %&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :name %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_field :name %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
    &amp;lt;%=  f.label :category %&amp;gt;&lt;br /&gt;
    &amp;lt;%=    select('recipe','category_id', Category.all.collect {|c| [c[:name],c[:id]]}) %&amp;gt;&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :description %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_field :description %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :instructions %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_area :instructions %&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.submit %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;&amp;lt;%= form_for(@recipe) do |f| %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
This statement means to render a form for the instance variable @recipe.&lt;br /&gt;
If this page is being used for creating a new recipe, then the values for each key in the @recipe hash will be empty. If it is used for editing a recipe, @recipe will contain information corresponding to the recipe being edited.&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;% if @recipe.errors.any? %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
While saving an recipe or updating a recipe in the database, some violations may occur. Like the recipe name is left empty or its description is empty and so on. So while saving this information, database will signal some error. The errors are sent back to this page and are caught in @recipe.errors.any? statement.&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%=    select('recipe','category_id', Category.all.collect {|c| [c[:name],c[:id]]}) %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
This statement creates a dropdown box.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;‘recipe’ : refers to the object to which the selected value is to be assigned. In this case , it is the recipe object&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;‘category_id’ : create a ‘category_id’ key in the recipe hash.&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;Category.all.collect {|c| [c[:name],c[:id]]} : recover all categories from the database and list them  in the dropdown box. The value for each name is c[:id]. Whenever a category is selected by the user, its corresponding id is assigned to the ‘category_id’ key in the ‘recipe’ object.&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
3. new.html.erb&amp;lt;br&amp;gt;&lt;br /&gt;
This page is used to create a new recipe. This page simply renders the partial described above.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;New recipe&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;%= render 'form' %&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%= render ‘form’ %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
This statement renders the partial.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
4. edit.html.erb&lt;br /&gt;
This page is used to edit a existing recipe. This page also renders the partial.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Editing recipe&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;%= render 'form' %&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Show', @recipe %&amp;gt; |&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
5. show.html.erb&lt;br /&gt;
This page is used to show a particular recipe requested by the user. The recipe requested is captured by the controller in the @recipe instance variable and is passed to this view.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;p id=&amp;quot;notice&amp;quot;&amp;gt;&amp;lt;%= notice %&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Name:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.name %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Description:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.description %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Instructions:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.instructions %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Edit', edit_recipe_path(@recipe) %&amp;gt; |&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;The Category Views are very similar to the recipe Views and can be created similarly.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Conclusion&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
MVC was first proposed by Trygve Reenskaug in 1979. He broke applications into three types of components: models, which is responsible for maintaining the state of the application; views, which are responsible for generating a user interface; and controllers, which orchestrate the applications by receiving events from outside world, interact with model, and display an appropriate view to user. This separation of concerns led to far less computing, and in turn made the code easier to write and maintain.&amp;lt;br&amp;gt;&lt;br /&gt;
Use Ruby on Rails can implement MVC to build various types of web applications. The Cookbook is an easy starter. Readers can learn the basics of Ruby on Rails from this page.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;References&amp;lt;/h1&amp;gt;&lt;br /&gt;
*[1] http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller&lt;br /&gt;
*[2] http://en.wikipedia.org/wiki/Ruby_on_Rails &lt;br /&gt;
*[3] Sam Ruby, Dave Thomas, David Heinemeier Hansson.agile web development with rails (fourth edition)&lt;br /&gt;
*[4] http://en.wikipedia.org/wiki/Don't_repeat_yourself&lt;br /&gt;
*[5] http://guides.rubyonrails.org/getting_started.html&lt;br /&gt;
*[6] http://api.rubyonrails.org/classes/ActiveRecord/Migration.html&lt;br /&gt;
*[7] http://en.wikipedia.org/wiki/Metaprogramming&lt;br /&gt;
*[8] http://net.tutsplus.com/tutorials/other/mvc-for-noobs/&lt;br /&gt;
*[9] Hotzner, S. (2007). Beginnning ruby on rail, Indianapolis: Wiley Publishing Inc&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The old wiki can be found here http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch4_4d_ch.&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w38_nm&amp;diff=67104</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w38 nm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w38_nm&amp;diff=67104"/>
		<updated>2012-10-04T01:45:24Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;Introduction&amp;lt;/h1&amp;gt;&lt;br /&gt;
This page is aimed at asserting the importance of Model View Controller Architecture and how MVC is implemented in Cookbook application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Model View Controller (MVC) Architecture&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The purpose of creating MVC architecture is to have clear separation between the responsibilities of business logic and representation. In a nutshell the responsibilities are mentioned as below:&lt;br /&gt;
&lt;br /&gt;
[[File:MVC.png|frame|none|MVC Responsibilities[8]]]&lt;br /&gt;
&lt;br /&gt;
Model-View-Controller (MVC)[1] is an architectural pattern used in software engineering.In complex computer applications that present a large amount of data to the user, a developer often wishes to separate data (model) and user interface (view) concerns, so that changes to the user interface will not affect data handling, and that the data can be reorganized without changing the user interface.&amp;lt;br&amp;gt;&lt;br /&gt;
The model-view-controller solves this problem by decoupling data access and business logic from data presentation and user interaction, by introducing an intermediate component: the controller. Having described the responsibilities of three components let’s take a look how these components interact with each other.&lt;br /&gt;
&lt;br /&gt;
[[File:MVC_architecture.PNG|frame|none|MVC Architecture[9]]] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Ruby on Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby on Rails[2] is an MVC framework. Rails enforces a structure for application, it helps to knit the models, views, and controllers as separate chunks of functionality while the program executes. No more external configuration needed. Rails is packed with features that make you more productive, with many of the following features building on one other.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Metaprogramming[7]:&amp;lt;/b&amp;gt; Other frameworks use extensive code generation from scratch. Metaprogramming techniques use programs to write programs. Ruby is one of the best languages for metaprogramming, and Rails uses this capability well. Rails also uses code generation but relies much more on metaprogramming for the heavy lifting.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Active Record[6]:&amp;lt;/b&amp;gt; Rails introduces the Active Record framework, which saves objects to the database. The Rails version of Active Record discovers the columns in a database schema and automatically attaches them to your domain objects using metaprogramming.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Convention over configuration:&amp;lt;/b&amp;gt; Most web development frameworks for .NET or Java force you to write pages of configuration code. If you follow suggested naming conventions, Rails doesn't need much configuration.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Scaffolding:&amp;lt;/b&amp;gt; You often create temporary code in the early stages of development to help get an application up quickly and see how major components work together. Rails automatically creates much of the scaffolding you'll need.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Built-in testing:&amp;lt;/b&amp;gt; Rails creates simple automated tests you can then extend. Rails also provides supporting code called harnesses and fixtures that make test cases easier to write and run. Ruby can then execute all your automated tests with the rake utility.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Three environments:&amp;lt;/b&amp;gt; Rails gives you three default environments: development, testing, and production. Each behaves slightly differently, making your entire software development cycle easier. For example, Rails creates a fresh copy of the Test database for each test run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Cookbook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
Cookbook Application is a very basic Ruby on Rails application. It is a great way to understand the nuances that ‘Ruby’ has to offer. It is built keeping in mind that the reader is new to the world of ruby and is developing his/her first Ruby on Rails application.&lt;br /&gt;
The motive of this wiki page is to make the reader understand how the Cookbook application was built. And on the way to building this application, user will be introduced to some key concepts like Model, Views and Controllers. At the end of this page, reader must fairly be ready to start building his/her basic Ruby on Rails application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;A simple application to start -- Cookbook application&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Start a new project. While creating a new project, remember to select Rails Application option as follows&lt;br /&gt;
&lt;br /&gt;
[[File:Start_application.png|frame|none|Start a new Application]] &lt;br /&gt;
&lt;br /&gt;
When we create a Rails application, rails automatically help us create a brunch of files and folders as follows:&lt;br /&gt;
&lt;br /&gt;
[[File:Listing_of_cookbook_application.png|frame|none|Listing_of_cookbook_application]] &lt;br /&gt;
&lt;br /&gt;
We mainly use the app directory. This is where the heart of the application is stored i.e. Models, Views and controllers.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Using scaffold to create framework&amp;lt;/h2&amp;gt;&lt;br /&gt;
Rails scaffolding is a quick way to generate some of the major pieces of an application. If you want to create the models, views, and controllers for a new resource in a single operation, scaffolding is the tool for the job.&amp;lt;br&amp;gt;&lt;br /&gt;
In Rubymine, we can create a scaffold by right clicking on the current Project-&amp;gt;New-&amp;gt;Run Rails Generator -&amp;gt;scaffold. This will open a dialog box to create a scaffold where we can enter the name and the attributes along with their data type. Once the scaffold has been created RubyMine will generate the model, view and controller files respectively. These files do not contain any specific methods but they provide a framework which is useful for general applications. The user can modify the MVC behavior according to his application requirements. Each scaffold has create, read, update and delete methods declared in the controller class. A user can modify the behavior of the controller through these methods. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Create_Scaffold.png|frame|none|Creating a new scaffold]]&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Scaffold.png|frame|left|Scaffold]] [[File:New_Scaffold_Categories.png|frame|center|Creating scaffold for categories]]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Working without scaffold&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or you don't want to create a fully standard framework with scaffold, then you can use RubyMine generate command, you can create merely model, controller and view using generate command, there are many command included in the generate that you can use, expect those MVC, you can also crate migration, assets, intergration_test and performance_test. Or you don't want to create a empty controller and writing code all yourself you can also use scaffold_controller to create controller&lt;br /&gt;
Here is an example. You have create the Model and everything on your own without using the scaffolding in Ruby on Rails. You create the following class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Dog &amp;lt; ActiveRecord::Base &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
You can add a table Dogs. by&lt;br /&gt;
&amp;lt;pre&amp;gt;rails g migration CreateDogs&amp;lt;/pre&amp;gt;&lt;br /&gt;
which also a generate command&lt;br /&gt;
&lt;br /&gt;
In the Cookbook application we are considering a Cookbook which contains recipes for various dishes. The recipes have been divided into different categories, for example there is a recipe for ice tea which belongs to the category of beverages. There is a relationship which is created between a recipe and a category which can be expressed as an ice tea belongs to beverages. We can have many such recipes belonging to a single category, in this case many to one relationship is established. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Controllers for Cookbook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
The Cookbook application starts by entering a URL in the browser which sends the request to the web server. Since our web server supports Rails application, it will hand over the control to a Rails controller.&lt;br /&gt;
A controller ‘controls’ the entire application by responding to the incoming requests as needed. For example, consider the following URL http ://localhost:3000/categories/show which is decoded by the controller to know that its action named show has been invoked.&lt;br /&gt;
All the controllers inherit only the ApplicationController which in turn inherits the ActionController::Base class. ActionContoller module provides the basic support for the controllers in Rails.&lt;br /&gt;
In our Cookbook application we generate Categories controller using the scaffold mechanism which populates the controller with the basic methods such as index, create, show, new, edit, update and destroy.&lt;br /&gt;
A simple example of index method is as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    @categories = Category.all&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # index.html.erb&lt;br /&gt;
      format.json { render json: @categories }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In ruby, @ operator is used to declare instance variables. The index method makes use of @categories instance variable which is populated with all the records in the Category table. The respond_to method is used for responding to particular type of request. It contains a block and a variable |format| which determines whether the incoming request should be responded with HTML (when we are interacting with a user) or JSON format (if we are returning an object). &lt;br /&gt;
After executing the code, the controller will render a template of the same name. (For this example, the template is views/categories/index.html.erb).&lt;br /&gt;
Similarly the show method is defined as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def show&lt;br /&gt;
    @category = Category.find(params[:id])&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # show.html.erb&lt;br /&gt;
      format.json { render json: @category }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The show method is used to display a particular object. The show method needs to access database to search for the given object. This is done using the find method which takes params[:id] as argument. The params[:id] contains the id or primary key of the object which is passed as a parameter to look up in the database and the result is stored in the instance variable. The result is displayed by rendering the show.html.erb file.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Active Record&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, a program deals with objects which are mapped into database relational databases. This kind of mapping is facilitated by the Active Record.&amp;lt;br&amp;gt;Active Record maps the Rails classes in to database tables and rails objects into database records.&lt;br /&gt;
This makes it easy to perform actions on database tables by invoking the class methods. Another benefit of Active Record is database migration. Migrations can manage the evolution of a schema used by several physical databases. It’s a solution to the common problem of adding a field to make a new feature work in your local database, but being unsure of how to push that change to other developers and to the production server. With migrations, you can describe the transformations in self-contained classes that can be checked into version control systems and executed against another database that might be one, two, or five versions behind.You can find a lot more details about rails migration here[8].&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Method Pairs&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;There are couple of method pairs such as new-create and edit-update which are executed in sequence. In method pair one method is responsible for display preparation and the other method validates the data and attempts it to save in the database.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def new&lt;br /&gt;
    @category = Category.new&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # new.html.erb&lt;br /&gt;
      format.json { render json: @category }&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def create&lt;br /&gt;
    @category = Category.new(params[:category])&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      if @category.save&lt;br /&gt;
        format.html { redirect_to @category, notice: 'Category was successfully created.' }&lt;br /&gt;
        format.json { render json: @category, status: :created, location: @category }&lt;br /&gt;
      else&lt;br /&gt;
        format.html { render action: &amp;quot;new&amp;quot; }&lt;br /&gt;
        format.json { render json: @category.errors, status: :unprocessable_entity }&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The new and create methods are known as method pairs which execute in sequence. The new method is used to instantiate a new Category object whereas create method is used to successfully save the object to the database. After execution of the new method, the controller renders the new.html.erb file which displays a form to create a new Category object. The user enters the required information in the form and the object id is returned in the URL on submitting the form. Consider the following URL is generated on submitting the form: http: //localhost:3000/categories/3.&lt;br /&gt;
This means that Category object with id =3 will be created. The details of the object will be processed by the create method. &lt;br /&gt;
The controller invokes the create method to store the object in the database. An observation can be made about the @category variable in the create method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@category = Category.new(params[:category])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The parameters passed in the URL after creating a new object is stored in the params hash (denoted by params[]). In this case the params[] contains an object with category_id = 3. The params[] assigns the data of object with category_id =3 to the instance variable. The data entered in the instance variable is validated using the @category.save method. If no errors are found then the object stored in the database and a message on successful creation of the object is displayed to the user. However, if the data entered in the object contains an error, the user is redirected to the new.html.erb page to resubmit the information and the errors are displayed. &lt;br /&gt;
Another example of such method pair is the edit and update methods. The edit method displays the table entry for an existing object, while the update method is invoked when the changes have been submitted.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Models of Cookbook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
Model handles the data processing in a web application. Actions interact with the model to manipulate the data. Rails is capable of handling databases and this is facilitated by the model. You can base models on the Rails ActiveRecord module which contains support databases.&lt;br /&gt;
In our Cookbook application we have two models namely: Category and Recipe. They are represented by files category.rb and recipe.rb and each file is used to describe the relation between two classes. Let’s take a look at the two models&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt; category.rb&amp;lt;/b&amp;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;
&amp;lt;b&amp;gt;recipe.rb&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;Relationships in models&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;one-to-one&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Syllabus &amp;lt; ActiveRecord::Base&lt;br /&gt;
	belongs_to :course¬&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Course &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_one :syllabus&lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;many-many&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Student &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_and_belongs_to_many :courses&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Course &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_and_belongs_to_many :students&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Cookbook, we have many-one relationship between recipes and categories.&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Views of Cookbook application&amp;lt;/h1&amp;gt;&lt;br /&gt;
What is V in MVC? ‘V’ refers to views. Views are the actual interface that users can see. It is through views, that a user may request some data to be viewed or submits some data for processing.&amp;lt;br&amp;gt;&lt;br /&gt;
Views in rails are of the form file_name.html.erb. Here erb means embedded ruby. What html.erb denotes is some ruby code is embedded in HTML code. Like HTML, embedded ruby also uses tags to represent data. However, to distinguish it from the HTML code, ruby is embedded between &amp;lt;% %&amp;gt; if some non-assignment ruby operation is to be performed. If something needs to be displayed on screen, &amp;lt;%= %&amp;gt; is used.&lt;br /&gt;
Views are present in “app/views” folder in the rails application.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Name of the view files is same as the name of their respective controller. For e.g.:- if I have a controller movie with an action new, the corresponding view page will be new.html.erb in the app/views/movie directory.&amp;lt;br&amp;gt;&lt;br /&gt;
The Cookbook application views are as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Recipe Controller Views&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
1. index.html.erb&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;nowiki&amp;gt;&amp;lt;h1&amp;gt;Listing categories&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
  &amp;lt;tr&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;Name&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;% @categories.each do |category| %&amp;gt;&lt;br /&gt;
  &amp;lt;tr&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= category.name %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Show', category %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Edit', edit_category_path(category) %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Destroy', category, method: :delete, data: { confirm: 'Are you sure?' } %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%= link_to 'New Category', new_category_path %&amp;gt;&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;% @recipes.each do |recipe| %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
Here @recipes is an instance variable of model recipe defined in index action of the controller ‘recipes’. It contains all the recipes currently present in our application. Then each recipe is taken from this instance variable and is assigned to the local variable recipe.  All the subsequent lines display the recipe details (name, description and instruction).&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%= link_to ‘Show’, recipe %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
link_to is an ruby operator which is used to redirect the control to some other page. ‘Show’ is displayed as it is on the page and is an hyperlink. It links to a page pointed by recipe.  The path is defined in the routes.rb file. &lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
2. _form.html.erb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is a partial. Partials are devices used for breaking the code in smaller, more manageable chunks. This helps in modularity and also maintain DRY principle[4]. With a partial, code for rendering a particular response is moved to its own file. So for e.g. we can use this same partial for creating and editing information, because the information involved is essentially the same.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= form_for(@recipe) do |f| %&amp;gt;&lt;br /&gt;
  &amp;lt;% if @recipe.errors.any? %&amp;gt;&lt;br /&gt;
    &amp;lt;div id=&amp;quot;error_explanation&amp;quot;&amp;gt;&lt;br /&gt;
      &amp;lt;h2&amp;gt;&amp;lt;%= pluralize(@recipe.errors.count, &amp;quot;error&amp;quot;) %&amp;gt; prohibited this recipe from being saved:&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      &amp;lt;ul&amp;gt;&lt;br /&gt;
      &amp;lt;% @recipe.errors.full_messages.each do |msg| %&amp;gt;&lt;br /&gt;
        &amp;lt;li&amp;gt;&amp;lt;%= msg %&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
      &amp;lt;% end %&amp;gt;&lt;br /&gt;
      &amp;lt;/ul&amp;gt;&lt;br /&gt;
    &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;% end %&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :name %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_field :name %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
    &amp;lt;%=  f.label :category %&amp;gt;&lt;br /&gt;
    &amp;lt;%=    select('recipe','category_id', Category.all.collect {|c| [c[:name],c[:id]]}) %&amp;gt;&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :description %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_field :description %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :instructions %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_area :instructions %&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.submit %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;&amp;lt;%= form_for(@recipe) do |f| %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
This statement means to render a form for the instance variable @recipe.&lt;br /&gt;
If this page is being used for creating a new recipe, then the values for each key in the @recipe hash will be empty. If it is used for editing a recipe, @recipe will contain information corresponding to the recipe being edited.&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;% if @recipe.errors.any? %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
While saving an recipe or updating a recipe in the database, some violations may occur. Like the recipe name is left empty or its description is empty and so on. So while saving this information, database will signal some error. The errors are sent back to this page and are caught in @recipe.errors.any? statement.&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%=    select('recipe','category_id', Category.all.collect {|c| [c[:name],c[:id]]}) %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
This statement creates a dropdown box.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;‘recipe’ : refers to the object to which the selected value is to be assigned. In this case , it is the recipe object&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;‘category_id’ : create a ‘category_id’ key in the recipe hash.&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;Category.all.collect {|c| [c[:name],c[:id]]} : recover all categories from the database and list them  in the dropdown box. The value for each name is c[:id]. Whenever a category is selected by the user, its corresponding id is assigned to the ‘category_id’ key in the ‘recipe’ object.&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
3. new.html.erb&amp;lt;br&amp;gt;&lt;br /&gt;
This page is used to create a new recipe. This page simply renders the partial described above.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;New recipe&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;%= render 'form' %&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%= render ‘form’ %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
This statement renders the partial.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
4. edit.html.erb&lt;br /&gt;
This page is used to edit a existing recipe. This page also renders the partial.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Editing recipe&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;%= render 'form' %&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Show', @recipe %&amp;gt; |&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
5. show.html.erb&lt;br /&gt;
This page is used to show a particular recipe requested by the user. The recipe requested is captured by the controller in the @recipe instance variable and is passed to this view.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;p id=&amp;quot;notice&amp;quot;&amp;gt;&amp;lt;%= notice %&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Name:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.name %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Description:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.description %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Instructions:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.instructions %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Edit', edit_recipe_path(@recipe) %&amp;gt; |&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;The Category Views are very similar to the recipe Views and can be created similarly.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Conclusion&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
MVC was first proposed by Trygve Reenskaug in 1979. He broke applications into three types of components: models, which is responsible for maintaining the state of the application; views, which are responsible for generating a user interface; and controllers, which orchestrate the applications by receiving events from outside world, interact with model, and display an appropriate view to user. This separation of concerns led to far less computing, and in turn made the code easier to write and maintain.&amp;lt;br&amp;gt;&lt;br /&gt;
Use Ruby on Rails can implement MVC to build various types of web applications. The Cookbook is an easy starter. Readers can learn the basics of Ruby on Rails from this page.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;References&amp;lt;/h1&amp;gt;&lt;br /&gt;
*[1] http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller&lt;br /&gt;
*[2] http://en.wikipedia.org/wiki/Ruby_on_Rails &lt;br /&gt;
*[3] Sam Ruby, Dave Thomas, David Heinemeier Hansson.agile web development with rails (fourth edition)&lt;br /&gt;
*[4] http://en.wikipedia.org/wiki/Don't_repeat_yourself&lt;br /&gt;
*[5] http://guides.rubyonrails.org/getting_started.html&lt;br /&gt;
*[6] http://api.rubyonrails.org/classes/ActiveRecord/Migration.html&lt;br /&gt;
*[7] http://en.wikipedia.org/wiki/Metaprogramming&lt;br /&gt;
*[8] http://net.tutsplus.com/tutorials/other/mvc-for-noobs/&lt;br /&gt;
*[9] Hotzner, S. (2007). Beginnning ruby on rail, Indianapolis: Wiley Publishing Inc&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The old wiki can be found here http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch4_4d_ch.&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w38_nm&amp;diff=67103</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w38 nm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w38_nm&amp;diff=67103"/>
		<updated>2012-10-04T01:44:38Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;Introduction&amp;lt;/h1&amp;gt;&lt;br /&gt;
This page is aimed at asserting the importance of Model View Controller Architecture and how MVC is implemented in Cookbook application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Model View Controller (MVC) Architecture&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The purpose of creating MVC architecture is to have clear separation between the responsibilities of business logic and representation. In a nutshell the responsibilities are mentioned as below:&lt;br /&gt;
&lt;br /&gt;
[[File:MVC.png|frame|none|MVC Responsibilities[8]]]&lt;br /&gt;
&lt;br /&gt;
Model-View-Controller (MVC)[1] is an architectural pattern used in software engineering.In complex computer applications that present a large amount of data to the user, a developer often wishes to separate data (model) and user interface (view) concerns, so that changes to the user interface will not affect data handling, and that the data can be reorganized without changing the user interface.&amp;lt;br&amp;gt;&lt;br /&gt;
The model-view-controller solves this problem by decoupling data access and business logic from data presentation and user interaction, by introducing an intermediate component: the controller. Having described the responsibilities of three components let’s take a look how these components interact with each other.&lt;br /&gt;
&lt;br /&gt;
[[File:MVC_architecture.PNG|frame|none|MVC Architecture[9]]] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Ruby on Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby on Rails[2] is an MVC framework. Rails enforces a structure for application, it helps to knit the models, views, and controllers as separate chunks of functionality while the program executes. No more external configuration needed. Rails is packed with features that make you more productive, with many of the following features building on one other.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Metaprogramming[7]:&amp;lt;/b&amp;gt; Other frameworks use extensive code generation from scratch. Metaprogramming[9] techniques use programs to write programs. Ruby is one of the best languages for metaprogramming, and Rails uses this capability well. Rails also uses code generation but relies much more on metaprogramming for the heavy lifting.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Active Record:&amp;lt;/b&amp;gt; Rails introduces the Active Record[6] framework, which saves objects to the database. The Rails version of Active Record discovers the columns in a database schema and automatically attaches them to your domain objects using metaprogramming.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Convention over configuration:&amp;lt;/b&amp;gt; Most web development frameworks for .NET or Java force you to write pages of configuration code. If you follow suggested naming conventions, Rails doesn't need much configuration.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Scaffolding:&amp;lt;/b&amp;gt; You often create temporary code in the early stages of development to help get an application up quickly and see how major components work together. Rails automatically creates much of the scaffolding you'll need.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Built-in testing:&amp;lt;/b&amp;gt; Rails creates simple automated tests you can then extend. Rails also provides supporting code called harnesses and fixtures that make test cases easier to write and run. Ruby can then execute all your automated tests with the rake utility.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Three environments:&amp;lt;/b&amp;gt; Rails gives you three default environments: development, testing, and production. Each behaves slightly differently, making your entire software development cycle easier. For example, Rails creates a fresh copy of the Test database for each test run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Cookbook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
Cookbook Application is a very basic Ruby on Rails application. It is a great way to understand the nuances that ‘Ruby’ has to offer. It is built keeping in mind that the reader is new to the world of ruby and is developing his/her first Ruby on Rails application.&lt;br /&gt;
The motive of this wiki page is to make the reader understand how the Cookbook application was built. And on the way to building this application, user will be introduced to some key concepts like Model, Views and Controllers. At the end of this page, reader must fairly be ready to start building his/her basic Ruby on Rails application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;A simple application to start -- Cookbook application&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Start a new project. While creating a new project, remember to select Rails Application option as follows&lt;br /&gt;
&lt;br /&gt;
[[File:Start_application.png|frame|none|Start a new Application]] &lt;br /&gt;
&lt;br /&gt;
When we create a Rails application, rails automatically help us create a brunch of files and folders as follows:&lt;br /&gt;
&lt;br /&gt;
[[File:Listing_of_cookbook_application.png|frame|none|Listing_of_cookbook_application]] &lt;br /&gt;
&lt;br /&gt;
We mainly use the app directory. This is where the heart of the application is stored i.e. Models, Views and controllers.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Using scaffold to create framework&amp;lt;/h2&amp;gt;&lt;br /&gt;
Rails scaffolding is a quick way to generate some of the major pieces of an application. If you want to create the models, views, and controllers for a new resource in a single operation, scaffolding is the tool for the job.&amp;lt;br&amp;gt;&lt;br /&gt;
In Rubymine, we can create a scaffold by right clicking on the current Project-&amp;gt;New-&amp;gt;Run Rails Generator -&amp;gt;scaffold. This will open a dialog box to create a scaffold where we can enter the name and the attributes along with their data type. Once the scaffold has been created RubyMine will generate the model, view and controller files respectively. These files do not contain any specific methods but they provide a framework which is useful for general applications. The user can modify the MVC behavior according to his application requirements. Each scaffold has create, read, update and delete methods declared in the controller class. A user can modify the behavior of the controller through these methods. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Create_Scaffold.png|frame|none|Creating a new scaffold]]&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Scaffold.png|frame|left|Scaffold]] [[File:New_Scaffold_Categories.png|frame|center|Creating scaffold for categories]]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Working without scaffold&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or you don't want to create a fully standard framework with scaffold, then you can use RubyMine generate command, you can create merely model, controller and view using generate command, there are many command included in the generate that you can use, expect those MVC, you can also crate migration, assets, intergration_test and performance_test. Or you don't want to create a empty controller and writing code all yourself you can also use scaffold_controller to create controller&lt;br /&gt;
Here is an example. You have create the Model and everything on your own without using the scaffolding in Ruby on Rails. You create the following class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Dog &amp;lt; ActiveRecord::Base &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
You can add a table Dogs. by&lt;br /&gt;
&amp;lt;pre&amp;gt;rails g migration CreateDogs&amp;lt;/pre&amp;gt;&lt;br /&gt;
which also a generate command&lt;br /&gt;
&lt;br /&gt;
In the Cookbook application we are considering a Cookbook which contains recipes for various dishes. The recipes have been divided into different categories, for example there is a recipe for ice tea which belongs to the category of beverages. There is a relationship which is created between a recipe and a category which can be expressed as an ice tea belongs to beverages. We can have many such recipes belonging to a single category, in this case many to one relationship is established. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Controllers for Cookbook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
The Cookbook application starts by entering a URL in the browser which sends the request to the web server. Since our web server supports Rails application, it will hand over the control to a Rails controller.&lt;br /&gt;
A controller ‘controls’ the entire application by responding to the incoming requests as needed. For example, consider the following URL http ://localhost:3000/categories/show which is decoded by the controller to know that its action named show has been invoked.&lt;br /&gt;
All the controllers inherit only the ApplicationController which in turn inherits the ActionController::Base class. ActionContoller module provides the basic support for the controllers in Rails.&lt;br /&gt;
In our Cookbook application we generate Categories controller using the scaffold mechanism which populates the controller with the basic methods such as index, create, show, new, edit, update and destroy.&lt;br /&gt;
A simple example of index method is as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    @categories = Category.all&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # index.html.erb&lt;br /&gt;
      format.json { render json: @categories }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In ruby, @ operator is used to declare instance variables. The index method makes use of @categories instance variable which is populated with all the records in the Category table. The respond_to method is used for responding to particular type of request. It contains a block and a variable |format| which determines whether the incoming request should be responded with HTML (when we are interacting with a user) or JSON format (if we are returning an object). &lt;br /&gt;
After executing the code, the controller will render a template of the same name. (For this example, the template is views/categories/index.html.erb).&lt;br /&gt;
Similarly the show method is defined as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def show&lt;br /&gt;
    @category = Category.find(params[:id])&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # show.html.erb&lt;br /&gt;
      format.json { render json: @category }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The show method is used to display a particular object. The show method needs to access database to search for the given object. This is done using the find method which takes params[:id] as argument. The params[:id] contains the id or primary key of the object which is passed as a parameter to look up in the database and the result is stored in the instance variable. The result is displayed by rendering the show.html.erb file.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Active Record&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, a program deals with objects which are mapped into database relational databases. This kind of mapping is facilitated by the Active Record.&amp;lt;br&amp;gt;Active Record maps the Rails classes in to database tables and rails objects into database records.&lt;br /&gt;
This makes it easy to perform actions on database tables by invoking the class methods. Another benefit of Active Record is database migration. Migrations can manage the evolution of a schema used by several physical databases. It’s a solution to the common problem of adding a field to make a new feature work in your local database, but being unsure of how to push that change to other developers and to the production server. With migrations, you can describe the transformations in self-contained classes that can be checked into version control systems and executed against another database that might be one, two, or five versions behind.You can find a lot more details about rails migration here[8].&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Method Pairs&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;There are couple of method pairs such as new-create and edit-update which are executed in sequence. In method pair one method is responsible for display preparation and the other method validates the data and attempts it to save in the database.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def new&lt;br /&gt;
    @category = Category.new&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # new.html.erb&lt;br /&gt;
      format.json { render json: @category }&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def create&lt;br /&gt;
    @category = Category.new(params[:category])&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      if @category.save&lt;br /&gt;
        format.html { redirect_to @category, notice: 'Category was successfully created.' }&lt;br /&gt;
        format.json { render json: @category, status: :created, location: @category }&lt;br /&gt;
      else&lt;br /&gt;
        format.html { render action: &amp;quot;new&amp;quot; }&lt;br /&gt;
        format.json { render json: @category.errors, status: :unprocessable_entity }&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The new and create methods are known as method pairs which execute in sequence. The new method is used to instantiate a new Category object whereas create method is used to successfully save the object to the database. After execution of the new method, the controller renders the new.html.erb file which displays a form to create a new Category object. The user enters the required information in the form and the object id is returned in the URL on submitting the form. Consider the following URL is generated on submitting the form: http: //localhost:3000/categories/3.&lt;br /&gt;
This means that Category object with id =3 will be created. The details of the object will be processed by the create method. &lt;br /&gt;
The controller invokes the create method to store the object in the database. An observation can be made about the @category variable in the create method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@category = Category.new(params[:category])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The parameters passed in the URL after creating a new object is stored in the params hash (denoted by params[]). In this case the params[] contains an object with category_id = 3. The params[] assigns the data of object with category_id =3 to the instance variable. The data entered in the instance variable is validated using the @category.save method. If no errors are found then the object stored in the database and a message on successful creation of the object is displayed to the user. However, if the data entered in the object contains an error, the user is redirected to the new.html.erb page to resubmit the information and the errors are displayed. &lt;br /&gt;
Another example of such method pair is the edit and update methods. The edit method displays the table entry for an existing object, while the update method is invoked when the changes have been submitted.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Models of Cookbook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
Model handles the data processing in a web application. Actions interact with the model to manipulate the data. Rails is capable of handling databases and this is facilitated by the model. You can base models on the Rails ActiveRecord module which contains support databases.&lt;br /&gt;
In our Cookbook application we have two models namely: Category and Recipe. They are represented by files category.rb and recipe.rb and each file is used to describe the relation between two classes. Let’s take a look at the two models&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt; category.rb&amp;lt;/b&amp;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;
&amp;lt;b&amp;gt;recipe.rb&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;Relationships in models&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;one-to-one&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Syllabus &amp;lt; ActiveRecord::Base&lt;br /&gt;
	belongs_to :course¬&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Course &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_one :syllabus&lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;many-many&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Student &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_and_belongs_to_many :courses&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Course &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_and_belongs_to_many :students&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Cookbook, we have many-one relationship between recipes and categories.&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Views of Cookbook application&amp;lt;/h1&amp;gt;&lt;br /&gt;
What is V in MVC? ‘V’ refers to views. Views are the actual interface that users can see. It is through views, that a user may request some data to be viewed or submits some data for processing.&amp;lt;br&amp;gt;&lt;br /&gt;
Views in rails are of the form file_name.html.erb. Here erb means embedded ruby. What html.erb denotes is some ruby code is embedded in HTML code. Like HTML, embedded ruby also uses tags to represent data. However, to distinguish it from the HTML code, ruby is embedded between &amp;lt;% %&amp;gt; if some non-assignment ruby operation is to be performed. If something needs to be displayed on screen, &amp;lt;%= %&amp;gt; is used.&lt;br /&gt;
Views are present in “app/views” folder in the rails application.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Name of the view files is same as the name of their respective controller. For e.g.:- if I have a controller movie with an action new, the corresponding view page will be new.html.erb in the app/views/movie directory.&amp;lt;br&amp;gt;&lt;br /&gt;
The Cookbook application views are as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Recipe Controller Views&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
1. index.html.erb&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;nowiki&amp;gt;&amp;lt;h1&amp;gt;Listing categories&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
  &amp;lt;tr&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;Name&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;% @categories.each do |category| %&amp;gt;&lt;br /&gt;
  &amp;lt;tr&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= category.name %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Show', category %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Edit', edit_category_path(category) %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Destroy', category, method: :delete, data: { confirm: 'Are you sure?' } %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%= link_to 'New Category', new_category_path %&amp;gt;&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;% @recipes.each do |recipe| %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
Here @recipes is an instance variable of model recipe defined in index action of the controller ‘recipes’. It contains all the recipes currently present in our application. Then each recipe is taken from this instance variable and is assigned to the local variable recipe.  All the subsequent lines display the recipe details (name, description and instruction).&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%= link_to ‘Show’, recipe %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
link_to is an ruby operator which is used to redirect the control to some other page. ‘Show’ is displayed as it is on the page and is an hyperlink. It links to a page pointed by recipe.  The path is defined in the routes.rb file. &lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
2. _form.html.erb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is a partial. Partials are devices used for breaking the code in smaller, more manageable chunks. This helps in modularity and also maintain DRY principle[4]. With a partial, code for rendering a particular response is moved to its own file. So for e.g. we can use this same partial for creating and editing information, because the information involved is essentially the same.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= form_for(@recipe) do |f| %&amp;gt;&lt;br /&gt;
  &amp;lt;% if @recipe.errors.any? %&amp;gt;&lt;br /&gt;
    &amp;lt;div id=&amp;quot;error_explanation&amp;quot;&amp;gt;&lt;br /&gt;
      &amp;lt;h2&amp;gt;&amp;lt;%= pluralize(@recipe.errors.count, &amp;quot;error&amp;quot;) %&amp;gt; prohibited this recipe from being saved:&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      &amp;lt;ul&amp;gt;&lt;br /&gt;
      &amp;lt;% @recipe.errors.full_messages.each do |msg| %&amp;gt;&lt;br /&gt;
        &amp;lt;li&amp;gt;&amp;lt;%= msg %&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
      &amp;lt;% end %&amp;gt;&lt;br /&gt;
      &amp;lt;/ul&amp;gt;&lt;br /&gt;
    &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;% end %&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :name %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_field :name %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
    &amp;lt;%=  f.label :category %&amp;gt;&lt;br /&gt;
    &amp;lt;%=    select('recipe','category_id', Category.all.collect {|c| [c[:name],c[:id]]}) %&amp;gt;&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :description %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_field :description %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :instructions %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_area :instructions %&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.submit %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;&amp;lt;%= form_for(@recipe) do |f| %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
This statement means to render a form for the instance variable @recipe.&lt;br /&gt;
If this page is being used for creating a new recipe, then the values for each key in the @recipe hash will be empty. If it is used for editing a recipe, @recipe will contain information corresponding to the recipe being edited.&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;% if @recipe.errors.any? %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
While saving an recipe or updating a recipe in the database, some violations may occur. Like the recipe name is left empty or its description is empty and so on. So while saving this information, database will signal some error. The errors are sent back to this page and are caught in @recipe.errors.any? statement.&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%=    select('recipe','category_id', Category.all.collect {|c| [c[:name],c[:id]]}) %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
This statement creates a dropdown box.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;‘recipe’ : refers to the object to which the selected value is to be assigned. In this case , it is the recipe object&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;‘category_id’ : create a ‘category_id’ key in the recipe hash.&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;Category.all.collect {|c| [c[:name],c[:id]]} : recover all categories from the database and list them  in the dropdown box. The value for each name is c[:id]. Whenever a category is selected by the user, its corresponding id is assigned to the ‘category_id’ key in the ‘recipe’ object.&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
3. new.html.erb&amp;lt;br&amp;gt;&lt;br /&gt;
This page is used to create a new recipe. This page simply renders the partial described above.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;New recipe&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;%= render 'form' %&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%= render ‘form’ %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
This statement renders the partial.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
4. edit.html.erb&lt;br /&gt;
This page is used to edit a existing recipe. This page also renders the partial.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Editing recipe&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;%= render 'form' %&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Show', @recipe %&amp;gt; |&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
5. show.html.erb&lt;br /&gt;
This page is used to show a particular recipe requested by the user. The recipe requested is captured by the controller in the @recipe instance variable and is passed to this view.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;p id=&amp;quot;notice&amp;quot;&amp;gt;&amp;lt;%= notice %&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Name:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.name %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Description:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.description %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Instructions:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.instructions %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Edit', edit_recipe_path(@recipe) %&amp;gt; |&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;The Category Views are very similar to the recipe Views and can be created similarly.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Conclusion&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
MVC was first proposed by Trygve Reenskaug in 1979. He broke applications into three types of components: models, which is responsible for maintaining the state of the application; views, which are responsible for generating a user interface; and controllers, which orchestrate the applications by receiving events from outside world, interact with model, and display an appropriate view to user. This separation of concerns led to far less computing, and in turn made the code easier to write and maintain.&amp;lt;br&amp;gt;&lt;br /&gt;
Use Ruby on Rails can implement MVC to build various types of web applications. The Cookbook is an easy starter. Readers can learn the basics of Ruby on Rails from this page.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;References&amp;lt;/h1&amp;gt;&lt;br /&gt;
*[1] http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller&lt;br /&gt;
*[2] http://en.wikipedia.org/wiki/Ruby_on_Rails &lt;br /&gt;
*[3] Sam Ruby, Dave Thomas, David Heinemeier Hansson.agile web development with rails (fourth edition)&lt;br /&gt;
*[4] http://en.wikipedia.org/wiki/Don't_repeat_yourself&lt;br /&gt;
*[5] http://guides.rubyonrails.org/getting_started.html&lt;br /&gt;
*[6] http://api.rubyonrails.org/classes/ActiveRecord/Migration.html&lt;br /&gt;
*[7] http://en.wikipedia.org/wiki/Metaprogramming&lt;br /&gt;
*[8] http://net.tutsplus.com/tutorials/other/mvc-for-noobs/&lt;br /&gt;
*[9] Hotzner, S. (2007). Beginnning ruby on rail, Indianapolis: Wiley Publishing Inc&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The old wiki can be found here http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch4_4d_ch.&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w38_nm&amp;diff=67099</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w38 nm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w38_nm&amp;diff=67099"/>
		<updated>2012-10-04T01:42:26Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;Introduction&amp;lt;/h1&amp;gt;&lt;br /&gt;
This page is aimed at asserting the importance of Model View Controller Architecture and how MVC is implemented in Cookbook application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Model View Controller (MVC) Architecture&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The purpose of creating MVC architecture is to have clear separation between the responsibilities of business logic and representation. In a nutshell the responsibilities are mentioned as below:&lt;br /&gt;
&lt;br /&gt;
[[File:MVC.png|frame|none|MVC Responsibilities[8]]]&lt;br /&gt;
&lt;br /&gt;
Model-View-Controller (MVC)[1] is an architectural pattern used in software engineering.In complex computer applications that present a large amount of data to the user, a developer often wishes to separate data (model) and user interface (view) concerns, so that changes to the user interface will not affect data handling, and that the data can be reorganized without changing the user interface.&amp;lt;br&amp;gt;&lt;br /&gt;
The model-view-controller solves this problem by decoupling data access and business logic from data presentation and user interaction, by introducing an intermediate component: the controller. Having described the responsibilities of three components let’s take a look how these components interact with each other.&lt;br /&gt;
&lt;br /&gt;
[[File:MVC_architecture.PNG|frame|none|MVC Architecture[9]]] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Ruby on Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby on Rails[2] is an MVC framework. Rails enforces a structure for application, it helps to knits the models, views, and controllers as separate chunks of functionality while the program executes. No more external configuration needed. Rails is packed with features that make you more productive, with many of the following features building on one other.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Metaprogramming[7]:&amp;lt;/b&amp;gt; Other frameworks use extensive code generation from scratch. Metaprogramming[9] techniques use programs to write programs. Ruby is one of the best languages for metaprogramming, and Rails uses this capability well. Rails also uses code generation but relies much more on metaprogramming for the heavy lifting.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Active Record:&amp;lt;/b&amp;gt; Rails introduces the Active Record[6] framework, which saves objects to the database. The Rails version of Active Record discovers the columns in a database schema and automatically attaches them to your domain objects using metaprogramming.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Convention over configuration:&amp;lt;/b&amp;gt; Most web development frameworks for .NET or Java force you to write pages of configuration code. If you follow suggested naming conventions, Rails doesn't need much configuration.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Scaffolding:&amp;lt;/b&amp;gt; You often create temporary code in the early stages of development to help get an application up quickly and see how major components work together. Rails automatically creates much of the scaffolding you'll need.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Built-in testing:&amp;lt;/b&amp;gt; Rails creates simple automated tests you can then extend. Rails also provides supporting code called harnesses and fixtures that make test cases easier to write and run. Ruby can then execute all your automated tests with the rake utility.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Three environments:&amp;lt;/b&amp;gt; Rails gives you three default environments: development, testing, and production. Each behaves slightly differently, making your entire software development cycle easier. For example, Rails creates a fresh copy of the Test database for each test run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Cookbook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
Cookbook Application is a very basic Ruby on Rails application. It is a great way to understand the nuances that ‘Ruby’ has to offer. It is built keeping in mind that the reader is new to the world of ruby and is developing his/her first Ruby on Rails application.&lt;br /&gt;
The motive of this wiki page is to make the reader understand how the Cookbook application was built. And on the way to building this application, user will be introduced to some key concepts like Model, Views and Controllers. At the end of this page, reader must fairly be ready to start building his/her basic Ruby on Rails application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;A simple application to start -- Cookbook application&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Start a new project. While creating a new project, remember to select Rails Application option as follows&lt;br /&gt;
&lt;br /&gt;
[[File:Start_application.png|frame|none|Start a new Application]] &lt;br /&gt;
&lt;br /&gt;
When we create a Rails application, rails automatically help us create a brunch of files and folders as follows:&lt;br /&gt;
&lt;br /&gt;
[[File:Listing_of_cookbook_application.png|frame|none|Listing_of_cookbook_application]] &lt;br /&gt;
&lt;br /&gt;
We mainly use the app directory. This is where the heart of the application is stored i.e. Models, Views and controllers.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Using scaffold to create framework&amp;lt;/h2&amp;gt;&lt;br /&gt;
Rails scaffolding is a quick way to generate some of the major pieces of an application. If you want to create the models, views, and controllers for a new resource in a single operation, scaffolding is the tool for the job.&amp;lt;br&amp;gt;&lt;br /&gt;
In Rubymine, we can create a scaffold by right clicking on the current Project-&amp;gt;New-&amp;gt;Run Rails Generator -&amp;gt;scaffold. This will open a dialog box to create a scaffold where we can enter the name and the attributes along with their data type. Once the scaffold has been created RubyMine will generate the model, view and controller files respectively. These files do not contain any specific methods but they provide a framework which is useful for general applications. The user can modify the MVC behavior according to his application requirements. Each scaffold has create, read, update and delete methods declared in the controller class. A user can modify the behavior of the controller through these methods. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Create_Scaffold.png|frame|none|Creating a new scaffold]]&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Scaffold.png|frame|left|Scaffold]] [[File:New_Scaffold_Categories.png|frame|center|Creating scaffold for categories]]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Working without scaffold&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or you don't want to create a fully standard framework with scaffold, then you can use RubyMine generate command, you can create merely model, controller and view using generate command, there are many command included in the generate that you can use, expect those MVC, you can also crate migration, assets, intergration_test and performance_test. Or you don't want to create a empty controller and writing code all yourself you can also use scaffold_controller to create controller&lt;br /&gt;
Here is an example. You have create the Model and everything on your own without using the scaffolding in Ruby on Rails. You create the following class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Dog &amp;lt; ActiveRecord::Base &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
You can add a table Dogs. by&lt;br /&gt;
&amp;lt;pre&amp;gt;rails g migration CreateDogs&amp;lt;/pre&amp;gt;&lt;br /&gt;
which also a generate command&lt;br /&gt;
&lt;br /&gt;
In the Cookbook application we are considering a Cookbook which contains recipes for various dishes. The recipes have been divided into different categories, for example there is a recipe for ice tea which belongs to the category of beverages. There is a relationship which is created between a recipe and a category which can be expressed as an ice tea belongs to beverages. We can have many such recipes belonging to a single category, in this case many to one relationship is established. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Controllers for Cookbook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
The Cookbook application starts by entering a URL in the browser which sends the request to the web server. Since our web server supports Rails application, it will hand over the control to a Rails controller.&lt;br /&gt;
A controller ‘controls’ the entire application by responding to the incoming requests as needed. For example, consider the following URL http ://localhost:3000/categories/show which is decoded by the controller to know that its action named show has been invoked.&lt;br /&gt;
All the controllers inherit only the ApplicationController which in turn inherits the ActionController::Base class. ActionContoller module provides the basic support for the controllers in Rails.&lt;br /&gt;
In our Cookbook application we generate Categories controller using the scaffold mechanism which populates the controller with the basic methods such as index, create, show, new, edit, update and destroy.&lt;br /&gt;
A simple example of index method is as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    @categories = Category.all&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # index.html.erb&lt;br /&gt;
      format.json { render json: @categories }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In ruby, @ operator is used to declare instance variables. The index method makes use of @categories instance variable which is populated with all the records in the Category table. The respond_to method is used for responding to particular type of request. It contains a block and a variable |format| which determines whether the incoming request should be responded with HTML (when we are interacting with a user) or JSON format (if we are returning an object). &lt;br /&gt;
After executing the code, the controller will render a template of the same name. (For this example, the template is views/categories/index.html.erb).&lt;br /&gt;
Similarly the show method is defined as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def show&lt;br /&gt;
    @category = Category.find(params[:id])&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # show.html.erb&lt;br /&gt;
      format.json { render json: @category }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The show method is used to display a particular object. The show method needs to access database to search for the given object. This is done using the find method which takes params[:id] as argument. The params[:id] contains the id or primary key of the object which is passed as a parameter to look up in the database and the result is stored in the instance variable. The result is displayed by rendering the show.html.erb file.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Active Record&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, a program deals with objects which are mapped into database relational databases. This kind of mapping is facilitated by the Active Record.&amp;lt;br&amp;gt;Active Record maps the Rails classes in to database tables and rails objects into database records.&lt;br /&gt;
This makes it easy to perform actions on database tables by invoking the class methods. Another benefit of Active Record is database migration. Migrations can manage the evolution of a schema used by several physical databases. It’s a solution to the common problem of adding a field to make a new feature work in your local database, but being unsure of how to push that change to other developers and to the production server. With migrations, you can describe the transformations in self-contained classes that can be checked into version control systems and executed against another database that might be one, two, or five versions behind.You can find a lot more details about rails migration here[8].&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Method Pairs&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;There are couple of method pairs such as new-create and edit-update which are executed in sequence. In method pair one method is responsible for display preparation and the other method validates the data and attempts it to save in the database.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def new&lt;br /&gt;
    @category = Category.new&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # new.html.erb&lt;br /&gt;
      format.json { render json: @category }&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def create&lt;br /&gt;
    @category = Category.new(params[:category])&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      if @category.save&lt;br /&gt;
        format.html { redirect_to @category, notice: 'Category was successfully created.' }&lt;br /&gt;
        format.json { render json: @category, status: :created, location: @category }&lt;br /&gt;
      else&lt;br /&gt;
        format.html { render action: &amp;quot;new&amp;quot; }&lt;br /&gt;
        format.json { render json: @category.errors, status: :unprocessable_entity }&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The new and create methods are known as method pairs which execute in sequence. The new method is used to instantiate a new Category object whereas create method is used to successfully save the object to the database. After execution of the new method, the controller renders the new.html.erb file which displays a form to create a new Category object. The user enters the required information in the form and the object id is returned in the URL on submitting the form. Consider the following URL is generated on submitting the form: http: //localhost:3000/categories/3.&lt;br /&gt;
This means that Category object with id =3 will be created. The details of the object will be processed by the create method. &lt;br /&gt;
The controller invokes the create method to store the object in the database. An observation can be made about the @category variable in the create method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@category = Category.new(params[:category])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The parameters passed in the URL after creating a new object is stored in the params hash (denoted by params[]). In this case the params[] contains an object with category_id = 3. The params[] assigns the data of object with category_id =3 to the instance variable. The data entered in the instance variable is validated using the @category.save method. If no errors are found then the object stored in the database and a message on successful creation of the object is displayed to the user. However, if the data entered in the object contains an error, the user is redirected to the new.html.erb page to resubmit the information and the errors are displayed. &lt;br /&gt;
Another example of such method pair is the edit and update methods. The edit method displays the table entry for an existing object, while the update method is invoked when the changes have been submitted.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Models of Cookbook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
Model handles the data processing in a web application. Actions interact with the model to manipulate the data. Rails is capable of handling databases and this is facilitated by the model. You can base models on the Rails ActiveRecord module which contains support databases.&lt;br /&gt;
In our Cookbook application we have two models namely: Category and Recipe. They are represented by files category.rb and recipe.rb and each file is used to describe the relation between two classes. Let’s take a look at the two models&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt; category.rb&amp;lt;/b&amp;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;
&amp;lt;b&amp;gt;recipe.rb&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;Relationships in models&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;one-to-one&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Syllabus &amp;lt; ActiveRecord::Base&lt;br /&gt;
	belongs_to :course¬&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Course &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_one :syllabus&lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;many-many&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Student &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_and_belongs_to_many :courses&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Course &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_and_belongs_to_many :students&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Cookbook, we have many-one relationship between recipes and categories.&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Views of Cookbook application&amp;lt;/h1&amp;gt;&lt;br /&gt;
What is V in MVC? ‘V’ refers to views. Views are the actual interface that users can see. It is through views, that a user may request some data to be viewed or submits some data for processing.&amp;lt;br&amp;gt;&lt;br /&gt;
Views in rails are of the form file_name.html.erb. Here erb means embedded ruby. What html.erb denotes is some ruby code is embedded in HTML code. Like HTML, embedded ruby also uses tags to represent data. However, to distinguish it from the HTML code, ruby is embedded between &amp;lt;% %&amp;gt; if some non-assignment ruby operation is to be performed. If something needs to be displayed on screen, &amp;lt;%= %&amp;gt; is used.&lt;br /&gt;
Views are present in “app/views” folder in the rails application.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Name of the view files is same as the name of their respective controller. For e.g.:- if I have a controller movie with an action new, the corresponding view page will be new.html.erb in the app/views/movie directory.&amp;lt;br&amp;gt;&lt;br /&gt;
The Cookbook application views are as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Recipe Controller Views&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
1. index.html.erb&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;nowiki&amp;gt;&amp;lt;h1&amp;gt;Listing categories&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
  &amp;lt;tr&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;Name&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;% @categories.each do |category| %&amp;gt;&lt;br /&gt;
  &amp;lt;tr&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= category.name %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Show', category %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Edit', edit_category_path(category) %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Destroy', category, method: :delete, data: { confirm: 'Are you sure?' } %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%= link_to 'New Category', new_category_path %&amp;gt;&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;% @recipes.each do |recipe| %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
Here @recipes is an instance variable of model recipe defined in index action of the controller ‘recipes’. It contains all the recipes currently present in our application. Then each recipe is taken from this instance variable and is assigned to the local variable recipe.  All the subsequent lines display the recipe details (name, description and instruction).&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%= link_to ‘Show’, recipe %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
link_to is an ruby operator which is used to redirect the control to some other page. ‘Show’ is displayed as it is on the page and is an hyperlink. It links to a page pointed by recipe.  The path is defined in the routes.rb file. &lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
2. _form.html.erb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is a partial. Partials are devices used for breaking the code in smaller, more manageable chunks. This helps in modularity and also maintain DRY principle[4]. With a partial, code for rendering a particular response is moved to its own file. So for e.g. we can use this same partial for creating and editing information, because the information involved is essentially the same.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= form_for(@recipe) do |f| %&amp;gt;&lt;br /&gt;
  &amp;lt;% if @recipe.errors.any? %&amp;gt;&lt;br /&gt;
    &amp;lt;div id=&amp;quot;error_explanation&amp;quot;&amp;gt;&lt;br /&gt;
      &amp;lt;h2&amp;gt;&amp;lt;%= pluralize(@recipe.errors.count, &amp;quot;error&amp;quot;) %&amp;gt; prohibited this recipe from being saved:&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      &amp;lt;ul&amp;gt;&lt;br /&gt;
      &amp;lt;% @recipe.errors.full_messages.each do |msg| %&amp;gt;&lt;br /&gt;
        &amp;lt;li&amp;gt;&amp;lt;%= msg %&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
      &amp;lt;% end %&amp;gt;&lt;br /&gt;
      &amp;lt;/ul&amp;gt;&lt;br /&gt;
    &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;% end %&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :name %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_field :name %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
    &amp;lt;%=  f.label :category %&amp;gt;&lt;br /&gt;
    &amp;lt;%=    select('recipe','category_id', Category.all.collect {|c| [c[:name],c[:id]]}) %&amp;gt;&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :description %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_field :description %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :instructions %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_area :instructions %&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.submit %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;&amp;lt;%= form_for(@recipe) do |f| %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
This statement means to render a form for the instance variable @recipe.&lt;br /&gt;
If this page is being used for creating a new recipe, then the values for each key in the @recipe hash will be empty. If it is used for editing a recipe, @recipe will contain information corresponding to the recipe being edited.&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;% if @recipe.errors.any? %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
While saving an recipe or updating a recipe in the database, some violations may occur. Like the recipe name is left empty or its description is empty and so on. So while saving this information, database will signal some error. The errors are sent back to this page and are caught in @recipe.errors.any? statement.&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%=    select('recipe','category_id', Category.all.collect {|c| [c[:name],c[:id]]}) %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
This statement creates a dropdown box.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;‘recipe’ : refers to the object to which the selected value is to be assigned. In this case , it is the recipe object&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;‘category_id’ : create a ‘category_id’ key in the recipe hash.&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;Category.all.collect {|c| [c[:name],c[:id]]} : recover all categories from the database and list them  in the dropdown box. The value for each name is c[:id]. Whenever a category is selected by the user, its corresponding id is assigned to the ‘category_id’ key in the ‘recipe’ object.&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
3. new.html.erb&amp;lt;br&amp;gt;&lt;br /&gt;
This page is used to create a new recipe. This page simply renders the partial described above.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;New recipe&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;%= render 'form' %&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%= render ‘form’ %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
This statement renders the partial.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
4. edit.html.erb&lt;br /&gt;
This page is used to edit a existing recipe. This page also renders the partial.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Editing recipe&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;%= render 'form' %&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Show', @recipe %&amp;gt; |&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
5. show.html.erb&lt;br /&gt;
This page is used to show a particular recipe requested by the user. The recipe requested is captured by the controller in the @recipe instance variable and is passed to this view.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;p id=&amp;quot;notice&amp;quot;&amp;gt;&amp;lt;%= notice %&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Name:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.name %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Description:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.description %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Instructions:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.instructions %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Edit', edit_recipe_path(@recipe) %&amp;gt; |&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;The Category Views are very similar to the recipe Views and can be created similarly.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Conclusion&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
MVC was first proposed by Trygve Reenskaug in 1979. He broke applications into three types of components: models, which is responsible for maintaining the state of the application; views, which are responsible for generating a user interface; and controllers, which orchestrate the applications by receiving events from outside world, interact with model, and display an appropriate view to user. This separation of concerns led to far less computing, and in turn made the code easier to write and maintain.&amp;lt;br&amp;gt;&lt;br /&gt;
Use Ruby on Rails can implement MVC to build various types of web applications. The Cookbook is an easy starter. Readers can learn the basics of Ruby on Rails from this page.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;References&amp;lt;/h1&amp;gt;&lt;br /&gt;
*[1] http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller&lt;br /&gt;
*[2] http://en.wikipedia.org/wiki/Ruby_on_Rails &lt;br /&gt;
*[3] Sam Ruby, Dave Thomas, David Heinemeier Hansson.agile web development with rails (fourth edition)&lt;br /&gt;
*[4] http://en.wikipedia.org/wiki/Don't_repeat_yourself&lt;br /&gt;
*[5] http://guides.rubyonrails.org/getting_started.html&lt;br /&gt;
*[6] http://api.rubyonrails.org/classes/ActiveRecord/Migration.html&lt;br /&gt;
*[7] http://en.wikipedia.org/wiki/Metaprogramming&lt;br /&gt;
*[8] http://net.tutsplus.com/tutorials/other/mvc-for-noobs/&lt;br /&gt;
*[9] Hotzner, S. (2007). Beginnning ruby on rail, Indianapolis: Wiley Publishing Inc&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The old wiki can be found here http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch4_4d_ch.&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w38_nm&amp;diff=67096</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w38 nm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w38_nm&amp;diff=67096"/>
		<updated>2012-10-04T01:40:38Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;Introduction&amp;lt;/h1&amp;gt;&lt;br /&gt;
This page is aimed at asserting the importance of Model View Controller Architecture and how MVC is implemented in Cookbook application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Model View Controller (MVC) Architecture&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The purpose of creating MVC architecture is to have clear separation between the responsibilities of business logic and representation. In a nutshell the responsibilities are mentioned as below:&lt;br /&gt;
&lt;br /&gt;
[[File:MVC.png|frame|none|MVC Responsibilities[8]]]&lt;br /&gt;
&lt;br /&gt;
Model-View-Controller (MVC)[1] is an architectural pattern used in software engineering.In complex computer applications that present a large amount of data to the user, a developer often wishes to separate data (model) and user interface (view) concerns, so that changes to the user interface will not affect data handling, and that the data can be reorganized without changing the user interface.&amp;lt;br&amp;gt;&lt;br /&gt;
The model-view-controller solves this problem by decoupling data access and business logic from data presentation and user interaction, by introducing an intermediate component: the controller. Having described the responsibilities of three components let’s take a look how these components interact with each other.&lt;br /&gt;
&lt;br /&gt;
[[File:MVC_architecture.PNG|frame|none|MVC Architecture]] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Ruby on Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby on Rails[2] is an MVC framework. Rails enforces a structure for application, it helps to knits the models, views, and controllers as separate chunks of functionality while the program executes. No more external configuration needed. Rails is packed with features that make you more productive, with many of the following features building on one other.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Metaprogramming[7]:&amp;lt;/b&amp;gt; Other frameworks use extensive code generation from scratch. Metaprogramming[9] techniques use programs to write programs. Ruby is one of the best languages for metaprogramming, and Rails uses this capability well. Rails also uses code generation but relies much more on metaprogramming for the heavy lifting.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Active Record:&amp;lt;/b&amp;gt; Rails introduces the Active Record[6] framework, which saves objects to the database. The Rails version of Active Record discovers the columns in a database schema and automatically attaches them to your domain objects using metaprogramming.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Convention over configuration:&amp;lt;/b&amp;gt; Most web development frameworks for .NET or Java force you to write pages of configuration code. If you follow suggested naming conventions, Rails doesn't need much configuration.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Scaffolding:&amp;lt;/b&amp;gt; You often create temporary code in the early stages of development to help get an application up quickly and see how major components work together. Rails automatically creates much of the scaffolding you'll need.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Built-in testing:&amp;lt;/b&amp;gt; Rails creates simple automated tests you can then extend. Rails also provides supporting code called harnesses and fixtures that make test cases easier to write and run. Ruby can then execute all your automated tests with the rake utility.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Three environments:&amp;lt;/b&amp;gt; Rails gives you three default environments: development, testing, and production. Each behaves slightly differently, making your entire software development cycle easier. For example, Rails creates a fresh copy of the Test database for each test run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Cookbook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
Cookbook Application is a very basic Ruby on Rails application. It is a great way to understand the nuances that ‘Ruby’ has to offer. It is built keeping in mind that the reader is new to the world of ruby and is developing his/her first Ruby on Rails application.&lt;br /&gt;
The motive of this wiki page is to make the reader understand how the Cookbook application was built. And on the way to building this application, user will be introduced to some key concepts like Model, Views and Controllers. At the end of this page, reader must fairly be ready to start building his/her basic Ruby on Rails application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;A simple application to start -- Cookbook application&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Start a new project. While creating a new project, remember to select Rails Application option as follows&lt;br /&gt;
&lt;br /&gt;
[[File:Start_application.png|frame|none|Start a new Application]] &lt;br /&gt;
&lt;br /&gt;
When we create a Rails application, rails automatically help us create a brunch of files and folders as follows:&lt;br /&gt;
&lt;br /&gt;
[[File:Listing_of_cookbook_application.png|frame|none|Listing_of_cookbook_application]] &lt;br /&gt;
&lt;br /&gt;
We mainly use the app directory. This is where the heart of the application is stored i.e. Models, Views and controllers.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Using scaffold to create framework&amp;lt;/h2&amp;gt;&lt;br /&gt;
Rails scaffolding is a quick way to generate some of the major pieces of an application. If you want to create the models, views, and controllers for a new resource in a single operation, scaffolding is the tool for the job.&amp;lt;br&amp;gt;&lt;br /&gt;
In Rubymine, we can create a scaffold by right clicking on the current Project-&amp;gt;New-&amp;gt;Run Rails Generator -&amp;gt;scaffold. This will open a dialog box to create a scaffold where we can enter the name and the attributes along with their data type. Once the scaffold has been created RubyMine will generate the model, view and controller files respectively. These files do not contain any specific methods but they provide a framework which is useful for general applications. The user can modify the MVC behavior according to his application requirements. Each scaffold has create, read, update and delete methods declared in the controller class. A user can modify the behavior of the controller through these methods. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Create_Scaffold.png|frame|none|Creating a new scaffold]]&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Scaffold.png|frame|left|Scaffold]] [[File:New_Scaffold_Categories.png|frame|center|Creating scaffold for categories]]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Working without scaffold&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or you don't want to create a fully standard framework with scaffold, then you can use RubyMine generate command, you can create merely model, controller and view using generate command, there are many command included in the generate that you can use, expect those MVC, you can also crate migration, assets, intergration_test and performance_test. Or you don't want to create a empty controller and writing code all yourself you can also use scaffold_controller to create controller&lt;br /&gt;
Here is an example. You have create the Model and everything on your own without using the scaffolding in Ruby on Rails. You create the following class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Dog &amp;lt; ActiveRecord::Base &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
You can add a table Dogs. by&lt;br /&gt;
&amp;lt;pre&amp;gt;rails g migration CreateDogs&amp;lt;/pre&amp;gt;&lt;br /&gt;
which also a generate command&lt;br /&gt;
&lt;br /&gt;
In the Cookbook application we are considering a Cookbook which contains recipes for various dishes. The recipes have been divided into different categories, for example there is a recipe for ice tea which belongs to the category of beverages. There is a relationship which is created between a recipe and a category which can be expressed as an ice tea belongs to beverages. We can have many such recipes belonging to a single category, in this case many to one relationship is established. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Controllers for Cookbook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
The Cookbook application starts by entering a URL in the browser which sends the request to the web server. Since our web server supports Rails application, it will hand over the control to a Rails controller.&lt;br /&gt;
A controller ‘controls’ the entire application by responding to the incoming requests as needed. For example, consider the following URL http ://localhost:3000/categories/show which is decoded by the controller to know that its action named show has been invoked.&lt;br /&gt;
All the controllers inherit only the ApplicationController which in turn inherits the ActionController::Base class. ActionContoller module provides the basic support for the controllers in Rails.&lt;br /&gt;
In our Cookbook application we generate Categories controller using the scaffold mechanism which populates the controller with the basic methods such as index, create, show, new, edit, update and destroy.&lt;br /&gt;
A simple example of index method is as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    @categories = Category.all&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # index.html.erb&lt;br /&gt;
      format.json { render json: @categories }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In ruby, @ operator is used to declare instance variables. The index method makes use of @categories instance variable which is populated with all the records in the Category table. The respond_to method is used for responding to particular type of request. It contains a block and a variable |format| which determines whether the incoming request should be responded with HTML (when we are interacting with a user) or JSON format (if we are returning an object). &lt;br /&gt;
After executing the code, the controller will render a template of the same name. (For this example, the template is views/categories/index.html.erb).&lt;br /&gt;
Similarly the show method is defined as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def show&lt;br /&gt;
    @category = Category.find(params[:id])&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # show.html.erb&lt;br /&gt;
      format.json { render json: @category }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The show method is used to display a particular object. The show method needs to access database to search for the given object. This is done using the find method which takes params[:id] as argument. The params[:id] contains the id or primary key of the object which is passed as a parameter to look up in the database and the result is stored in the instance variable. The result is displayed by rendering the show.html.erb file.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Active Record&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, a program deals with objects which are mapped into database relational databases. This kind of mapping is facilitated by the Active Record.&amp;lt;br&amp;gt;Active Record maps the Rails classes in to database tables and rails objects into database records.&lt;br /&gt;
This makes it easy to perform actions on database tables by invoking the class methods. Another benefit of Active Record is database migration. Migrations can manage the evolution of a schema used by several physical databases. It’s a solution to the common problem of adding a field to make a new feature work in your local database, but being unsure of how to push that change to other developers and to the production server. With migrations, you can describe the transformations in self-contained classes that can be checked into version control systems and executed against another database that might be one, two, or five versions behind.You can find a lot more details about rails migration here[8].&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Method Pairs&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;There are couple of method pairs such as new-create and edit-update which are executed in sequence. In method pair one method is responsible for display preparation and the other method validates the data and attempts it to save in the database.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def new&lt;br /&gt;
    @category = Category.new&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # new.html.erb&lt;br /&gt;
      format.json { render json: @category }&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def create&lt;br /&gt;
    @category = Category.new(params[:category])&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      if @category.save&lt;br /&gt;
        format.html { redirect_to @category, notice: 'Category was successfully created.' }&lt;br /&gt;
        format.json { render json: @category, status: :created, location: @category }&lt;br /&gt;
      else&lt;br /&gt;
        format.html { render action: &amp;quot;new&amp;quot; }&lt;br /&gt;
        format.json { render json: @category.errors, status: :unprocessable_entity }&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The new and create methods are known as method pairs which execute in sequence. The new method is used to instantiate a new Category object whereas create method is used to successfully save the object to the database. After execution of the new method, the controller renders the new.html.erb file which displays a form to create a new Category object. The user enters the required information in the form and the object id is returned in the URL on submitting the form. Consider the following URL is generated on submitting the form: http: //localhost:3000/categories/3.&lt;br /&gt;
This means that Category object with id =3 will be created. The details of the object will be processed by the create method. &lt;br /&gt;
The controller invokes the create method to store the object in the database. An observation can be made about the @category variable in the create method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@category = Category.new(params[:category])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The parameters passed in the URL after creating a new object is stored in the params hash (denoted by params[]). In this case the params[] contains an object with category_id = 3. The params[] assigns the data of object with category_id =3 to the instance variable. The data entered in the instance variable is validated using the @category.save method. If no errors are found then the object stored in the database and a message on successful creation of the object is displayed to the user. However, if the data entered in the object contains an error, the user is redirected to the new.html.erb page to resubmit the information and the errors are displayed. &lt;br /&gt;
Another example of such method pair is the edit and update methods. The edit method displays the table entry for an existing object, while the update method is invoked when the changes have been submitted.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Models of Cookbook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
Model handles the data processing in a web application. Actions interact with the model to manipulate the data. Rails is capable of handling databases and this is facilitated by the model. You can base models on the Rails ActiveRecord module which contains support databases.&lt;br /&gt;
In our Cookbook application we have two models namely: Category and Recipe. They are represented by files category.rb and recipe.rb and each file is used to describe the relation between two classes. Let’s take a look at the two models&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt; category.rb&amp;lt;/b&amp;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;
&amp;lt;b&amp;gt;recipe.rb&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;Relationships in models&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;one-to-one&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Syllabus &amp;lt; ActiveRecord::Base&lt;br /&gt;
	belongs_to :course¬&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Course &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_one :syllabus&lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;many-many&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Student &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_and_belongs_to_many :courses&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Course &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_and_belongs_to_many :students&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Cookbook, we have many-one relationship between recipes and categories.&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Views of Cookbook application&amp;lt;/h1&amp;gt;&lt;br /&gt;
What is V in MVC? ‘V’ refers to views. Views are the actual interface that users can see. It is through views, that a user may request some data to be viewed or submits some data for processing.&amp;lt;br&amp;gt;&lt;br /&gt;
Views in rails are of the form file_name.html.erb. Here erb means embedded ruby. What html.erb denotes is some ruby code is embedded in HTML code. Like HTML, embedded ruby also uses tags to represent data. However, to distinguish it from the HTML code, ruby is embedded between &amp;lt;% %&amp;gt; if some non-assignment ruby operation is to be performed. If something needs to be displayed on screen, &amp;lt;%= %&amp;gt; is used.&lt;br /&gt;
Views are present in “app/views” folder in the rails application.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Name of the view files is same as the name of their respective controller. For e.g.:- if I have a controller movie with an action new, the corresponding view page will be new.html.erb in the app/views/movie directory.&amp;lt;br&amp;gt;&lt;br /&gt;
The Cookbook application views are as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Recipe Controller Views&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
1. index.html.erb&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;nowiki&amp;gt;&amp;lt;h1&amp;gt;Listing categories&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
  &amp;lt;tr&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;Name&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;% @categories.each do |category| %&amp;gt;&lt;br /&gt;
  &amp;lt;tr&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= category.name %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Show', category %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Edit', edit_category_path(category) %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Destroy', category, method: :delete, data: { confirm: 'Are you sure?' } %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%= link_to 'New Category', new_category_path %&amp;gt;&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;% @recipes.each do |recipe| %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
Here @recipes is an instance variable of model recipe defined in index action of the controller ‘recipes’. It contains all the recipes currently present in our application. Then each recipe is taken from this instance variable and is assigned to the local variable recipe.  All the subsequent lines display the recipe details (name, description and instruction).&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%= link_to ‘Show’, recipe %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
link_to is an ruby operator which is used to redirect the control to some other page. ‘Show’ is displayed as it is on the page and is an hyperlink. It links to a page pointed by recipe.  The path is defined in the routes.rb file. &lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
2. _form.html.erb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is a partial. Partials are devices used for breaking the code in smaller, more manageable chunks. This helps in modularity and also maintain DRY principle[4]. With a partial, code for rendering a particular response is moved to its own file. So for e.g. we can use this same partial for creating and editing information, because the information involved is essentially the same.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= form_for(@recipe) do |f| %&amp;gt;&lt;br /&gt;
  &amp;lt;% if @recipe.errors.any? %&amp;gt;&lt;br /&gt;
    &amp;lt;div id=&amp;quot;error_explanation&amp;quot;&amp;gt;&lt;br /&gt;
      &amp;lt;h2&amp;gt;&amp;lt;%= pluralize(@recipe.errors.count, &amp;quot;error&amp;quot;) %&amp;gt; prohibited this recipe from being saved:&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      &amp;lt;ul&amp;gt;&lt;br /&gt;
      &amp;lt;% @recipe.errors.full_messages.each do |msg| %&amp;gt;&lt;br /&gt;
        &amp;lt;li&amp;gt;&amp;lt;%= msg %&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
      &amp;lt;% end %&amp;gt;&lt;br /&gt;
      &amp;lt;/ul&amp;gt;&lt;br /&gt;
    &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;% end %&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :name %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_field :name %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
    &amp;lt;%=  f.label :category %&amp;gt;&lt;br /&gt;
    &amp;lt;%=    select('recipe','category_id', Category.all.collect {|c| [c[:name],c[:id]]}) %&amp;gt;&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :description %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_field :description %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :instructions %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_area :instructions %&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.submit %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;&amp;lt;%= form_for(@recipe) do |f| %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
This statement means to render a form for the instance variable @recipe.&lt;br /&gt;
If this page is being used for creating a new recipe, then the values for each key in the @recipe hash will be empty. If it is used for editing a recipe, @recipe will contain information corresponding to the recipe being edited.&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;% if @recipe.errors.any? %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
While saving an recipe or updating a recipe in the database, some violations may occur. Like the recipe name is left empty or its description is empty and so on. So while saving this information, database will signal some error. The errors are sent back to this page and are caught in @recipe.errors.any? statement.&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%=    select('recipe','category_id', Category.all.collect {|c| [c[:name],c[:id]]}) %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
This statement creates a dropdown box.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;‘recipe’ : refers to the object to which the selected value is to be assigned. In this case , it is the recipe object&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;‘category_id’ : create a ‘category_id’ key in the recipe hash.&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;Category.all.collect {|c| [c[:name],c[:id]]} : recover all categories from the database and list them  in the dropdown box. The value for each name is c[:id]. Whenever a category is selected by the user, its corresponding id is assigned to the ‘category_id’ key in the ‘recipe’ object.&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
3. new.html.erb&amp;lt;br&amp;gt;&lt;br /&gt;
This page is used to create a new recipe. This page simply renders the partial described above.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;New recipe&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;%= render 'form' %&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%= render ‘form’ %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
This statement renders the partial.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
4. edit.html.erb&lt;br /&gt;
This page is used to edit a existing recipe. This page also renders the partial.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Editing recipe&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;%= render 'form' %&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Show', @recipe %&amp;gt; |&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
5. show.html.erb&lt;br /&gt;
This page is used to show a particular recipe requested by the user. The recipe requested is captured by the controller in the @recipe instance variable and is passed to this view.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;p id=&amp;quot;notice&amp;quot;&amp;gt;&amp;lt;%= notice %&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Name:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.name %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Description:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.description %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Instructions:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.instructions %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Edit', edit_recipe_path(@recipe) %&amp;gt; |&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;The Category Views are very similar to the recipe Views and can be created similarly.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Conclusion&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
MVC was first proposed by Trygve Reenskaug in 1979. He broke applications into three types of components: models, which is responsible for maintaining the state of the application; views, which are responsible for generating a user interface; and controllers, which orchestrate the applications by receiving events from outside world, interact with model, and display an appropriate view to user. This separation of concerns led to far less computing, and in turn made the code easier to write and maintain.&amp;lt;br&amp;gt;&lt;br /&gt;
Use Ruby on Rails can implement MVC to build various types of web applications. The Cookbook is an easy starter. Readers can learn the basics of Ruby on Rails from this page.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;References&amp;lt;/h1&amp;gt;&lt;br /&gt;
*[1] http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller&lt;br /&gt;
*[2] http://en.wikipedia.org/wiki/Ruby_on_Rails &lt;br /&gt;
*[3] Sam Ruby, Dave Thomas, David Heinemeier Hansson.agile web development with rails (fourth edition)&lt;br /&gt;
*[4] http://en.wikipedia.org/wiki/Don't_repeat_yourself&lt;br /&gt;
*[5] http://guides.rubyonrails.org/getting_started.html&lt;br /&gt;
*[6] http://api.rubyonrails.org/classes/ActiveRecord/Migration.html&lt;br /&gt;
*[7] http://en.wikipedia.org/wiki/Metaprogramming&lt;br /&gt;
*[8] http://net.tutsplus.com/tutorials/other/mvc-for-noobs/&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The old wiki can be found here http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch4_4d_ch.&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012&amp;diff=67077</id>
		<title>CSC/ECE 517 Fall 2012</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012&amp;diff=67077"/>
		<updated>2012-10-04T01:31:34Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*[[CSC/ECE 517 Fall 2012/ch1 n xx]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w1 rk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w20 pp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w5 su]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w6 pp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w4 aj]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w7 am]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w8 aa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w9 av]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w10 pk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w11 ap]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w12 mv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w14 gv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w17 ir]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w18 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w22 an]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w21 aa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w21 wi]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w31 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w16 br]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w23 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w24 nr]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w15 rt]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w3 pl]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w32 cm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w37 ss]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w67 ks]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w27 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w29 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w33 op]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w19 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w34 vd]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w35 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w30 rp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w47 sk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w69 mv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w44 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w45 is]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w53 kc]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w40 ar]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w39 sn]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w54 go]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w56 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w64 nn]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w66 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w40 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w42 js]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w46 sm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w71 gs]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w63 dv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w55 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w57 mp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w52 an]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch1b 1w38 nm]]&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012&amp;diff=67076</id>
		<title>CSC/ECE 517 Fall 2012</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012&amp;diff=67076"/>
		<updated>2012-10-04T01:30:59Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*[[CSC/ECE 517 Fall 2012/ch1 n xx]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w1 rk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w20 pp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w5 su]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w6 pp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w4 aj]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w7 am]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w8 aa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w9 av]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w10 pk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w11 ap]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w12 mv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w14 gv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w17 ir]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w18 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w22 an]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w21 aa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w21 wi]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w31 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w16 br]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w23 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w24 nr]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w15 rt]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w3 pl]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w32 cm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w37 ss]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w67 ks]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w27 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w29 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w33 op]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w19 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w34 vd]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w35 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w30 rp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w47 sk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w69 mv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w44 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w45 is]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w53 kc]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w40 ar]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w39 sn]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w54 go]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w56 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w64 nn]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w66 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w40 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w42 js]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w46 sm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w71 gs]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w63 dv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w55 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w57 mp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w52 an]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch1b_1w38_nm]]&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w38_nm&amp;diff=67072</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w38 nm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w38_nm&amp;diff=67072"/>
		<updated>2012-10-04T01:30:25Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: Created page with &amp;quot;&amp;lt;h1&amp;gt;Introduction&amp;lt;/h1&amp;gt; This page is aimed at asserting the importance of Model View Controller Architecture and how MVC is implemented in Cookbook application.  &amp;lt;h2&amp;gt;Model View Con...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;Introduction&amp;lt;/h1&amp;gt;&lt;br /&gt;
This page is aimed at asserting the importance of Model View Controller Architecture and how MVC is implemented in Cookbook application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Model View Controller (MVC) Architecture&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The purpose of creating MVC architecture is to have clear separation between the responsibilities of business logic and representation. In a nutshell the responsibilities are mentioned as below:&lt;br /&gt;
&lt;br /&gt;
[[File:MVC.png|frame|none|MVC Responsibilities[8]]]&lt;br /&gt;
&lt;br /&gt;
Model-View-Controller (MVC)[1] is an architectural pattern used in software engineering.In complex computer applications that present a large amount of data to the user, a developer often wishes to separate data (model) and user interface (view) concerns, so that changes to the user interface will not affect data handling, and that the data can be reorganized without changing the user interface.&amp;lt;br&amp;gt;&lt;br /&gt;
The model-view-controller solves this problem by decoupling data access and business logic from data presentation and user interaction, by introducing an intermediate component: the controller. Having described the responsibilities of three components let’s take a look how these components interact with each other.&lt;br /&gt;
&lt;br /&gt;
[[File:MVC_architecture.PNG|frame|none|MVC Architecture]] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Ruby on Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby on Rails[2] is an MVC framework. Rails enforces a structure for application, it helps to knits the models, views, and controllers as separate chunks of functionality while the program executes. No more external configuration needed. Rails is packed with features that make you more productive, with many of the following features building on one other.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Metaprogramming[7]:&amp;lt;/b&amp;gt; Other frameworks use extensive code generation from scratch. Metaprogramming[9] techniques use programs to write programs. Ruby is one of the best languages for metaprogramming, and Rails uses this capability well. Rails also uses code generation but relies much more on metaprogramming for the heavy lifting.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Active Record:&amp;lt;/b&amp;gt; Rails introduces the Active Record[6] framework, which saves objects to the database. The Rails version of Active Record discovers the columns in a database schema and automatically attaches them to your domain objects using metaprogramming.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Convention over configuration:&amp;lt;/b&amp;gt; Most web development frameworks for .NET or Java force you to write pages of configuration code. If you follow suggested naming conventions, Rails doesn't need much configuration.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Scaffolding:&amp;lt;/b&amp;gt; You often create temporary code in the early stages of development to help get an application up quickly and see how major components work together. Rails automatically creates much of the scaffolding you'll need.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Built-in testing:&amp;lt;/b&amp;gt; Rails creates simple automated tests you can then extend. Rails also provides supporting code called harnesses and fixtures that make test cases easier to write and run. Ruby can then execute all your automated tests with the rake utility.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Three environments:&amp;lt;/b&amp;gt; Rails gives you three default environments: development, testing, and production. Each behaves slightly differently, making your entire software development cycle easier. For example, Rails creates a fresh copy of the Test database for each test run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Cookbook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
Cookbook Application is a very basic Ruby on Rails application. It is a great way to understand the nuances that ‘Ruby’ has to offer. It is built keeping in mind that the reader is new to the world of ruby and is developing his/her first Ruby on Rails application.&lt;br /&gt;
The motive of this wiki page is to make the reader understand how the Cookbook application was built. And on the way to building this application, user will be introduced to some key concepts like Model, Views and Controllers. At the end of this page, reader must fairly be ready to start building his/her basic Ruby on Rails application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;A simple application to start -- Cookbook application&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Start a new project. While creating a new project, remember to select Rails Application option as follows&lt;br /&gt;
&lt;br /&gt;
[[File:Start_application.png|frame|none|Start a new Application]] &lt;br /&gt;
&lt;br /&gt;
When we create a Rails application, rails automatically help us create a brunch of files and folders as follows:&lt;br /&gt;
&lt;br /&gt;
[[File:Listing_of_cookbook_application.png|frame|none|Listing_of_cookbook_application]] &lt;br /&gt;
&lt;br /&gt;
We mainly use the app directory. This is where the heart of the application is stored i.e. Models, Views and controllers.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Using scaffold to create framework&amp;lt;/h2&amp;gt;&lt;br /&gt;
Rails scaffolding is a quick way to generate some of the major pieces of an application. If you want to create the models, views, and controllers for a new resource in a single operation, scaffolding is the tool for the job.&amp;lt;br&amp;gt;&lt;br /&gt;
In Rubymine, we can create a scaffold by right clicking on the current Project-&amp;gt;New-&amp;gt;Run Rails Generator -&amp;gt;scaffold. This will open a dialog box to create a scaffold where we can enter the name and the attributes along with their data type. Once the scaffold has been created RubyMine will generate the model, view and controller files respectively. These files do not contain any specific methods but they provide a framework which is useful for general applications. The user can modify the MVC behavior according to his application requirements. Each scaffold has create, read, update and delete methods declared in the controller class. A user can modify the behavior of the controller through these methods. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Create_Scaffold.png|frame|none|Creating a new scaffold]]&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Scaffold.png|frame|left|Scaffold]] [[File:New_Scaffold_Categories.png|frame|center|Creating scaffold for categories]]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Working without scaffold&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or you don't want to create a fully standard framework with scaffold, then you can use RubyMine generate command, you can create merely model, controller and view using generate command, there are many command included in the generate that you can use, expect those MVC, you can also crate migration, assets, intergration_test and performance_test. Or you don't want to create a empty controller and writing code all yourself you can also use scaffold_controller to create controller&lt;br /&gt;
Here is an example. You have create the Model and everything on your own without using the scaffolding in Ruby on Rails. You create the following class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Dog &amp;lt; ActiveRecord::Base &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
You can add a table Dogs. by&lt;br /&gt;
&amp;lt;pre&amp;gt;rails g migration CreateDogs&amp;lt;/pre&amp;gt;&lt;br /&gt;
which also a generate command&lt;br /&gt;
&lt;br /&gt;
In the Cookbook application we are considering a Cookbook which contains recipes for various dishes. The recipes have been divided into different categories, for example there is a recipe for ice tea which belongs to the category of beverages. There is a relationship which is created between a recipe and a category which can be expressed as an ice tea belongs to beverages. We can have many such recipes belonging to a single category, in this case many to one relationship is established. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Controllers for Cookbook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
The Cookbook application starts by entering a URL in the browser which sends the request to the web server. Since our web server supports Rails application, it will hand over the control to a Rails controller.&lt;br /&gt;
A controller ‘controls’ the entire application by responding to the incoming requests as needed. For example, consider the following URL http ://localhost:3000/categories/show which is decoded by the controller to know that its action named show has been invoked.&lt;br /&gt;
All the controllers inherit only the ApplicationController which in turn inherits the ActionController::Base class. ActionContoller module provides the basic support for the controllers in Rails.&lt;br /&gt;
In our Cookbook application we generate Categories controller using the scaffold mechanism which populates the controller with the basic methods such as index, create, show, new, edit, update and destroy.&lt;br /&gt;
A simple example of index method is as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    @categories = Category.all&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # index.html.erb&lt;br /&gt;
      format.json { render json: @categories }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In ruby, @ operator is used to declare instance variables. The index method makes use of @categories instance variable which is populated with all the records in the Category table. The respond_to method is used for responding to particular type of request. It contains a block and a variable |format| which determines whether the incoming request should be responded with HTML (when we are interacting with a user) or JSON format (if we are returning an object). &lt;br /&gt;
After executing the code, the controller will render a template of the same name. (For this example, the template is views/categories/index.html.erb).&lt;br /&gt;
Similarly the show method is defined as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def show&lt;br /&gt;
    @category = Category.find(params[:id])&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # show.html.erb&lt;br /&gt;
      format.json { render json: @category }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The show method is used to display a particular object. The show method needs to access database to search for the given object. This is done using the find method which takes params[:id] as argument. The params[:id] contains the id or primary key of the object which is passed as a parameter to look up in the database and the result is stored in the instance variable. The result is displayed by rendering the show.html.erb file.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Active Record&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, a program deals with objects which are mapped into database relational databases. This kind of mapping is facilitated by the Active Record.&amp;lt;br&amp;gt;Active Record maps the Rails classes in to database tables and rails objects into database records.&lt;br /&gt;
This makes it easy to perform actions on database tables by invoking the class methods. Another benefit of Active Record is database migration. Migrations can manage the evolution of a schema used by several physical databases. It’s a solution to the common problem of adding a field to make a new feature work in your local database, but being unsure of how to push that change to other developers and to the production server. With migrations, you can describe the transformations in self-contained classes that can be checked into version control systems and executed against another database that might be one, two, or five versions behind.You can find a lot more details about rails migration here[8].&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Method Pairs&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;There are couple of method pairs such as new-create and edit-update which are executed in sequence. In method pair one method is responsible for display preparation and the other method validates the data and attempts it to save in the database.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def new&lt;br /&gt;
    @category = Category.new&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # new.html.erb&lt;br /&gt;
      format.json { render json: @category }&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def create&lt;br /&gt;
    @category = Category.new(params[:category])&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      if @category.save&lt;br /&gt;
        format.html { redirect_to @category, notice: 'Category was successfully created.' }&lt;br /&gt;
        format.json { render json: @category, status: :created, location: @category }&lt;br /&gt;
      else&lt;br /&gt;
        format.html { render action: &amp;quot;new&amp;quot; }&lt;br /&gt;
        format.json { render json: @category.errors, status: :unprocessable_entity }&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The new and create methods are known as method pairs which execute in sequence. The new method is used to instantiate a new Category object whereas create method is used to successfully save the object to the database. After execution of the new method, the controller renders the new.html.erb file which displays a form to create a new Category object. The user enters the required information in the form and the object id is returned in the URL on submitting the form. Consider the following URL is generated on submitting the form: http: //localhost:3000/categories/3.&lt;br /&gt;
This means that Category object with id =3 will be created. The details of the object will be processed by the create method. &lt;br /&gt;
The controller invokes the create method to store the object in the database. An observation can be made about the @category variable in the create method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@category = Category.new(params[:category])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The parameters passed in the URL after creating a new object is stored in the params hash (denoted by params[]). In this case the params[] contains an object with category_id = 3. The params[] assigns the data of object with category_id =3 to the instance variable. The data entered in the instance variable is validated using the @category.save method. If no errors are found then the object stored in the database and a message on successful creation of the object is displayed to the user. However, if the data entered in the object contains an error, the user is redirected to the new.html.erb page to resubmit the information and the errors are displayed. &lt;br /&gt;
Another example of such method pair is the edit and update methods. The edit method displays the table entry for an existing object, while the update method is invoked when the changes have been submitted.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Models of Cookbook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
Model handles the data processing in a web application. Actions interact with the model to manipulate the data. Rails is capable of handling databases and this is facilitated by the model. You can base models on the Rails ActiveRecord module which contains support databases.&lt;br /&gt;
In our Cookbook application we have two models namely: Category and Recipe. They are represented by files category.rb and recipe.rb and each file is used to describe the relation between two classes. Let’s take a look at the two models&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt; category.rb&amp;lt;/b&amp;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;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;recipe.rb&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;lt;b&amp;gt;Relationships in models&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;one-to-one&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Syllabus &amp;lt; ActiveRecord::Base&lt;br /&gt;
	belongs_to :course¬&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Course &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_one :syllabus&lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;many-many&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Student &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_and_belongs_to_many :courses&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Course &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_and_belongs_to_many :students&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In Cookbook, we have many-one relationship between recipes and categories.&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Views of Cookbook application&amp;lt;/h1&amp;gt;&lt;br /&gt;
What is V in MVC? ‘V’ refers to views. Views are the actual interface that users can see. It is through views, that a user may request some data to be viewed or submits some data for processing.&amp;lt;br&amp;gt;&lt;br /&gt;
Views in rails are of the form file_name.html.erb. Here erb means embedded ruby. What html.erb denotes is some ruby code is embedded in HTML code. Like HTML, embedded ruby also uses tags to represent data. However, to distinguish it from the HTML code, ruby is embedded between &amp;lt;% %&amp;gt; if some non-assignment ruby operation is to be performed. If something needs to be displayed on screen, &amp;lt;%= %&amp;gt; is used.&lt;br /&gt;
Views are present in “app/views” folder in the rails application.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Name of the view files is same as the name of their respective controller. For e.g.:- if I have a controller movie with an action new, the corresponding view page will be new.html.erb in the app/views/movie directory.&amp;lt;br&amp;gt;&lt;br /&gt;
The Cookbook application views are as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Recipe Controller Views&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
1. index.html.erb&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;nowiki&amp;gt;&amp;lt;h1&amp;gt;Listing categories&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
  &amp;lt;tr&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;Name&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;% @categories.each do |category| %&amp;gt;&lt;br /&gt;
  &amp;lt;tr&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= category.name %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Show', category %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Edit', edit_category_path(category) %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Destroy', category, method: :delete, data: { confirm: 'Are you sure?' } %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%= link_to 'New Category', new_category_path %&amp;gt;&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;% @recipes.each do |recipe| %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
Here @recipes is an instance variable of model recipe defined in index action of the controller ‘recipes’. It contains all the recipes currently present in our application. Then each recipe is taken from this instance variable and is assigned to the local variable recipe.  All the subsequent lines display the recipe details (name, description and instruction).&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%= link_to ‘Show’, recipe %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
link_to is an ruby operator which is used to redirect the control to some other page. ‘Show’ is displayed as it is on the page and is an hyperlink. It links to a page pointed by recipe.  The path is defined in the routes.rb file. &lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
2. _form.html.erb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is a partial. Partials are devices used for breaking the code in smaller, more manageable chunks. This helps in modularity and also maintain DRY principle[4]. With a partial, code for rendering a particular response is moved to its own file. So for e.g. we can use this same partial for creating and editing information, because the information involved is essentially the same.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= form_for(@recipe) do |f| %&amp;gt;&lt;br /&gt;
  &amp;lt;% if @recipe.errors.any? %&amp;gt;&lt;br /&gt;
    &amp;lt;div id=&amp;quot;error_explanation&amp;quot;&amp;gt;&lt;br /&gt;
      &amp;lt;h2&amp;gt;&amp;lt;%= pluralize(@recipe.errors.count, &amp;quot;error&amp;quot;) %&amp;gt; prohibited this recipe from being saved:&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      &amp;lt;ul&amp;gt;&lt;br /&gt;
      &amp;lt;% @recipe.errors.full_messages.each do |msg| %&amp;gt;&lt;br /&gt;
        &amp;lt;li&amp;gt;&amp;lt;%= msg %&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
      &amp;lt;% end %&amp;gt;&lt;br /&gt;
      &amp;lt;/ul&amp;gt;&lt;br /&gt;
    &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;% end %&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :name %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_field :name %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
    &amp;lt;%=  f.label :category %&amp;gt;&lt;br /&gt;
    &amp;lt;%=    select('recipe','category_id', Category.all.collect {|c| [c[:name],c[:id]]}) %&amp;gt;&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :description %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_field :description %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :instructions %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_area :instructions %&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.submit %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;&amp;lt;%= form_for(@recipe) do |f| %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
This statement means to render a form for the instance variable @recipe.&lt;br /&gt;
If this page is being used for creating a new recipe, then the values for each key in the @recipe hash will be empty. If it is used for editing a recipe, @recipe will contain information corresponding to the recipe being edited.&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;% if @recipe.errors.any? %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
While saving an recipe or updating a recipe in the database, some violations may occur. Like the recipe name is left empty or its description is empty and so on. So while saving this information, database will signal some error. The errors are sent back to this page and are caught in @recipe.errors.any? statement.&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%=    select('recipe','category_id', Category.all.collect {|c| [c[:name],c[:id]]}) %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
This statement creates a dropdown box.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;‘recipe’ : refers to the object to which the selected value is to be assigned. In this case , it is the recipe object&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;‘category_id’ : create a ‘category_id’ key in the recipe hash.&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;Category.all.collect {|c| [c[:name],c[:id]]} : recover all categories from the database and list them  in the dropdown box. The value for each name is c[:id]. Whenever a category is selected by the user, its corresponding id is assigned to the ‘category_id’ key in the ‘recipe’ object.&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
3. new.html.erb&amp;lt;br&amp;gt;&lt;br /&gt;
This page is used to create a new recipe. This page simply renders the partial described above.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;New recipe&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;%= render 'form' %&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%= render ‘form’ %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
This statement renders the partial.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
4. edit.html.erb&lt;br /&gt;
This page is used to edit a existing recipe. This page also renders the partial.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Editing recipe&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;%= render 'form' %&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Show', @recipe %&amp;gt; |&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
5. show.html.erb&lt;br /&gt;
This page is used to show a particular recipe requested by the user. The recipe requested is captured by the controller in the @recipe instance variable and is passed to this view.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;p id=&amp;quot;notice&amp;quot;&amp;gt;&amp;lt;%= notice %&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Name:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.name %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Description:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.description %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Instructions:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.instructions %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Edit', edit_recipe_path(@recipe) %&amp;gt; |&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;The Category Views are very similar to the recipe Views and can be created similarly.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Conclusion&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
MVC was first proposed by Trygve Reenskaug in 1979. He broke applications into three types of components: models, which is responsible for maintaining the state of the application; views, which are responsible for generating a user interface; and controllers, which orchestrate the applications by receiving events from outside world, interact with model, and display an appropriate view to user. This separation of concerns led to far less computing, and in turn made the code easier to write and maintain.&amp;lt;br&amp;gt;&lt;br /&gt;
Use Ruby on Rails can implement MVC to build various types of web applications. The Cookbook is an easy starter. Readers can learn the basics of Ruby on Rails from this page.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;References&amp;lt;/h1&amp;gt;&lt;br /&gt;
*[1] http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller&lt;br /&gt;
*[2] http://en.wikipedia.org/wiki/Ruby_on_Rails &lt;br /&gt;
*[3] Sam Ruby, Dave Thomas, David Heinemeier Hansson.agile web development with rails (fourth edition)&lt;br /&gt;
*[4] http://en.wikipedia.org/wiki/Don't_repeat_yourself&lt;br /&gt;
*[5] http://guides.rubyonrails.org/getting_started.html&lt;br /&gt;
*[6] http://api.rubyonrails.org/classes/ActiveRecord/Migration.html&lt;br /&gt;
*[7] http://en.wikipedia.org/wiki/Metaprogramming&lt;br /&gt;
*[8] http://net.tutsplus.com/tutorials/other/mvc-for-noobs/&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The old wiki can be found here http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch4_4d_ch.&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w38&amp;diff=67067</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w38</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w38&amp;diff=67067"/>
		<updated>2012-10-04T01:27:52Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;Introduction&amp;lt;/h1&amp;gt;&lt;br /&gt;
This page is aimed at asserting the importance of Model View Controller Architecture and how MVC is implemented in Cookbook application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Model View Controller (MVC) Architecture&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The purpose of creating MVC architecture is to have clear separation between the responsibilities of business logic and representation. In a nutshell the responsibilities are mentioned as below:&lt;br /&gt;
&lt;br /&gt;
[[File:MVC.png|frame|none|MVC Responsibilities[8]]]&lt;br /&gt;
&lt;br /&gt;
Model-View-Controller (MVC)[1] is an architectural pattern used in software engineering.In complex computer applications that present a large amount of data to the user, a developer often wishes to separate data (model) and user interface (view) concerns, so that changes to the user interface will not affect data handling, and that the data can be reorganized without changing the user interface.&amp;lt;br&amp;gt;&lt;br /&gt;
The model-view-controller solves this problem by decoupling data access and business logic from data presentation and user interaction, by introducing an intermediate component: the controller. Having described the responsibilities of three components let’s take a look how these components interact with each other.&lt;br /&gt;
&lt;br /&gt;
[[File:MVC_architecture.PNG|frame|none|MVC Architecture]] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Ruby on Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby on Rails[2] is an MVC framework. Rails enforces a structure for application, it helps to knits the models, views, and controllers as separate chunks of functionality while the program executes. No more external configuration needed. Rails is packed with features that make you more productive, with many of the following features building on one other.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Metaprogramming[7]:&amp;lt;/b&amp;gt; Other frameworks use extensive code generation from scratch. Metaprogramming[9] techniques use programs to write programs. Ruby is one of the best languages for metaprogramming, and Rails uses this capability well. Rails also uses code generation but relies much more on metaprogramming for the heavy lifting.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Active Record:&amp;lt;/b&amp;gt; Rails introduces the Active Record[6] framework, which saves objects to the database. The Rails version of Active Record discovers the columns in a database schema and automatically attaches them to your domain objects using metaprogramming.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Convention over configuration:&amp;lt;/b&amp;gt; Most web development frameworks for .NET or Java force you to write pages of configuration code. If you follow suggested naming conventions, Rails doesn't need much configuration.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Scaffolding:&amp;lt;/b&amp;gt; You often create temporary code in the early stages of development to help get an application up quickly and see how major components work together. Rails automatically creates much of the scaffolding you'll need.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Built-in testing:&amp;lt;/b&amp;gt; Rails creates simple automated tests you can then extend. Rails also provides supporting code called harnesses and fixtures that make test cases easier to write and run. Ruby can then execute all your automated tests with the rake utility.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Three environments:&amp;lt;/b&amp;gt; Rails gives you three default environments: development, testing, and production. Each behaves slightly differently, making your entire software development cycle easier. For example, Rails creates a fresh copy of the Test database for each test run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Cookbook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
Cookbook Application is a very basic Ruby on Rails application. It is a great way to understand the nuances that ‘Ruby’ has to offer. It is built keeping in mind that the reader is new to the world of ruby and is developing his/her first Ruby on Rails application.&lt;br /&gt;
The motive of this wiki page is to make the reader understand how the Cookbook application was built. And on the way to building this application, user will be introduced to some key concepts like Model, Views and Controllers. At the end of this page, reader must fairly be ready to start building his/her basic Ruby on Rails application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;A simple application to start -- Cookbook application&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Start a new project. While creating a new project, remember to select Rails Application option as follows&lt;br /&gt;
&lt;br /&gt;
[[File:Start_application.png|frame|none|Start a new Application]] &lt;br /&gt;
&lt;br /&gt;
When we create a Rails application, rails automatically help us create a brunch of files and folders as follows:&lt;br /&gt;
&lt;br /&gt;
[[File:Listing_of_cookbook_application.png|frame|none|Listing_of_cookbook_application]] &lt;br /&gt;
&lt;br /&gt;
We mainly use the app directory. This is where the heart of the application is stored i.e. Models, Views and controllers.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Using scaffold to create framework&amp;lt;/h2&amp;gt;&lt;br /&gt;
Rails scaffolding is a quick way to generate some of the major pieces of an application. If you want to create the models, views, and controllers for a new resource in a single operation, scaffolding is the tool for the job.&amp;lt;br&amp;gt;&lt;br /&gt;
In Rubymine, we can create a scaffold by right clicking on the current Project-&amp;gt;New-&amp;gt;Run Rails Generator -&amp;gt;scaffold. This will open a dialog box to create a scaffold where we can enter the name and the attributes along with their data type. Once the scaffold has been created RubyMine will generate the model, view and controller files respectively. These files do not contain any specific methods but they provide a framework which is useful for general applications. The user can modify the MVC behavior according to his application requirements. Each scaffold has create, read, update and delete methods declared in the controller class. A user can modify the behavior of the controller through these methods. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Create_Scaffold.png|frame|none|Creating a new scaffold]]&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Scaffold.png|frame|left|Scaffold]] [[File:New_Scaffold_Categories.png|frame|center|Creating scaffold for categories]]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Working without scaffold&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or you don't want to create a fully standard framework with scaffold, then you can use RubyMine generate command, you can create merely model, controller and view using generate command, there are many command included in the generate that you can use, expect those MVC, you can also crate migration, assets, intergration_test and performance_test. Or you don't want to create a empty controller and writing code all yourself you can also use scaffold_controller to create controller&lt;br /&gt;
Here is an example. You have create the Model and everything on your own without using the scaffolding in Ruby on Rails. You create the following class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Dog &amp;lt; ActiveRecord::Base &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
You can add a table Dogs. by&lt;br /&gt;
&amp;lt;pre&amp;gt;rails g migration CreateDogs&amp;lt;/pre&amp;gt;&lt;br /&gt;
which also a generate command&lt;br /&gt;
&lt;br /&gt;
In the Cookbook application we are considering a Cookbook which contains recipes for various dishes. The recipes have been divided into different categories, for example there is a recipe for ice tea which belongs to the category of beverages. There is a relationship which is created between a recipe and a category which can be expressed as an ice tea belongs to beverages. We can have many such recipes belonging to a single category, in this case many to one relationship is established. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Controllers for Cookbook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
The Cookbook application starts by entering a URL in the browser which sends the request to the web server. Since our web server supports Rails application, it will hand over the control to a Rails controller.&lt;br /&gt;
A controller ‘controls’ the entire application by responding to the incoming requests as needed. For example, consider the following URL http ://localhost:3000/categories/show which is decoded by the controller to know that its action named show has been invoked.&lt;br /&gt;
All the controllers inherit only the ApplicationController which in turn inherits the ActionController::Base class. ActionContoller module provides the basic support for the controllers in Rails.&lt;br /&gt;
In our Cookbook application we generate Categories controller using the scaffold mechanism which populates the controller with the basic methods such as index, create, show, new, edit, update and destroy.&lt;br /&gt;
A simple example of index method is as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    @categories = Category.all&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # index.html.erb&lt;br /&gt;
      format.json { render json: @categories }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In ruby, @ operator is used to declare instance variables. The index method makes use of @categories instance variable which is populated with all the records in the Category table. The respond_to method is used for responding to particular type of request. It contains a block and a variable |format| which determines whether the incoming request should be responded with HTML (when we are interacting with a user) or JSON format (if we are returning an object). &lt;br /&gt;
After executing the code, the controller will render a template of the same name. (For this example, the template is views/categories/index.html.erb).&lt;br /&gt;
Similarly the show method is defined as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def show&lt;br /&gt;
    @category = Category.find(params[:id])&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # show.html.erb&lt;br /&gt;
      format.json { render json: @category }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The show method is used to display a particular object. The show method needs to access database to search for the given object. This is done using the find method which takes params[:id] as argument. The params[:id] contains the id or primary key of the object which is passed as a parameter to look up in the database and the result is stored in the instance variable. The result is displayed by rendering the show.html.erb file.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Active Record&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, a program deals with objects which are mapped into database relational databases. This kind of mapping is facilitated by the Active Record.&amp;lt;br&amp;gt;Active Record maps the Rails classes in to database tables and rails objects into database records.&lt;br /&gt;
This makes it easy to perform actions on database tables by invoking the class methods. Another benefit of Active Record is database migration. Migrations can manage the evolution of a schema used by several physical databases. It’s a solution to the common problem of adding a field to make a new feature work in your local database, but being unsure of how to push that change to other developers and to the production server. With migrations, you can describe the transformations in self-contained classes that can be checked into version control systems and executed against another database that might be one, two, or five versions behind.You can find a lot more details about rails migration here[8].&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Method Pairs&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;There are couple of method pairs such as new-create and edit-update which are executed in sequence. In method pair one method is responsible for display preparation and the other method validates the data and attempts it to save in the database.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def new&lt;br /&gt;
    @category = Category.new&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # new.html.erb&lt;br /&gt;
      format.json { render json: @category }&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def create&lt;br /&gt;
    @category = Category.new(params[:category])&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      if @category.save&lt;br /&gt;
        format.html { redirect_to @category, notice: 'Category was successfully created.' }&lt;br /&gt;
        format.json { render json: @category, status: :created, location: @category }&lt;br /&gt;
      else&lt;br /&gt;
        format.html { render action: &amp;quot;new&amp;quot; }&lt;br /&gt;
        format.json { render json: @category.errors, status: :unprocessable_entity }&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The new and create methods are known as method pairs which execute in sequence. The new method is used to instantiate a new Category object whereas create method is used to successfully save the object to the database. After execution of the new method, the controller renders the new.html.erb file which displays a form to create a new Category object. The user enters the required information in the form and the object id is returned in the URL on submitting the form. Consider the following URL is generated on submitting the form: http: //localhost:3000/categories/3.&lt;br /&gt;
This means that Category object with id =3 will be created. The details of the object will be processed by the create method. &lt;br /&gt;
The controller invokes the create method to store the object in the database. An observation can be made about the @category variable in the create method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@category = Category.new(params[:category])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The parameters passed in the URL after creating a new object is stored in the params hash (denoted by params[]). In this case the params[] contains an object with category_id = 3. The params[] assigns the data of object with category_id =3 to the instance variable. The data entered in the instance variable is validated using the @category.save method. If no errors are found then the object stored in the database and a message on successful creation of the object is displayed to the user. However, if the data entered in the object contains an error, the user is redirected to the new.html.erb page to resubmit the information and the errors are displayed. &lt;br /&gt;
Another example of such method pair is the edit and update methods. The edit method displays the table entry for an existing object, while the update method is invoked when the changes have been submitted.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Models of Cookbook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
Model handles the data processing in a web application. Actions interact with the model to manipulate the data. Rails is capable of handling databases and this is facilitated by the model. You can base models on the Rails ActiveRecord module which contains support databases.&lt;br /&gt;
In our Cookbook application we have two models namely: Category and Recipe. They are represented by files category.rb and recipe.rb and each file is used to describe the relation between two classes. Let’s take a look at the two models&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt; category.rb&amp;lt;/b&amp;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;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;recipe.rb&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;lt;b&amp;gt;Relationships in models&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;one-to-one&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Syllabus &amp;lt; ActiveRecord::Base&lt;br /&gt;
	belongs_to :course¬&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Course &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_one :syllabus&lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;many-many&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Student &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_and_belongs_to_many :courses&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Course &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_and_belongs_to_many :students&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In Cookbook, we have many-one relationship between recipes and categories.&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Views of Cookbook application&amp;lt;/h1&amp;gt;&lt;br /&gt;
What is V in MVC? ‘V’ refers to views. Views are the actual interface that users can see. It is through views, that a user may request some data to be viewed or submits some data for processing.&amp;lt;br&amp;gt;&lt;br /&gt;
Views in rails are of the form file_name.html.erb. Here erb means embedded ruby. What html.erb denotes is some ruby code is embedded in HTML code. Like HTML, embedded ruby also uses tags to represent data. However, to distinguish it from the HTML code, ruby is embedded between &amp;lt;% %&amp;gt; if some non-assignment ruby operation is to be performed. If something needs to be displayed on screen, &amp;lt;%= %&amp;gt; is used.&lt;br /&gt;
Views are present in “app/views” folder in the rails application.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Name of the view files is same as the name of their respective controller. For e.g.:- if I have a controller movie with an action new, the corresponding view page will be new.html.erb in the app/views/movie directory.&amp;lt;br&amp;gt;&lt;br /&gt;
The Cookbook application views are as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Recipe Controller Views&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
1. index.html.erb&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;nowiki&amp;gt;&amp;lt;h1&amp;gt;Listing categories&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
  &amp;lt;tr&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;Name&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;% @categories.each do |category| %&amp;gt;&lt;br /&gt;
  &amp;lt;tr&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= category.name %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Show', category %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Edit', edit_category_path(category) %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Destroy', category, method: :delete, data: { confirm: 'Are you sure?' } %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%= link_to 'New Category', new_category_path %&amp;gt;&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;% @recipes.each do |recipe| %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
Here @recipes is an instance variable of model recipe defined in index action of the controller ‘recipes’. It contains all the recipes currently present in our application. Then each recipe is taken from this instance variable and is assigned to the local variable recipe.  All the subsequent lines display the recipe details (name, description and instruction).&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%= link_to ‘Show’, recipe %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
link_to is an ruby operator which is used to redirect the control to some other page. ‘Show’ is displayed as it is on the page and is an hyperlink. It links to a page pointed by recipe.  The path is defined in the routes.rb file. &lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
2. _form.html.erb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is a partial. Partials are devices used for breaking the code in smaller, more manageable chunks. This helps in modularity and also maintain DRY principle[4]. With a partial, code for rendering a particular response is moved to its own file. So for e.g. we can use this same partial for creating and editing information, because the information involved is essentially the same.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= form_for(@recipe) do |f| %&amp;gt;&lt;br /&gt;
  &amp;lt;% if @recipe.errors.any? %&amp;gt;&lt;br /&gt;
    &amp;lt;div id=&amp;quot;error_explanation&amp;quot;&amp;gt;&lt;br /&gt;
      &amp;lt;h2&amp;gt;&amp;lt;%= pluralize(@recipe.errors.count, &amp;quot;error&amp;quot;) %&amp;gt; prohibited this recipe from being saved:&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      &amp;lt;ul&amp;gt;&lt;br /&gt;
      &amp;lt;% @recipe.errors.full_messages.each do |msg| %&amp;gt;&lt;br /&gt;
        &amp;lt;li&amp;gt;&amp;lt;%= msg %&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
      &amp;lt;% end %&amp;gt;&lt;br /&gt;
      &amp;lt;/ul&amp;gt;&lt;br /&gt;
    &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;% end %&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :name %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_field :name %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
    &amp;lt;%=  f.label :category %&amp;gt;&lt;br /&gt;
    &amp;lt;%=    select('recipe','category_id', Category.all.collect {|c| [c[:name],c[:id]]}) %&amp;gt;&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :description %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_field :description %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :instructions %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_area :instructions %&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.submit %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;&amp;lt;%= form_for(@recipe) do |f| %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
This statement means to render a form for the instance variable @recipe.&lt;br /&gt;
If this page is being used for creating a new recipe, then the values for each key in the @recipe hash will be empty. If it is used for editing a recipe, @recipe will contain information corresponding to the recipe being edited.&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;% if @recipe.errors.any? %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
While saving an recipe or updating a recipe in the database, some violations may occur. Like the recipe name is left empty or its description is empty and so on. So while saving this information, database will signal some error. The errors are sent back to this page and are caught in @recipe.errors.any? statement.&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%=    select('recipe','category_id', Category.all.collect {|c| [c[:name],c[:id]]}) %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
This statement creates a dropdown box.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;‘recipe’ : refers to the object to which the selected value is to be assigned. In this case , it is the recipe object&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;‘category_id’ : create a ‘category_id’ key in the recipe hash.&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;Category.all.collect {|c| [c[:name],c[:id]]} : recover all categories from the database and list them  in the dropdown box. The value for each name is c[:id]. Whenever a category is selected by the user, its corresponding id is assigned to the ‘category_id’ key in the ‘recipe’ object.&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
3. new.html.erb&amp;lt;br&amp;gt;&lt;br /&gt;
This page is used to create a new recipe. This page simply renders the partial described above.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;New recipe&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;%= render 'form' %&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%= render ‘form’ %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
This statement renders the partial.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
4. edit.html.erb&lt;br /&gt;
This page is used to edit a existing recipe. This page also renders the partial.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Editing recipe&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;%= render 'form' %&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Show', @recipe %&amp;gt; |&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
5. show.html.erb&lt;br /&gt;
This page is used to show a particular recipe requested by the user. The recipe requested is captured by the controller in the @recipe instance variable and is passed to this view.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;p id=&amp;quot;notice&amp;quot;&amp;gt;&amp;lt;%= notice %&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Name:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.name %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Description:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.description %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Instructions:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.instructions %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Edit', edit_recipe_path(@recipe) %&amp;gt; |&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;The Category Views are very similar to the recipe Views and can be created similarly.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Conclusion&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
MVC was first proposed by Trygve Reenskaug in 1979. He broke applications into three types of components: models, which is responsible for maintaining the state of the application; views, which are responsible for generating a user interface; and controllers, which orchestrate the applications by receiving events from outside world, interact with model, and display an appropriate view to user. This separation of concerns led to far less computing, and in turn made the code easier to write and maintain.&amp;lt;br&amp;gt;&lt;br /&gt;
Use Ruby on Rails can implement MVC to build various types of web applications. The Cookbook is an easy starter. Readers can learn the basics of Ruby on Rails from this page.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;References&amp;lt;/h1&amp;gt;&lt;br /&gt;
*[1] http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller&lt;br /&gt;
*[2] http://en.wikipedia.org/wiki/Ruby_on_Rails &lt;br /&gt;
*[3] Sam Ruby, Dave Thomas, David Heinemeier Hansson.agile web development with rails (fourth edition)&lt;br /&gt;
*[4] http://en.wikipedia.org/wiki/Don't_repeat_yourself&lt;br /&gt;
*[5] http://guides.rubyonrails.org/getting_started.html&lt;br /&gt;
*[6] http://api.rubyonrails.org/classes/ActiveRecord/Migration.html&lt;br /&gt;
*[7] http://en.wikipedia.org/wiki/Metaprogramming&lt;br /&gt;
*[8] http://net.tutsplus.com/tutorials/other/mvc-for-noobs/&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The old wiki can be found here http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch4_4d_ch.&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w38&amp;diff=66989</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w38</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w38&amp;diff=66989"/>
		<updated>2012-10-04T01:10:44Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;Introduction&amp;lt;/h1&amp;gt;&lt;br /&gt;
This page is aimed at asserting the importance of Model View Controller Architecture and how MVC is implemented in Cookbook application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Model View Controller (MVC) Architecture&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The purpose of creating MVC architecture is to have clear separation between the responsibilities of business logic and representation. In a nutshell the responsibilities are mentioned as below:&lt;br /&gt;
&lt;br /&gt;
[[File:MVC.png|MVC Responsibilities]] [8]&lt;br /&gt;
&lt;br /&gt;
Model-View-Controller (MVC)[1] is an architectural pattern used in software engineering.In complex computer applications that present a large amount of data to the user, a developer often wishes to separate data (model) and user interface (view) concerns, so that changes to the user interface will not affect data handling, and that the data can be reorganized without changing the user interface.&amp;lt;br&amp;gt;&lt;br /&gt;
The model-view-controller solves this problem by decoupling data access and business logic from data presentation and user interaction, by introducing an intermediate component: the controller. Having described the responsibilities of three components let’s take a look how these components interact with each other.&lt;br /&gt;
&lt;br /&gt;
[[File:MVC_architecture.PNG|MVC Architecture]] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Ruby on Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby on Rails[2] is an MVC framework. Rails enforces a structure for application, it helps to knits the models, views, and controllers as separate chunks of functionality while the program executes. No more external configuration needed. Rails is packed with features that make you more productive, with many of the following features building on one other.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Metaprogramming[7]:&amp;lt;/b&amp;gt; Other frameworks use extensive code generation from scratch. Metaprogramming[9] techniques use programs to write programs. Ruby is one of the best languages for metaprogramming, and Rails uses this capability well. Rails also uses code generation but relies much more on metaprogramming for the heavy lifting.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Active Record:&amp;lt;/b&amp;gt; Rails introduces the Active Record[6] framework, which saves objects to the database. The Rails version of Active Record discovers the columns in a database schema and automatically attaches them to your domain objects using metaprogramming.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Convention over configuration:&amp;lt;/b&amp;gt; Most web development frameworks for .NET or Java force you to write pages of configuration code. If you follow suggested naming conventions, Rails doesn't need much configuration.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Scaffolding:&amp;lt;/b&amp;gt; You often create temporary code in the early stages of development to help get an application up quickly and see how major components work together. Rails automatically creates much of the scaffolding you'll need.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Built-in testing:&amp;lt;/b&amp;gt; Rails creates simple automated tests you can then extend. Rails also provides supporting code called harnesses and fixtures that make test cases easier to write and run. Ruby can then execute all your automated tests with the rake utility.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Three environments:&amp;lt;/b&amp;gt; Rails gives you three default environments: development, testing, and production. Each behaves slightly differently, making your entire software development cycle easier. For example, Rails creates a fresh copy of the Test database for each test run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Cookbook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
Cookbook Application is a very basic Ruby on Rails application. It is a great way to understand the nuances that ‘Ruby’ has to offer. It is built keeping in mind that the reader is new to the world of ruby and is developing his/her first Ruby on Rails application.&lt;br /&gt;
The motive of this wiki page is to make the reader understand how the Cookbook application was built. And on the way to building this application, user will be introduced to some key concepts like Model, Views and Controllers. At the end of this page, reader must fairly be ready to start building his/her basic Ruby on Rails application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;A simple application to start -- Cookbook application&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Start a new project. While creating a new project, remember to select Rails Application option as follows&lt;br /&gt;
&lt;br /&gt;
[[File:Start_application.png|Start a new Application]] &lt;br /&gt;
&lt;br /&gt;
When we create a Rails application, rails automatically help us create a brunch of files and folders as follows:&lt;br /&gt;
&lt;br /&gt;
[[File:Listing_of_cookbook_application.png|Listing_of_cookbook_application]] &lt;br /&gt;
&lt;br /&gt;
We mainly use the app directory. This is where the heart of the application is stored i.e. Models, Views and controllers.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Using scaffold to create framework&amp;lt;/h2&amp;gt;&lt;br /&gt;
Rails scaffolding is a quick way to generate some of the major pieces of an application. If you want to create the models, views, and controllers for a new resource in a single operation, scaffolding is the tool for the job.&amp;lt;br&amp;gt;&lt;br /&gt;
In Rubymine, we can create a scaffold by right clicking on the current Project-&amp;gt;New-&amp;gt;Run Rails Generator -&amp;gt;scaffold. This will open a dialog box to create a scaffold where we can enter the name and the attributes along with their data type. Once the scaffold has been created RubyMine will generate the model, view and controller files respectively. These files do not contain any specific methods but they provide a framework which is useful for general applications. The user can modify the MVC behavior according to his application requirements. Each scaffold has create, read, update and delete methods declared in the controller class. A user can modify the behavior of the controller through these methods. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;[[File:Create_Scaffold.png|text-bottom|thumb|Creating a new scaffold]]&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Scaffold.png|Scaffold]]          [[File:New_Scaffold_Categories.png|Creating scaffold for categories]]&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Working without scaffold&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or you don't want to create a fully standard framework with scaffold, then you can use RubyMine generate command, you can create merely model, controller and view using generate command, there are many command included in the generate that you can use, expect those MVC, you can also crate migration, assets, intergration_test and performance_test. Or you don't want to create a empty controller and writing code all yourself you can also use scaffold_controller to create controller&lt;br /&gt;
Here is an example. You have create the Model and everything on your own without using the scaffolding in Ruby on Rails. You create the following class:&lt;br /&gt;
&lt;br /&gt;
In the Cookbook application we are considering a Cookbook which contains recipes for various dishes. The recipes have been divided into different categories, for example there is a recipe for ice tea which belongs to the category of beverages. There is a relationship which is created between a recipe and a category which can be expressed as an ice tea belongs to beverages. We can have many such recipes belonging to a single category, in this case many to one relationship is established. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Controllers for Cookbook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
The Cookbook application starts by entering a URL in the browser which sends the request to the web server. Since our web server supports Rails application, it will hand over the control to a Rails controller.&lt;br /&gt;
A controller ‘controls’ the entire application by responding to the incoming requests as needed. For example, consider the following URL http ://localhost:3000/categories/show which is decoded by the controller to know that its action named show has been invoked.&lt;br /&gt;
All the controllers inherit only the ApplicationController which in turn inherits the ActionController::Base class. ActionContoller module provides the basic support for the controllers in Rails.&lt;br /&gt;
In our Cookbook application we generate Categories controller using the scaffold mechanism which populates the controller with the basic methods such as index, create, show, new, edit, update and destroy.&lt;br /&gt;
A simple example of index method is as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    @categories = Category.all&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # index.html.erb&lt;br /&gt;
      format.json { render json: @categories }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In ruby, @ operator is used to declare instance variables. The index method makes use of @categories instance variable which is populated with all the records in the Category table. The respond_to method is used for responding to particular type of request. It contains a block and a variable |format| which determines whether the incoming request should be responded with HTML (when we are interacting with a user) or JSON format (if we are returning an object). &lt;br /&gt;
After executing the code, the controller will render a template of the same name. (For this example, the template is views/categories/index.html.erb).&lt;br /&gt;
Similarly the show method is defined as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def show&lt;br /&gt;
    @category = Category.find(params[:id])&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # show.html.erb&lt;br /&gt;
      format.json { render json: @category }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The show method is used to display a particular object. The show method needs to access database to search for the given object. This is done using the find method which takes params[:id] as argument. The params[:id] contains the id or primary key of the object which is passed as a parameter to look up in the database and the result is stored in the instance variable. The result is displayed by rendering the show.html.erb file.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Active Record&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, a program deals with objects which are mapped into database relational databases. This kind of mapping is facilitated by the Active Record.&amp;lt;br&amp;gt;Active Record maps the Rails classes in to database tables and rails objects into database records.&lt;br /&gt;
This makes it easy to perform actions on database tables by invoking the class methods. Another benefit of Active Record is database migration. Migrations can manage the evolution of a schema used by several physical databases. It’s a solution to the common problem of adding a field to make a new feature work in your local database, but being unsure of how to push that change to other developers and to the production server. With migrations, you can describe the transformations in self-contained classes that can be checked into version control systems and executed against another database that might be one, two, or five versions behind.You can find a lot more details about rails migration here[8].&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Method Pairs&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;There are couple of method pairs such as new-create and edit-update which are executed in sequence. In method pair one method is responsible for display preparation and the other method validates the data and attempts it to save in the database.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def new&lt;br /&gt;
    @category = Category.new&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # new.html.erb&lt;br /&gt;
      format.json { render json: @category }&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def create&lt;br /&gt;
    @category = Category.new(params[:category])&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      if @category.save&lt;br /&gt;
        format.html { redirect_to @category, notice: 'Category was successfully created.' }&lt;br /&gt;
        format.json { render json: @category, status: :created, location: @category }&lt;br /&gt;
      else&lt;br /&gt;
        format.html { render action: &amp;quot;new&amp;quot; }&lt;br /&gt;
        format.json { render json: @category.errors, status: :unprocessable_entity }&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The new and create methods are known as method pairs which execute in sequence. The new method is used to instantiate a new Category object whereas create method is used to successfully save the object to the database. After execution of the new method, the controller renders the new.html.erb file which displays a form to create a new Category object. The user enters the required information in the form and the object id is returned in the URL on submitting the form. Consider the following URL is generated on submitting the form: http: //localhost:3000/categories/3.&lt;br /&gt;
This means that Category object with id =3 will be created. The details of the object will be processed by the create method. &lt;br /&gt;
The controller invokes the create method to store the object in the database. An observation can be made about the @category variable in the create method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@category = Category.new(params[:category])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The parameters passed in the URL after creating a new object is stored in the params hash (denoted by params[]). In this case the params[] contains an object with category_id = 3. The params[] assigns the data of object with category_id =3 to the instance variable. The data entered in the instance variable is validated using the @category.save method. If no errors are found then the object stored in the database and a message on successful creation of the object is displayed to the user. However, if the data entered in the object contains an error, the user is redirected to the new.html.erb page to resubmit the information and the errors are displayed. &lt;br /&gt;
Another example of such method pair is the edit and update methods. The edit method displays the table entry for an existing object, while the update method is invoked when the changes have been submitted.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Models of Cookbook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
Model handles the data processing in a web application. Actions interact with the model to manipulate the data. Rails is capable of handling databases and this is facilitated by the model. You can base models on the Rails ActiveRecord module which contains support databases.&lt;br /&gt;
In our Cookbook application we have two models namely: Category and Recipe. They are represented by files category.rb and recipe.rb and each file is used to describe the relation between two classes. Let’s take a look at the two models&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt; category.rb&amp;lt;/b&amp;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;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;recipe.rb&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;lt;b&amp;gt;Relationships in models&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;one-to-one&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Syllabus &amp;lt; ActiveRecord::Base&lt;br /&gt;
	belongs_to :course¬&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Course &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_one :syllabus&lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;many-many&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Student &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_and_belongs_to_many :courses&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Course &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_and_belongs_to_many :students&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In Cookbook, we have many-one relationship between recipes and categories.&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Views of Cookbook application&amp;lt;/h1&amp;gt;&lt;br /&gt;
What is V in MVC? ‘V’ refers to views. Views are the actual interface that users can see. It is through views, that a user may request some data to be viewed or submits some data for processing.&amp;lt;br&amp;gt;&lt;br /&gt;
Views in rails are of the form file_name.html.erb. Here erb means embedded ruby. What html.erb denotes is some ruby code is embedded in HTML code. Like HTML, embedded ruby also uses tags to represent data. However, to distinguish it from the HTML code, ruby is embedded between &amp;lt;% %&amp;gt; if some non-assignment ruby operation is to be performed. If something needs to be displayed on screen, &amp;lt;%= %&amp;gt; is used.&lt;br /&gt;
Views are present in “app/views” folder in the rails application.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Name of the view files is same as the name of their respective controller. For e.g.:- if I have a controller movie with an action new, the corresponding view page will be new.html.erb in the app/views/movie directory.&amp;lt;br&amp;gt;&lt;br /&gt;
The Cookbook application views are as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Recipe Controller Views&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
1. index.html.erb&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;nowiki&amp;gt;&amp;lt;h1&amp;gt;Listing categories&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
  &amp;lt;tr&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;Name&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;% @categories.each do |category| %&amp;gt;&lt;br /&gt;
  &amp;lt;tr&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= category.name %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Show', category %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Edit', edit_category_path(category) %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Destroy', category, method: :delete, data: { confirm: 'Are you sure?' } %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%= link_to 'New Category', new_category_path %&amp;gt;&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;% @recipes.each do |recipe| %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
Here @recipes is an instance variable of model recipe defined in index action of the controller ‘recipes’. It contains all the recipes currently present in our application. Then each recipe is taken from this instance variable and is assigned to the local variable recipe.  All the subsequent lines display the recipe details (name, description and instruction).&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%= link_to ‘Show’, recipe %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
link_to is an ruby operator which is used to redirect the control to some other page. ‘Show’ is displayed as it is on the page and is an hyperlink. It links to a page pointed by recipe.  The path is defined in the routes.rb file. &lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
2. _form.html.erb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is a partial. Partials are devices used for breaking the code in smaller, more manageable chunks. This helps in modularity and also maintain DRY principle[4]. With a partial, code for rendering a particular response is moved to its own file. So for e.g. we can use this same partial for creating and editing information, because the information involved is essentially the same.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= form_for(@recipe) do |f| %&amp;gt;&lt;br /&gt;
  &amp;lt;% if @recipe.errors.any? %&amp;gt;&lt;br /&gt;
    &amp;lt;div id=&amp;quot;error_explanation&amp;quot;&amp;gt;&lt;br /&gt;
      &amp;lt;h2&amp;gt;&amp;lt;%= pluralize(@recipe.errors.count, &amp;quot;error&amp;quot;) %&amp;gt; prohibited this recipe from being saved:&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      &amp;lt;ul&amp;gt;&lt;br /&gt;
      &amp;lt;% @recipe.errors.full_messages.each do |msg| %&amp;gt;&lt;br /&gt;
        &amp;lt;li&amp;gt;&amp;lt;%= msg %&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
      &amp;lt;% end %&amp;gt;&lt;br /&gt;
      &amp;lt;/ul&amp;gt;&lt;br /&gt;
    &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;% end %&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :name %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_field :name %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
    &amp;lt;%=  f.label :category %&amp;gt;&lt;br /&gt;
    &amp;lt;%=    select('recipe','category_id', Category.all.collect {|c| [c[:name],c[:id]]}) %&amp;gt;&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :description %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_field :description %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :instructions %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_area :instructions %&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.submit %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;&amp;lt;%= form_for(@recipe) do |f| %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
This statement means to render a form for the instance variable @recipe.&lt;br /&gt;
If this page is being used for creating a new recipe, then the values for each key in the @recipe hash will be empty. If it is used for editing a recipe, @recipe will contain information corresponding to the recipe being edited.&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;% if @recipe.errors.any? %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
While saving an recipe or updating a recipe in the database, some violations may occur. Like the recipe name is left empty or its description is empty and so on. So while saving this information, database will signal some error. The errors are sent back to this page and are caught in @recipe.errors.any? statement.&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%=    select('recipe','category_id', Category.all.collect {|c| [c[:name],c[:id]]}) %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
This statement creates a dropdown box.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;‘recipe’ : refers to the object to which the selected value is to be assigned. In this case , it is the recipe object&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;‘category_id’ : create a ‘category_id’ key in the recipe hash.&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;Category.all.collect {|c| [c[:name],c[:id]]} : recover all categories from the database and list them  in the dropdown box. The value for each name is c[:id]. Whenever a category is selected by the user, its corresponding id is assigned to the ‘category_id’ key in the ‘recipe’ object.&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
3. new.html.erb&amp;lt;br&amp;gt;&lt;br /&gt;
This page is used to create a new recipe. This page simply renders the partial described above.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;New recipe&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;%= render 'form' %&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%= render ‘form’ %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
This statement renders the partial.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
4. edit.html.erb&lt;br /&gt;
This page is used to edit a existing recipe. This page also renders the partial.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Editing recipe&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;%= render 'form' %&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Show', @recipe %&amp;gt; |&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
5. show.html.erb&lt;br /&gt;
This page is used to show a particular recipe requested by the user. The recipe requested is captured by the controller in the @recipe instance variable and is passed to this view.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;p id=&amp;quot;notice&amp;quot;&amp;gt;&amp;lt;%= notice %&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Name:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.name %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Description:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.description %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Instructions:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.instructions %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Edit', edit_recipe_path(@recipe) %&amp;gt; |&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;The Category Views are very similar to the recipe Views and can be created similarly.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Conclusion&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
MVC was first proposed by Trygve Reenskaug in 1979. He broke applications into three types of components: models, which is responsible for maintaining the state of the application; views, which are responsible for generating a user interface; and controllers, which orchestrate the applications by receiving events from outside world, interact with model, and display an appropriate view to user. This separation of concerns led to far less computing, and in turn made the code easier to write and maintain.&amp;lt;br&amp;gt;&lt;br /&gt;
Use Ruby on Rails can implement MVC to build various types of web applications. The Cookbook is an easy starter. Readers can learn the basics of Ruby on Rails from this page.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;References&amp;lt;/h1&amp;gt;&lt;br /&gt;
*[1] http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller&lt;br /&gt;
*[2] http://en.wikipedia.org/wiki/Ruby_on_Rails &lt;br /&gt;
*[3] Sam Ruby, Dave Thomas, David Heinemeier Hansson.agile web development with rails (fourth edition)&lt;br /&gt;
*[4] http://en.wikipedia.org/wiki/Don't_repeat_yourself&lt;br /&gt;
*[5] http://guides.rubyonrails.org/getting_started.html&lt;br /&gt;
*[6] http://api.rubyonrails.org/classes/ActiveRecord/Migration.html&lt;br /&gt;
*[7] http://en.wikipedia.org/wiki/Metaprogramming&lt;br /&gt;
*[8] http://net.tutsplus.com/tutorials/other/mvc-for-noobs/&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The old wiki can be found here http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch4_4d_ch.&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w38&amp;diff=66986</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w38</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w38&amp;diff=66986"/>
		<updated>2012-10-04T01:09:40Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;Introduction&amp;lt;/h1&amp;gt;&lt;br /&gt;
This page is aimed at asserting the importance of Model View Controller Architecture and how MVC is implemented in Cookbook application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Model View Controller (MVC) Architecture&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The purpose of creating MVC architecture is to have clear separation between the responsibilities of business logic and representation. In a nutshell the responsibilities are mentioned as below:&lt;br /&gt;
&lt;br /&gt;
[[File:MVC.png|MVC Responsibilities]] [8]&lt;br /&gt;
&lt;br /&gt;
Model-View-Controller (MVC)[1] is an architectural pattern used in software engineering.In complex computer applications that present a large amount of data to the user, a developer often wishes to separate data (model) and user interface (view) concerns, so that changes to the user interface will not affect data handling, and that the data can be reorganized without changing the user interface.&amp;lt;br&amp;gt;&lt;br /&gt;
The model-view-controller solves this problem by decoupling data access and business logic from data presentation and user interaction, by introducing an intermediate component: the controller. Having described the responsibilities of three components let’s take a look how these components interact with each other.&lt;br /&gt;
&lt;br /&gt;
[[File:MVC_architecture.PNG|MVC Architecture]] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Ruby on Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby on Rails[2] is an MVC framework. Rails enforces a structure for application, it helps to knits the models, views, and controllers as separate chunks of functionality while the program executes. No more external configuration needed. Rails is packed with features that make you more productive, with many of the following features building on one other.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Metaprogramming[7]:&amp;lt;/b&amp;gt; Other frameworks use extensive code generation from scratch. Metaprogramming[9] techniques use programs to write programs. Ruby is one of the best languages for metaprogramming, and Rails uses this capability well. Rails also uses code generation but relies much more on metaprogramming for the heavy lifting.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Active Record:&amp;lt;/b&amp;gt; Rails introduces the Active Record[6] framework, which saves objects to the database. The Rails version of Active Record discovers the columns in a database schema and automatically attaches them to your domain objects using metaprogramming.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Convention over configuration:&amp;lt;/b&amp;gt; Most web development frameworks for .NET or Java force you to write pages of configuration code. If you follow suggested naming conventions, Rails doesn't need much configuration.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Scaffolding:&amp;lt;/b&amp;gt; You often create temporary code in the early stages of development to help get an application up quickly and see how major components work together. Rails automatically creates much of the scaffolding you'll need.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Built-in testing:&amp;lt;/b&amp;gt; Rails creates simple automated tests you can then extend. Rails also provides supporting code called harnesses and fixtures that make test cases easier to write and run. Ruby can then execute all your automated tests with the rake utility.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Three environments:&amp;lt;/b&amp;gt; Rails gives you three default environments: development, testing, and production. Each behaves slightly differently, making your entire software development cycle easier. For example, Rails creates a fresh copy of the Test database for each test run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Cookbook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
Cookbook Application is a very basic Ruby on Rails application. It is a great way to understand the nuances that ‘Ruby’ has to offer. It is built keeping in mind that the reader is new to the world of ruby and is developing his/her first Ruby on Rails application.&lt;br /&gt;
The motive of this wiki page is to make the reader understand how the Cookbook application was built. And on the way to building this application, user will be introduced to some key concepts like Model, Views and Controllers. At the end of this page, reader must fairly be ready to start building his/her basic Ruby on Rails application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;A simple application to start -- Cookbook application&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Start a new project. While creating a new project, remember to select Rails Application option as follows&lt;br /&gt;
&lt;br /&gt;
[[File:Start_application.png|Start a new Application]] &lt;br /&gt;
&lt;br /&gt;
When we create a Rails application, rails automatically help us create a brunch of files and folders as follows:&lt;br /&gt;
&lt;br /&gt;
[[File:Listing_of_cookbook_application.png|Listing_of_cookbook_application]] &lt;br /&gt;
&lt;br /&gt;
We mainly use the app directory. This is where the heart of the application is stored i.e. Models, Views and controllers.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Using scaffold to create framework&amp;lt;/h2&amp;gt;&lt;br /&gt;
Rails scaffolding is a quick way to generate some of the major pieces of an application. If you want to create the models, views, and controllers for a new resource in a single operation, scaffolding is the tool for the job.&amp;lt;br&amp;gt;&lt;br /&gt;
In Rubymine, we can create a scaffold by right clicking on the current Project-&amp;gt;New-&amp;gt;Run Rails Generator -&amp;gt;scaffold. This will open a dialog box to create a scaffold where we can enter the name and the attributes along with their data type. Once the scaffold has been created RubyMine will generate the model, view and controller files respectively. These files do not contain any specific methods but they provide a framework which is useful for general applications. The user can modify the MVC behavior according to his application requirements. Each scaffold has create, read, update and delete methods declared in the controller class. A user can modify the behavior of the controller through these methods. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;[[File:Create_Scaffold.png|thumb|Creating a new scaffold]]&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Scaffold.png|Scaffold]]          [[File:New_Scaffold_Categories.png|Creating scaffold for categories]]&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Working without scaffold&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or you don't want to create a fully standard framework with scaffold, then you can use RubyMine generate command, you can create merely model, controller and view using generate command, there are many command included in the generate that you can use, expect those MVC, you can also crate migration, assets, intergration_test and performance_test. Or you don't want to create a empty controller and writing code all yourself you can also use scaffold_controller to create controller&lt;br /&gt;
Here is an example. You have create the Model and everything on your own without using the scaffolding in Ruby on Rails. You create the following class:&lt;br /&gt;
&lt;br /&gt;
In the Cookbook application we are considering a Cookbook which contains recipes for various dishes. The recipes have been divided into different categories, for example there is a recipe for ice tea which belongs to the category of beverages. There is a relationship which is created between a recipe and a category which can be expressed as an ice tea belongs to beverages. We can have many such recipes belonging to a single category, in this case many to one relationship is established. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Controllers for Cookbook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
The Cookbook application starts by entering a URL in the browser which sends the request to the web server. Since our web server supports Rails application, it will hand over the control to a Rails controller.&lt;br /&gt;
A controller ‘controls’ the entire application by responding to the incoming requests as needed. For example, consider the following URL http ://localhost:3000/categories/show which is decoded by the controller to know that its action named show has been invoked.&lt;br /&gt;
All the controllers inherit only the ApplicationController which in turn inherits the ActionController::Base class. ActionContoller module provides the basic support for the controllers in Rails.&lt;br /&gt;
In our Cookbook application we generate Categories controller using the scaffold mechanism which populates the controller with the basic methods such as index, create, show, new, edit, update and destroy.&lt;br /&gt;
A simple example of index method is as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    @categories = Category.all&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # index.html.erb&lt;br /&gt;
      format.json { render json: @categories }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In ruby, @ operator is used to declare instance variables. The index method makes use of @categories instance variable which is populated with all the records in the Category table. The respond_to method is used for responding to particular type of request. It contains a block and a variable |format| which determines whether the incoming request should be responded with HTML (when we are interacting with a user) or JSON format (if we are returning an object). &lt;br /&gt;
After executing the code, the controller will render a template of the same name. (For this example, the template is views/categories/index.html.erb).&lt;br /&gt;
Similarly the show method is defined as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def show&lt;br /&gt;
    @category = Category.find(params[:id])&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # show.html.erb&lt;br /&gt;
      format.json { render json: @category }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The show method is used to display a particular object. The show method needs to access database to search for the given object. This is done using the find method which takes params[:id] as argument. The params[:id] contains the id or primary key of the object which is passed as a parameter to look up in the database and the result is stored in the instance variable. The result is displayed by rendering the show.html.erb file.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Active Record&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, a program deals with objects which are mapped into database relational databases. This kind of mapping is facilitated by the Active Record.&amp;lt;br&amp;gt;Active Record maps the Rails classes in to database tables and rails objects into database records.&lt;br /&gt;
This makes it easy to perform actions on database tables by invoking the class methods. Another benefit of Active Record is database migration. Migrations can manage the evolution of a schema used by several physical databases. It’s a solution to the common problem of adding a field to make a new feature work in your local database, but being unsure of how to push that change to other developers and to the production server. With migrations, you can describe the transformations in self-contained classes that can be checked into version control systems and executed against another database that might be one, two, or five versions behind.You can find a lot more details about rails migration here[8].&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Method Pairs&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;There are couple of method pairs such as new-create and edit-update which are executed in sequence. In method pair one method is responsible for display preparation and the other method validates the data and attempts it to save in the database.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def new&lt;br /&gt;
    @category = Category.new&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # new.html.erb&lt;br /&gt;
      format.json { render json: @category }&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def create&lt;br /&gt;
    @category = Category.new(params[:category])&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      if @category.save&lt;br /&gt;
        format.html { redirect_to @category, notice: 'Category was successfully created.' }&lt;br /&gt;
        format.json { render json: @category, status: :created, location: @category }&lt;br /&gt;
      else&lt;br /&gt;
        format.html { render action: &amp;quot;new&amp;quot; }&lt;br /&gt;
        format.json { render json: @category.errors, status: :unprocessable_entity }&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The new and create methods are known as method pairs which execute in sequence. The new method is used to instantiate a new Category object whereas create method is used to successfully save the object to the database. After execution of the new method, the controller renders the new.html.erb file which displays a form to create a new Category object. The user enters the required information in the form and the object id is returned in the URL on submitting the form. Consider the following URL is generated on submitting the form: http: //localhost:3000/categories/3.&lt;br /&gt;
This means that Category object with id =3 will be created. The details of the object will be processed by the create method. &lt;br /&gt;
The controller invokes the create method to store the object in the database. An observation can be made about the @category variable in the create method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@category = Category.new(params[:category])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The parameters passed in the URL after creating a new object is stored in the params hash (denoted by params[]). In this case the params[] contains an object with category_id = 3. The params[] assigns the data of object with category_id =3 to the instance variable. The data entered in the instance variable is validated using the @category.save method. If no errors are found then the object stored in the database and a message on successful creation of the object is displayed to the user. However, if the data entered in the object contains an error, the user is redirected to the new.html.erb page to resubmit the information and the errors are displayed. &lt;br /&gt;
Another example of such method pair is the edit and update methods. The edit method displays the table entry for an existing object, while the update method is invoked when the changes have been submitted.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Models of Cookbook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
Model handles the data processing in a web application. Actions interact with the model to manipulate the data. Rails is capable of handling databases and this is facilitated by the model. You can base models on the Rails ActiveRecord module which contains support databases.&lt;br /&gt;
In our Cookbook application we have two models namely: Category and Recipe. They are represented by files category.rb and recipe.rb and each file is used to describe the relation between two classes. Let’s take a look at the two models&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt; category.rb&amp;lt;/b&amp;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;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;recipe.rb&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;lt;b&amp;gt;Relationships in models&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;one-to-one&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Syllabus &amp;lt; ActiveRecord::Base&lt;br /&gt;
	belongs_to :course¬&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Course &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_one :syllabus&lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;many-many&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Student &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_and_belongs_to_many :courses&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Course &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_and_belongs_to_many :students&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In Cookbook, we have many-one relationship between recipes and categories.&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Views of Cookbook application&amp;lt;/h1&amp;gt;&lt;br /&gt;
What is V in MVC? ‘V’ refers to views. Views are the actual interface that users can see. It is through views, that a user may request some data to be viewed or submits some data for processing.&amp;lt;br&amp;gt;&lt;br /&gt;
Views in rails are of the form file_name.html.erb. Here erb means embedded ruby. What html.erb denotes is some ruby code is embedded in HTML code. Like HTML, embedded ruby also uses tags to represent data. However, to distinguish it from the HTML code, ruby is embedded between &amp;lt;% %&amp;gt; if some non-assignment ruby operation is to be performed. If something needs to be displayed on screen, &amp;lt;%= %&amp;gt; is used.&lt;br /&gt;
Views are present in “app/views” folder in the rails application.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Name of the view files is same as the name of their respective controller. For e.g.:- if I have a controller movie with an action new, the corresponding view page will be new.html.erb in the app/views/movie directory.&amp;lt;br&amp;gt;&lt;br /&gt;
The Cookbook application views are as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Recipe Controller Views&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
1. index.html.erb&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;nowiki&amp;gt;&amp;lt;h1&amp;gt;Listing categories&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
  &amp;lt;tr&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;Name&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;% @categories.each do |category| %&amp;gt;&lt;br /&gt;
  &amp;lt;tr&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= category.name %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Show', category %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Edit', edit_category_path(category) %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Destroy', category, method: :delete, data: { confirm: 'Are you sure?' } %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%= link_to 'New Category', new_category_path %&amp;gt;&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;% @recipes.each do |recipe| %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
Here @recipes is an instance variable of model recipe defined in index action of the controller ‘recipes’. It contains all the recipes currently present in our application. Then each recipe is taken from this instance variable and is assigned to the local variable recipe.  All the subsequent lines display the recipe details (name, description and instruction).&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%= link_to ‘Show’, recipe %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
link_to is an ruby operator which is used to redirect the control to some other page. ‘Show’ is displayed as it is on the page and is an hyperlink. It links to a page pointed by recipe.  The path is defined in the routes.rb file. &lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
2. _form.html.erb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is a partial. Partials are devices used for breaking the code in smaller, more manageable chunks. This helps in modularity and also maintain DRY principle[4]. With a partial, code for rendering a particular response is moved to its own file. So for e.g. we can use this same partial for creating and editing information, because the information involved is essentially the same.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= form_for(@recipe) do |f| %&amp;gt;&lt;br /&gt;
  &amp;lt;% if @recipe.errors.any? %&amp;gt;&lt;br /&gt;
    &amp;lt;div id=&amp;quot;error_explanation&amp;quot;&amp;gt;&lt;br /&gt;
      &amp;lt;h2&amp;gt;&amp;lt;%= pluralize(@recipe.errors.count, &amp;quot;error&amp;quot;) %&amp;gt; prohibited this recipe from being saved:&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      &amp;lt;ul&amp;gt;&lt;br /&gt;
      &amp;lt;% @recipe.errors.full_messages.each do |msg| %&amp;gt;&lt;br /&gt;
        &amp;lt;li&amp;gt;&amp;lt;%= msg %&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
      &amp;lt;% end %&amp;gt;&lt;br /&gt;
      &amp;lt;/ul&amp;gt;&lt;br /&gt;
    &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;% end %&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :name %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_field :name %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
    &amp;lt;%=  f.label :category %&amp;gt;&lt;br /&gt;
    &amp;lt;%=    select('recipe','category_id', Category.all.collect {|c| [c[:name],c[:id]]}) %&amp;gt;&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :description %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_field :description %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :instructions %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_area :instructions %&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.submit %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;&amp;lt;%= form_for(@recipe) do |f| %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
This statement means to render a form for the instance variable @recipe.&lt;br /&gt;
If this page is being used for creating a new recipe, then the values for each key in the @recipe hash will be empty. If it is used for editing a recipe, @recipe will contain information corresponding to the recipe being edited.&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;% if @recipe.errors.any? %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
While saving an recipe or updating a recipe in the database, some violations may occur. Like the recipe name is left empty or its description is empty and so on. So while saving this information, database will signal some error. The errors are sent back to this page and are caught in @recipe.errors.any? statement.&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%=    select('recipe','category_id', Category.all.collect {|c| [c[:name],c[:id]]}) %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
This statement creates a dropdown box.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;‘recipe’ : refers to the object to which the selected value is to be assigned. In this case , it is the recipe object&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;‘category_id’ : create a ‘category_id’ key in the recipe hash.&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;Category.all.collect {|c| [c[:name],c[:id]]} : recover all categories from the database and list them  in the dropdown box. The value for each name is c[:id]. Whenever a category is selected by the user, its corresponding id is assigned to the ‘category_id’ key in the ‘recipe’ object.&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
3. new.html.erb&amp;lt;br&amp;gt;&lt;br /&gt;
This page is used to create a new recipe. This page simply renders the partial described above.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;New recipe&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;%= render 'form' %&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%= render ‘form’ %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
This statement renders the partial.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
4. edit.html.erb&lt;br /&gt;
This page is used to edit a existing recipe. This page also renders the partial.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Editing recipe&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;%= render 'form' %&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Show', @recipe %&amp;gt; |&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
5. show.html.erb&lt;br /&gt;
This page is used to show a particular recipe requested by the user. The recipe requested is captured by the controller in the @recipe instance variable and is passed to this view.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;p id=&amp;quot;notice&amp;quot;&amp;gt;&amp;lt;%= notice %&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Name:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.name %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Description:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.description %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Instructions:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.instructions %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Edit', edit_recipe_path(@recipe) %&amp;gt; |&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;The Category Views are very similar to the recipe Views and can be created similarly.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Conclusion&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
MVC was first proposed by Trygve Reenskaug in 1979. He broke applications into three types of components: models, which is responsible for maintaining the state of the application; views, which are responsible for generating a user interface; and controllers, which orchestrate the applications by receiving events from outside world, interact with model, and display an appropriate view to user. This separation of concerns led to far less computing, and in turn made the code easier to write and maintain.&amp;lt;br&amp;gt;&lt;br /&gt;
Use Ruby on Rails can implement MVC to build various types of web applications. The Cookbook is an easy starter. Readers can learn the basics of Ruby on Rails from this page.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;References&amp;lt;/h1&amp;gt;&lt;br /&gt;
*[1] http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller&lt;br /&gt;
*[2] http://en.wikipedia.org/wiki/Ruby_on_Rails &lt;br /&gt;
*[3] Sam Ruby, Dave Thomas, David Heinemeier Hansson.agile web development with rails (fourth edition)&lt;br /&gt;
*[4] http://en.wikipedia.org/wiki/Don't_repeat_yourself&lt;br /&gt;
*[5] http://guides.rubyonrails.org/getting_started.html&lt;br /&gt;
*[6] http://api.rubyonrails.org/classes/ActiveRecord/Migration.html&lt;br /&gt;
*[7] http://en.wikipedia.org/wiki/Metaprogramming&lt;br /&gt;
*[8] http://net.tutsplus.com/tutorials/other/mvc-for-noobs/&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The old wiki can be found here http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch4_4d_ch.&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w38&amp;diff=66974</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w38</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w38&amp;diff=66974"/>
		<updated>2012-10-04T01:04:53Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;Introduction&amp;lt;/h1&amp;gt;&lt;br /&gt;
This page is aimed at asserting the importance of Model View Controller Architecture and how MVC is implemented in Cookbook application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Model View Controller (MVC) Architecture&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The purpose of creating MVC architecture is to have clear separation between the responsibilities of business logic and representation. In a nutshell the responsibilities are mentioned as below:&lt;br /&gt;
&lt;br /&gt;
[[File:MVC.png|MVC Responsibilities]] [8]&lt;br /&gt;
&lt;br /&gt;
Model-View-Controller (MVC)[1] is an architectural pattern used in software engineering.In complex computer applications that present a large amount of data to the user, a developer often wishes to separate data (model) and user interface (view) concerns, so that changes to the user interface will not affect data handling, and that the data can be reorganized without changing the user interface.&amp;lt;br&amp;gt;&lt;br /&gt;
The model-view-controller solves this problem by decoupling data access and business logic from data presentation and user interaction, by introducing an intermediate component: the controller. Having described the responsibilities of three components let’s take a look how these components interact with each other.&lt;br /&gt;
&lt;br /&gt;
[[File:MVC_architecture.PNG|MVC Architecture]] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Ruby on Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby on Rails[2] is an MVC framework. Rails enforces a structure for application, it helps to knits the models, views, and controllers as separate chunks of functionality while the program executes. No more external configuration needed. Rails is packed with features that make you more productive, with many of the following features building on one other.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Metaprogramming[7]:&amp;lt;/b&amp;gt; Other frameworks use extensive code generation from scratch. Metaprogramming[9] techniques use programs to write programs. Ruby is one of the best languages for metaprogramming, and Rails uses this capability well. Rails also uses code generation but relies much more on metaprogramming for the heavy lifting.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Active Record:&amp;lt;/b&amp;gt; Rails introduces the Active Record[6] framework, which saves objects to the database. The Rails version of Active Record discovers the columns in a database schema and automatically attaches them to your domain objects using metaprogramming.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Convention over configuration:&amp;lt;/b&amp;gt; Most web development frameworks for .NET or Java force you to write pages of configuration code. If you follow suggested naming conventions, Rails doesn't need much configuration.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Scaffolding:&amp;lt;/b&amp;gt; You often create temporary code in the early stages of development to help get an application up quickly and see how major components work together. Rails automatically creates much of the scaffolding you'll need.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Built-in testing:&amp;lt;/b&amp;gt; Rails creates simple automated tests you can then extend. Rails also provides supporting code called harnesses and fixtures that make test cases easier to write and run. Ruby can then execute all your automated tests with the rake utility.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Three environments:&amp;lt;/b&amp;gt; Rails gives you three default environments: development, testing, and production. Each behaves slightly differently, making your entire software development cycle easier. For example, Rails creates a fresh copy of the Test database for each test run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Cookbook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
Cookbook Application is a very basic Ruby on Rails application. It is a great way to understand the nuances that ‘Ruby’ has to offer. It is built keeping in mind that the reader is new to the world of ruby and is developing his/her first Ruby on Rails application.&lt;br /&gt;
The motive of this wiki page is to make the reader understand how the Cookbook application was built. And on the way to building this application, user will be introduced to some key concepts like Model, Views and Controllers. At the end of this page, reader must fairly be ready to start building his/her basic Ruby on Rails application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;A simple application to start -- Cookbook application&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Start a new project. While creating a new project, remember to select Rails Application option as follows&lt;br /&gt;
&lt;br /&gt;
[[File:Start_application.png|Start a new Application]] &lt;br /&gt;
&lt;br /&gt;
When we create a Rails application, rails automatically help us create a brunch of files and folders as follows:&lt;br /&gt;
&lt;br /&gt;
[[File:Listing_of_cookbook_application.png|Listing_of_cookbook_application]] &lt;br /&gt;
&lt;br /&gt;
We mainly use the app directory. This is where the heart of the application is stored i.e. Models, Views and controllers.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Using scaffold to create framework&amp;lt;/h2&amp;gt;&lt;br /&gt;
Rails scaffolding is a quick way to generate some of the major pieces of an application. If you want to create the models, views, and controllers for a new resource in a single operation, scaffolding is the tool for the job.&amp;lt;br&amp;gt;&lt;br /&gt;
In Rubymine, we can create a scaffold by right clicking on the current Project-&amp;gt;New-&amp;gt;Run Rails Generator -&amp;gt;scaffold. This will open a dialog box to create a scaffold where we can enter the name and the attributes along with their data type. Once the scaffold has been created RubyMine will generate the model, view and controller files respectively. These files do not contain any specific methods but they provide a framework which is useful for general applications. The user can modify the MVC behavior according to his application requirements. Each scaffold has create, read, update and delete methods declared in the controller class. A user can modify the behavior of the controller through these methods. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;[[File:Create_Scaffold.png|Creating a new scaffold]]&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Scaffold.png|Scaffold]]          [[File:New_Scaffold_Categories.png|Creating scaffold for categories]]&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the Cookbook application we are considering a Cookbook which contains recipes for various dishes. The recipes have been divided into different categories, for example there is a recipe for ice tea which belongs to the category of beverages. There is a relationship which is created between a recipe and a category which can be expressed as an ice tea belongs to beverages. We can have many such recipes belonging to a single category, in this case many to one relationship is established. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Controllers for Cookbook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
The Cookbook application starts by entering a URL in the browser which sends the request to the web server. Since our web server supports Rails application, it will hand over the control to a Rails controller.&lt;br /&gt;
A controller ‘controls’ the entire application by responding to the incoming requests as needed. For example, consider the following URL http ://localhost:3000/categories/show which is decoded by the controller to know that its action named show has been invoked.&lt;br /&gt;
All the controllers inherit only the ApplicationController which in turn inherits the ActionController::Base class. ActionContoller module provides the basic support for the controllers in Rails.&lt;br /&gt;
In our Cookbook application we generate Categories controller using the scaffold mechanism which populates the controller with the basic methods such as index, create, show, new, edit, update and destroy.&lt;br /&gt;
A simple example of index method is as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    @categories = Category.all&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # index.html.erb&lt;br /&gt;
      format.json { render json: @categories }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In ruby, @ operator is used to declare instance variables. The index method makes use of @categories instance variable which is populated with all the records in the Category table. The respond_to method is used for responding to particular type of request. It contains a block and a variable |format| which determines whether the incoming request should be responded with HTML (when we are interacting with a user) or JSON format (if we are returning an object). &lt;br /&gt;
After executing the code, the controller will render a template of the same name. (For this example, the template is views/categories/index.html.erb).&lt;br /&gt;
Similarly the show method is defined as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def show&lt;br /&gt;
    @category = Category.find(params[:id])&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # show.html.erb&lt;br /&gt;
      format.json { render json: @category }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The show method is used to display a particular object. The show method needs to access database to search for the given object. This is done using the find method which takes params[:id] as argument. The params[:id] contains the id or primary key of the object which is passed as a parameter to look up in the database and the result is stored in the instance variable. The result is displayed by rendering the show.html.erb file.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Active Record&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, a program deals with objects which are mapped into database relational databases. This kind of mapping is facilitated by the Active Record.&amp;lt;br&amp;gt;Active Record maps the Rails classes in to database tables and rails objects into database records.&lt;br /&gt;
This makes it easy to perform actions on database tables by invoking the class methods. Another benefit of Active Record is database migration. Migrations can manage the evolution of a schema used by several physical databases. It’s a solution to the common problem of adding a field to make a new feature work in your local database, but being unsure of how to push that change to other developers and to the production server. With migrations, you can describe the transformations in self-contained classes that can be checked into version control systems and executed against another database that might be one, two, or five versions behind.You can find a lot more details about rails migration here[8].&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Method Pairs&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;There are couple of method pairs such as new-create and edit-update which are executed in sequence. In method pair one method is responsible for display preparation and the other method validates the data and attempts it to save in the database.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def new&lt;br /&gt;
    @category = Category.new&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # new.html.erb&lt;br /&gt;
      format.json { render json: @category }&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def create&lt;br /&gt;
    @category = Category.new(params[:category])&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      if @category.save&lt;br /&gt;
        format.html { redirect_to @category, notice: 'Category was successfully created.' }&lt;br /&gt;
        format.json { render json: @category, status: :created, location: @category }&lt;br /&gt;
      else&lt;br /&gt;
        format.html { render action: &amp;quot;new&amp;quot; }&lt;br /&gt;
        format.json { render json: @category.errors, status: :unprocessable_entity }&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The new and create methods are known as method pairs which execute in sequence. The new method is used to instantiate a new Category object whereas create method is used to successfully save the object to the database. After execution of the new method, the controller renders the new.html.erb file which displays a form to create a new Category object. The user enters the required information in the form and the object id is returned in the URL on submitting the form. Consider the following URL is generated on submitting the form: http: //localhost:3000/categories/3.&lt;br /&gt;
This means that Category object with id =3 will be created. The details of the object will be processed by the create method. &lt;br /&gt;
The controller invokes the create method to store the object in the database. An observation can be made about the @category variable in the create method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@category = Category.new(params[:category])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The parameters passed in the URL after creating a new object is stored in the params hash (denoted by params[]). In this case the params[] contains an object with category_id = 3. The params[] assigns the data of object with category_id =3 to the instance variable. The data entered in the instance variable is validated using the @category.save method. If no errors are found then the object stored in the database and a message on successful creation of the object is displayed to the user. However, if the data entered in the object contains an error, the user is redirected to the new.html.erb page to resubmit the information and the errors are displayed. &lt;br /&gt;
Another example of such method pair is the edit and update methods. The edit method displays the table entry for an existing object, while the update method is invoked when the changes have been submitted.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Models of Cookbook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
Model handles the data processing in a web application. Actions interact with the model to manipulate the data. Rails is capable of handling databases and this is facilitated by the model. You can base models on the Rails ActiveRecord module which contains support databases.&lt;br /&gt;
In our Cookbook application we have two models namely: Category and Recipe. They are represented by files category.rb and recipe.rb and each file is used to describe the relation between two classes. Let’s take a look at the two models&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt; category.rb&amp;lt;/b&amp;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;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;recipe.rb&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;lt;b&amp;gt;Relationships in models&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;one-to-one&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Syllabus &amp;lt; ActiveRecord::Base&lt;br /&gt;
	belongs_to :course¬&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Course &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_one :syllabus&lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;many-many&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Student &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_and_belongs_to_many :courses&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Course &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_and_belongs_to_many :students&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In Cookbook, we have many-one relationship between recipes and categories.&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Views of Cookbook application&amp;lt;/h1&amp;gt;&lt;br /&gt;
What is V in MVC? ‘V’ refers to views. Views are the actual interface that users can see. It is through views, that a user may request some data to be viewed or submits some data for processing.&amp;lt;br&amp;gt;&lt;br /&gt;
Views in rails are of the form file_name.html.erb. Here erb means embedded ruby. What html.erb denotes is some ruby code is embedded in HTML code. Like HTML, embedded ruby also uses tags to represent data. However, to distinguish it from the HTML code, ruby is embedded between &amp;lt;% %&amp;gt; if some non-assignment ruby operation is to be performed. If something needs to be displayed on screen, &amp;lt;%= %&amp;gt; is used.&lt;br /&gt;
Views are present in “app/views” folder in the rails application.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Name of the view files is same as the name of their respective controller. For e.g.:- if I have a controller movie with an action new, the corresponding view page will be new.html.erb in the app/views/movie directory.&amp;lt;br&amp;gt;&lt;br /&gt;
The Cookbook application views are as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Recipe Controller Views&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
1. index.html.erb&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;nowiki&amp;gt;&amp;lt;h1&amp;gt;Listing categories&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
  &amp;lt;tr&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;Name&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;% @categories.each do |category| %&amp;gt;&lt;br /&gt;
  &amp;lt;tr&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= category.name %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Show', category %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Edit', edit_category_path(category) %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Destroy', category, method: :delete, data: { confirm: 'Are you sure?' } %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%= link_to 'New Category', new_category_path %&amp;gt;&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;% @recipes.each do |recipe| %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
Here @recipes is an instance variable of model recipe defined in index action of the controller ‘recipes’. It contains all the recipes currently present in our application. Then each recipe is taken from this instance variable and is assigned to the local variable recipe.  All the subsequent lines display the recipe details (name, description and instruction).&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%= link_to ‘Show’, recipe %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
link_to is an ruby operator which is used to redirect the control to some other page. ‘Show’ is displayed as it is on the page and is an hyperlink. It links to a page pointed by recipe.  The path is defined in the routes.rb file. &lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
2. _form.html.erb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is a partial. Partials are devices used for breaking the code in smaller, more manageable chunks. This helps in modularity and also maintain DRY principle[4]. With a partial, code for rendering a particular response is moved to its own file. So for e.g. we can use this same partial for creating and editing information, because the information involved is essentially the same.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= form_for(@recipe) do |f| %&amp;gt;&lt;br /&gt;
  &amp;lt;% if @recipe.errors.any? %&amp;gt;&lt;br /&gt;
    &amp;lt;div id=&amp;quot;error_explanation&amp;quot;&amp;gt;&lt;br /&gt;
      &amp;lt;h2&amp;gt;&amp;lt;%= pluralize(@recipe.errors.count, &amp;quot;error&amp;quot;) %&amp;gt; prohibited this recipe from being saved:&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      &amp;lt;ul&amp;gt;&lt;br /&gt;
      &amp;lt;% @recipe.errors.full_messages.each do |msg| %&amp;gt;&lt;br /&gt;
        &amp;lt;li&amp;gt;&amp;lt;%= msg %&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
      &amp;lt;% end %&amp;gt;&lt;br /&gt;
      &amp;lt;/ul&amp;gt;&lt;br /&gt;
    &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;% end %&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :name %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_field :name %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
    &amp;lt;%=  f.label :category %&amp;gt;&lt;br /&gt;
    &amp;lt;%=    select('recipe','category_id', Category.all.collect {|c| [c[:name],c[:id]]}) %&amp;gt;&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :description %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_field :description %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :instructions %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_area :instructions %&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.submit %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;&amp;lt;%= form_for(@recipe) do |f| %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
This statement means to render a form for the instance variable @recipe.&lt;br /&gt;
If this page is being used for creating a new recipe, then the values for each key in the @recipe hash will be empty. If it is used for editing a recipe, @recipe will contain information corresponding to the recipe being edited.&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;% if @recipe.errors.any? %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
While saving an recipe or updating a recipe in the database, some violations may occur. Like the recipe name is left empty or its description is empty and so on. So while saving this information, database will signal some error. The errors are sent back to this page and are caught in @recipe.errors.any? statement.&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%=    select('recipe','category_id', Category.all.collect {|c| [c[:name],c[:id]]}) %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
This statement creates a dropdown box.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;‘recipe’ : refers to the object to which the selected value is to be assigned. In this case , it is the recipe object&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;‘category_id’ : create a ‘category_id’ key in the recipe hash.&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;Category.all.collect {|c| [c[:name],c[:id]]} : recover all categories from the database and list them  in the dropdown box. The value for each name is c[:id]. Whenever a category is selected by the user, its corresponding id is assigned to the ‘category_id’ key in the ‘recipe’ object.&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
3. new.html.erb&amp;lt;br&amp;gt;&lt;br /&gt;
This page is used to create a new recipe. This page simply renders the partial described above.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;New recipe&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;%= render 'form' %&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%= render ‘form’ %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
This statement renders the partial.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
4. edit.html.erb&lt;br /&gt;
This page is used to edit a existing recipe. This page also renders the partial.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Editing recipe&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;%= render 'form' %&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Show', @recipe %&amp;gt; |&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
5. show.html.erb&lt;br /&gt;
This page is used to show a particular recipe requested by the user. The recipe requested is captured by the controller in the @recipe instance variable and is passed to this view.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;p id=&amp;quot;notice&amp;quot;&amp;gt;&amp;lt;%= notice %&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Name:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.name %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Description:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.description %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Instructions:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.instructions %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Edit', edit_recipe_path(@recipe) %&amp;gt; |&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;The Category Views are very similar to the recipe Views and can be created similarly.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Conclusion&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
MVC was first proposed by Trygve Reenskaug in 1979. He broke applications into three types of components: models, which is responsible for maintaining the state of the application; views, which are responsible for generating a user interface; and controllers, which orchestrate the applications by receiving events from outside world, interact with model, and display an appropriate view to user. This separation of concerns led to far less computing, and in turn made the code easier to write and maintain.&amp;lt;br&amp;gt;&lt;br /&gt;
Use Ruby on Rails can implement MVC to build various types of web applications. The Cookbook is an easy starter. Readers can learn the basics of Ruby on Rails from this page.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;References&amp;lt;/h1&amp;gt;&lt;br /&gt;
*[1] http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller&lt;br /&gt;
*[2] http://en.wikipedia.org/wiki/Ruby_on_Rails &lt;br /&gt;
*[3] Sam Ruby, Dave Thomas, David Heinemeier Hansson.agile web development with rails (fourth edition)&lt;br /&gt;
*[4] http://en.wikipedia.org/wiki/Don't_repeat_yourself&lt;br /&gt;
*[5] http://guides.rubyonrails.org/getting_started.html&lt;br /&gt;
*[6] http://api.rubyonrails.org/classes/ActiveRecord/Migration.html&lt;br /&gt;
*[7] http://en.wikipedia.org/wiki/Metaprogramming&lt;br /&gt;
*[8] http://net.tutsplus.com/tutorials/other/mvc-for-noobs/&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The old wiki can be found here http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch4_4d_ch.&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012&amp;diff=66944</id>
		<title>CSC/ECE 517 Fall 2012</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012&amp;diff=66944"/>
		<updated>2012-10-04T00:56:55Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*[[CSC/ECE 517 Fall 2012/ch1 n xx]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w1 rk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w20 pp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w5 su]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w6 pp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w4 aj]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w7 am]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w8 aa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w9 av]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w10 pk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w11 ap]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w12 mv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w14 gv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w17 ir]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w18 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w22 an]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w21 aa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w21 wi]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w31 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w16 br]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w23 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w24 nr]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w15 rt]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w3 pl]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w32 cm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w37 ss]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w67 ks]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w27 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w29 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w33 op]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w19 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w34 vd]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w35 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w30 rp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w47 sk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w69 mv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w44 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w45 is]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w53 kc]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w40 ar]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w39 sn]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w54 go]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w56 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w64 nn]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w66 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w40 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w42 js]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w46 sm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w71 gs]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w63 dv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w55 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w57 mp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w52 an]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch1b_1w38]]&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w38&amp;diff=66939</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w38</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w38&amp;diff=66939"/>
		<updated>2012-10-04T00:55:50Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;Introduction&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Model View Controller (MVC) Architecture&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The purpose of creating MVC architecture is to have clear separation between the responsibilities of business logic and representation. In a nutshell the responsibilities are mentioned as below:&lt;br /&gt;
&lt;br /&gt;
[[File:MVC.png|MVC Responsibilities]] [8]&lt;br /&gt;
&lt;br /&gt;
Model-View-Controller (MVC)[1] is an architectural pattern used in software engineering.In complex computer applications that present a large amount of data to the user, a developer often wishes to separate data (model) and user interface (view) concerns, so that changes to the user interface will not affect data handling, and that the data can be reorganized without changing the user interface.&amp;lt;br&amp;gt;&lt;br /&gt;
The model-view-controller solves this problem by decoupling data access and business logic from data presentation and user interaction, by introducing an intermediate component: the controller. Having described the responsibilities of three components let’s take a look how these components interact with each other.&lt;br /&gt;
&lt;br /&gt;
[[File:MVC_architecture.PNG|MVC Architecture]] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Ruby on Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby on Rails[2] is an MVC framework. Rails enforces a structure for application, it helps to knits the models, views, and controllers as separate chunks of functionality while the program executes. No more external configuration needed. Rails is packed with features that make you more productive, with many of the following features building on one other.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Metaprogramming[7]:&amp;lt;/b&amp;gt; Other frameworks use extensive code generation from scratch. Metaprogramming[9] techniques use programs to write programs. Ruby is one of the best languages for metaprogramming, and Rails uses this capability well. Rails also uses code generation but relies much more on metaprogramming for the heavy lifting.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Active Record:&amp;lt;/b&amp;gt; Rails introduces the Active Record[6] framework, which saves objects to the database. The Rails version of Active Record discovers the columns in a database schema and automatically attaches them to your domain objects using metaprogramming.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Convention over configuration:&amp;lt;/b&amp;gt; Most web development frameworks for .NET or Java force you to write pages of configuration code. If you follow suggested naming conventions, Rails doesn't need much configuration.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Scaffolding:&amp;lt;/b&amp;gt; You often create temporary code in the early stages of development to help get an application up quickly and see how major components work together. Rails automatically creates much of the scaffolding you'll need.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Built-in testing:&amp;lt;/b&amp;gt; Rails creates simple automated tests you can then extend. Rails also provides supporting code called harnesses and fixtures that make test cases easier to write and run. Ruby can then execute all your automated tests with the rake utility.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Three environments:&amp;lt;/b&amp;gt; Rails gives you three default environments: development, testing, and production. Each behaves slightly differently, making your entire software development cycle easier. For example, Rails creates a fresh copy of the Test database for each test run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;CookBook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
Cookbook Application is a very basic Ruby on Rails application. It is a great way to understand the nuances that ‘Ruby’ has to offer. It is built keeping in mind that the reader is new to the world of ruby and is developing his/her first Ruby on Rails application.&lt;br /&gt;
The motive of this wiki page is to make the reader understand how the Cookbook application was built. And on the way to building this application, user will be introduced to some key concepts like Model, Views and Controllers. At the end of this page, reader must fairly be ready to start building his/her basic Ruby on Rails application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;A simple application to start -- Cookbook application&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Start a new project. While creating a new project, remember to select Rails Application option as follows&lt;br /&gt;
&lt;br /&gt;
[[File:Start_application.png|Start a new Application]] &lt;br /&gt;
&lt;br /&gt;
When we create a Rails application, rails automatically help us create a brunch of files and folders as follows:&lt;br /&gt;
&lt;br /&gt;
[[File:Listing_of_cookbook_application.png|Listing_of_cookbook_application]] &lt;br /&gt;
&lt;br /&gt;
We mainly use the app directory. This is where the heart of the application is stored i.e. Models, Views and controllers.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Using scaffold to create framework&amp;lt;/h2&amp;gt;&lt;br /&gt;
Rails scaffolding is a quick way to generate some of the major pieces of an application. If you want to create the models, views, and controllers for a new resource in a single operation, scaffolding is the tool for the job.&amp;lt;br&amp;gt;&lt;br /&gt;
In Rubymine, we can create a scaffold by right clicking on the current Project-&amp;gt;New-&amp;gt;Run Rails Generator -&amp;gt;scaffold. This will open a dialog box to create a scaffold where we can enter the name and the attributes along with their data type. Once the scaffold has been created RubyMine will generate the model, view and controller files respectively. These files do not contain any specific methods but they provide a framework which is useful for general applications. The user can modify the MVC behavior according to his application requirements. Each scaffold has create, read, update and delete methods declared in the controller class. A user can modify the behavior of the controller through these methods. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;[[File:Create_Scaffold.png|Creating a new scaffold]]&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Scaffold.png|Scaffold]]          [[File:New_Scaffold_Categories.png|Creating scaffold for categories]]&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the Cookbook application we are considering a Cookbook which contains recipes for various dishes. The recipes have been divided into different categories, for example there is a recipe for ice tea which belongs to the category of beverages. There is a relationship which is created between a recipe and a category which can be expressed as an ice tea belongs to beverages. We can have many such recipes belonging to a single category, in this case many to one relationship is established. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Controllers for CookBook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
The Cookbook application starts by entering a URL in the browser which sends the request to the web server. Since our web server supports Rails application, it will hand over the control to a Rails controller.&lt;br /&gt;
A controller ‘controls’ the entire application by responding to the incoming requests as needed. For example, consider the following URL http://localhost:3000/categories/show which is decoded by the controller to know that its action named show has been invoked.&lt;br /&gt;
All the controllers inherit only the ApplicationController which in turn inherits the ActionController::Base class. ActionContoller module provides the basic support for the controllers in Rails.&lt;br /&gt;
In our Cookbook application we generate Categories controller using the scaffold mechanism which populates the controller with the basic methods such as index, create, show, new, edit, update and destroy.&lt;br /&gt;
A simple example of index method is as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    @categories = Category.all&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # index.html.erb&lt;br /&gt;
      format.json { render json: @categories }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In ruby, @ operator is used to declare instance variables. The index method makes use of @categories instance variable which is populated with all the records in the Category table. The respond_to method is used for responding to particular type of request. It contains a block and a variable |format| which determines whether the incoming request should be responded with HTML (when we are interacting with a user) or JSON format (if we are returning an object). &lt;br /&gt;
After executing the code, the controller will render a template of the same name. (For this example, the template is views/categories/index.html.erb).&lt;br /&gt;
Similarly the show method is defined as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def show&lt;br /&gt;
    @category = Category.find(params[:id])&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # show.html.erb&lt;br /&gt;
      format.json { render json: @category }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The show method is used to display a particular object. The show method needs to access database to search for the given object. This is done using the find method which takes params[:id] as argument. The params[:id] contains the id or primary key of the object which is passed as a parameter to look up in the database and the result is stored in the instance variable. The result is displayed by rendering the show.html.erb file.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Active Record&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, a program deals with objects which are mapped into database relational databases. This kind of mapping is facilitated by the Active Record.&amp;lt;br&amp;gt;Active Record maps the Rails classes in to database tables and rails objects into database records.&lt;br /&gt;
This makes it easy to perform actions on database tables by invoking the class methods. Another benefit of Active Record is database migration. Migrations can manage the evolution of a schema used by several physical databases. It’s a solution to the common problem of adding a field to make a new feature work in your local database, but being unsure of how to push that change to other developers and to the production server. With migrations, you can describe the transformations in self-contained classes that can be checked into version control systems and executed against another database that might be one, two, or five versions behind.You can find a lot more details about rails migration here[8].&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Method Pairs&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;There are couple of method pairs such as new-create and edit-update which are executed in sequence. In method pair one method is responsible for display preparation and the other method validates the data and attempts it to save in the database.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def new&lt;br /&gt;
    @category = Category.new&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # new.html.erb&lt;br /&gt;
      format.json { render json: @category }&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def create&lt;br /&gt;
    @category = Category.new(params[:category])&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      if @category.save&lt;br /&gt;
        format.html { redirect_to @category, notice: 'Category was successfully created.' }&lt;br /&gt;
        format.json { render json: @category, status: :created, location: @category }&lt;br /&gt;
      else&lt;br /&gt;
        format.html { render action: &amp;quot;new&amp;quot; }&lt;br /&gt;
        format.json { render json: @category.errors, status: :unprocessable_entity }&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The new and create methods are known as method pairs which execute in sequence. The new method is used to instantiate a new Category object whereas create method is used to successfully save the object to the database. After execution of the new method, the controller renders the new.html.erb file which displays a form to create a new Category object. The user enters the required information in the form and the object id is returned in the URL on submitting the form. Consider the following URL is generated on submitting the form: http: //localhost:3000/categories/3.&lt;br /&gt;
This means that Category object with id =3 will be created. The details of the object will be processed by the create method. &lt;br /&gt;
The controller invokes the create method to store the object in the database. An observation can be made about the @category variable in the create method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@category = Category.new(params[:category])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The parameters passed in the URL after creating a new object is stored in the params hash (denoted by params[]). In this case the params[] contains an object with category_id = 3. The params[] assigns the data of object with category_id =3 to the instance variable. The data entered in the instance variable is validated using the @category.save method. If no errors are found then the object stored in the database and a message on successful creation of the object is displayed to the user. However, if the data entered in the object contains an error, the user is redirected to the new.html.erb page to resubmit the information and the errors are displayed. &lt;br /&gt;
Another example of such method pair is the edit and update methods. The edit method displays the table entry for an existing object, while the update method is invoked when the changes have been submitted.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Models of CookBook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
Model handles the data processing in a web application. Actions interact with the model to manipulate the data. Rails is capable of handling databases and this is facilitated by the model. You can base models on the Rails ActiveRecord module which contains support databases.&lt;br /&gt;
In our Cookbook application we have two models namely: Category and Recipe. They are represented by files category.rb and recipe.rb and each file is used to describe the relation between two classes. Let’s take a look at the two models&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt; category.rb&amp;lt;/b&amp;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;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;recipe.rb&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;lt;b&amp;gt;Relationships in models&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;one-to-one&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Syllabus &amp;lt; ActiveRecord::Base&lt;br /&gt;
	belongs_to :course¬&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Course &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_one :syllabus&lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;many-many&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Student &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_and_belongs_to_many :courses&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Course &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_and_belongs_to_many :students&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In cookbook, we have many-one relationship between recipes and categories.&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Views of Cookbook application&amp;lt;/h1&amp;gt;&lt;br /&gt;
What is V in MVC? ‘V’ refers to views. Views are the actual interface that users can see. It is through views, that a user may request some data to be viewed or submits some data for processing.&amp;lt;br&amp;gt;&lt;br /&gt;
Views in rails are of the form file_name.html.erb. Here erb means embedded ruby. What html.erb denotes is some ruby code is embedded in HTML code. Like HTML, embedded ruby also uses tags to represent data. However, to distinguish it from the HTML code, ruby is embedded between &amp;lt;% %&amp;gt; if some non-assignment ruby operation is to be performed. If something needs to be displayed on screen, &amp;lt;%= %&amp;gt; is used.&lt;br /&gt;
Views are present in “app/views” folder in the rails application.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Name of the view files is same as the name of their respective controller. For e.g.:- if I have a controller movie with an action new, the corresponding view page will be new.html.erb in the app/views/movie directory.&amp;lt;br&amp;gt;&lt;br /&gt;
The cookbook application views are as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Recipe Controller Views&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
1. index.html.erb&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;nowiki&amp;gt;&amp;lt;h1&amp;gt;Listing categories&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
  &amp;lt;tr&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;Name&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;% @categories.each do |category| %&amp;gt;&lt;br /&gt;
  &amp;lt;tr&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= category.name %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Show', category %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Edit', edit_category_path(category) %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Destroy', category, method: :delete, data: { confirm: 'Are you sure?' } %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%= link_to 'New Category', new_category_path %&amp;gt;&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;% @recipes.each do |recipe| %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
Here @recipes is an instance variable of model recipe defined in index action of the controller ‘recipes’. It contains all the recipes currently present in our application. Then each recipe is taken from this instance variable and is assigned to the local variable recipe.  All the subsequent lines display the recipe details (name, description and instruction).&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%= link_to ‘Show’, recipe %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
link_to is an ruby operator which is used to redirect the control to some other page. ‘Show’ is displayed as it is on the page and is an hyperlink. It links to a page pointed by recipe.  The path is defined in the routes.rb file. &lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
2. _form.html.erb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is a partial. Partials are devices used for breaking the code in smaller, more manageable chunks. This helps in modularity and also maintain DRY principle[4]. With a partial, code for rendering a particular response is moved to its own file. So for e.g. we can use this same partial for creating and editing information, because the information involved is essentially the same.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= form_for(@recipe) do |f| %&amp;gt;&lt;br /&gt;
  &amp;lt;% if @recipe.errors.any? %&amp;gt;&lt;br /&gt;
    &amp;lt;div id=&amp;quot;error_explanation&amp;quot;&amp;gt;&lt;br /&gt;
      &amp;lt;h2&amp;gt;&amp;lt;%= pluralize(@recipe.errors.count, &amp;quot;error&amp;quot;) %&amp;gt; prohibited this recipe from being saved:&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      &amp;lt;ul&amp;gt;&lt;br /&gt;
      &amp;lt;% @recipe.errors.full_messages.each do |msg| %&amp;gt;&lt;br /&gt;
        &amp;lt;li&amp;gt;&amp;lt;%= msg %&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
      &amp;lt;% end %&amp;gt;&lt;br /&gt;
      &amp;lt;/ul&amp;gt;&lt;br /&gt;
    &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;% end %&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :name %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_field :name %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
    &amp;lt;%=  f.label :category %&amp;gt;&lt;br /&gt;
    &amp;lt;%=    select('recipe','category_id', Category.all.collect {|c| [c[:name],c[:id]]}) %&amp;gt;&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :description %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_field :description %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :instructions %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_area :instructions %&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.submit %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;&amp;lt;%= form_for(@recipe) do |f| %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
This statement means to render a form for the instance variable @recipe.&lt;br /&gt;
If this page is being used for creating a new recipe, then the values for each key in the @recipe hash will be empty. If it is used for editing a recipe, @recipe will contain information corresponding to the recipe being edited.&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;% if @recipe.errors.any? %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
While saving an recipe or updating a recipe in the database, some violations may occur. Like the recipe name is left empty or its description is empty and so on. So while saving this information, database will signal some error. The errors are sent back to this page and are caught in @recipe.errors.any? statement.&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%=    select('recipe','category_id', Category.all.collect {|c| [c[:name],c[:id]]}) %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
This statement creates a dropdown box.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;‘recipe’ : refers to the object to which the selected value is to be assigned. In this case , it is the recipe object&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;‘category_id’ : create a ‘category_id’ key in the recipe hash.&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;Category.all.collect {|c| [c[:name],c[:id]]} : recover all categories from the database and list them  in the dropdown box. The value for each name is c[:id]. Whenever a category is selected by the user, its corresponding id is assigned to the ‘category_id’ key in the ‘recipe’ object.&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
3. new.html.erb&amp;lt;br&amp;gt;&lt;br /&gt;
This page is used to create a new recipe. This page simply renders the partial described above.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;New recipe&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;%= render 'form' %&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%= render ‘form’ %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
This statement renders the partial.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
4. edit.html.erb&lt;br /&gt;
This page is used to edit a existing recipe. This page also renders the partial.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Editing recipe&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;%= render 'form' %&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Show', @recipe %&amp;gt; |&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
5. show.html.erb&lt;br /&gt;
This page is used to show a particular recipe requested by the user. The recipe requested is captured by the controller in the @recipe instance variable and is passed to this view.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;p id=&amp;quot;notice&amp;quot;&amp;gt;&amp;lt;%= notice %&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Name:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.name %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Description:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.description %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Instructions:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.instructions %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Edit', edit_recipe_path(@recipe) %&amp;gt; |&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;The Category Views are very similar to the recipe Views and can be created similarly.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Conclusion&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
MVC was first proposed by Trygve Reenskaug in 1979. He broke applications into three types of components: models, which is responsible for maintaining the state of the application; views, which are responsible for generating a user interface; and controllers, which orchestrate the applications by receiving events from outside world, interact with model, and display an appropriate view to user. This separation of concerns led to far less computing, and in turn made the code easier to write and maintain.&amp;lt;br&amp;gt;&lt;br /&gt;
Use Ruby on Rails can implement MVC to build various types of web applications. The Cookbook is an easy starter. Readers can learn the basics of Ruby on Rails from this page.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;References&amp;lt;/h1&amp;gt;&lt;br /&gt;
*[1] http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller&lt;br /&gt;
*[2] http://en.wikipedia.org/wiki/Ruby_on_Rails &lt;br /&gt;
*[3] Sam Ruby, Dave Thomas, David Heinemeier Hansson.agile web development with rails (fourth edition)&lt;br /&gt;
*[4] http://en.wikipedia.org/wiki/Don't_repeat_yourself&lt;br /&gt;
*[5] http://guides.rubyonrails.org/getting_started.html&lt;br /&gt;
*[6] http://api.rubyonrails.org/classes/ActiveRecord/Migration.html&lt;br /&gt;
*[7] http://en.wikipedia.org/wiki/Metaprogramming&lt;br /&gt;
*[8] http://net.tutsplus.com/tutorials/other/mvc-for-noobs/&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The old wiki can be found here http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch4_4d_ch.&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w38&amp;diff=66927</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w38</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w38&amp;diff=66927"/>
		<updated>2012-10-04T00:53:17Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;Introduction&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Model View Controller (MVC) Architecture&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The purpose of creating MVC architecture is to have clear separation between the responsibilities of business logic and representation. In a nutshell the responsibilities are mentioned as below:&lt;br /&gt;
&lt;br /&gt;
[[File:MVC.png|MVC Responsibilities]] &lt;br /&gt;
&lt;br /&gt;
Model-View-Controller (MVC)[1] is an architectural pattern used in software engineering.In complex computer applications that present a large amount of data to the user, a developer often wishes to separate data (model) and user interface (view) concerns, so that changes to the user interface will not affect data handling, and that the data can be reorganized without changing the user interface.&amp;lt;br&amp;gt;&lt;br /&gt;
The model-view-controller solves this problem by decoupling data access and business logic from data presentation and user interaction, by introducing an intermediate component: the controller. Having described the responsibilities of three components let’s take a look how these components interact with each other.&lt;br /&gt;
&lt;br /&gt;
[[File:MVC_architecture.PNG|MVC Architecture]] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Ruby on Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby on Rails[2] is an MVC framework. Rails enforces a structure for application, it helps to knits the models, views, and controllers as separate chunks of functionality while the program executes. No more external configuration needed. Rails is packed with features that make you more productive, with many of the following features building on one other.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Metaprogramming[7]:&amp;lt;/b&amp;gt; Other frameworks use extensive code generation from scratch. Metaprogramming[9] techniques use programs to write programs. Ruby is one of the best languages for metaprogramming, and Rails uses this capability well. Rails also uses code generation but relies much more on metaprogramming for the heavy lifting.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Active Record:&amp;lt;/b&amp;gt; Rails introduces the Active Record[6] framework, which saves objects to the database. The Rails version of Active Record discovers the columns in a database schema and automatically attaches them to your domain objects using metaprogramming.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Convention over configuration:&amp;lt;/b&amp;gt; Most web development frameworks for .NET or Java force you to write pages of configuration code. If you follow suggested naming conventions, Rails doesn't need much configuration.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Scaffolding:&amp;lt;/b&amp;gt; You often create temporary code in the early stages of development to help get an application up quickly and see how major components work together. Rails automatically creates much of the scaffolding you'll need.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Built-in testing:&amp;lt;/b&amp;gt; Rails creates simple automated tests you can then extend. Rails also provides supporting code called harnesses and fixtures that make test cases easier to write and run. Ruby can then execute all your automated tests with the rake utility.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Three environments:&amp;lt;/b&amp;gt; Rails gives you three default environments: development, testing, and production. Each behaves slightly differently, making your entire software development cycle easier. For example, Rails creates a fresh copy of the Test database for each test run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;CookBook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
Cookbook Application is a very basic Ruby on Rails application. It is a great way to understand the nuances that ‘Ruby’ has to offer. It is built keeping in mind that the reader is new to the world of ruby and is developing his/her first Ruby on Rails application.&lt;br /&gt;
The motive of this wiki page is to make the reader understand how the Cookbook application was built. And on the way to building this application, user will be introduced to some key concepts like Model, Views and Controllers. At the end of this page, reader must fairly be ready to start building his/her basic Ruby on Rails application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;A simple application to start -- Cookbook application&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Start a new project. While creating a new project, remember to select Rails Application option as follows&lt;br /&gt;
&lt;br /&gt;
[[File:Start_application.png|Start a new Application]] &lt;br /&gt;
&lt;br /&gt;
When we create a Rails application, rails automatically help us create a brunch of files and folders as follows:&lt;br /&gt;
&lt;br /&gt;
[[File:Listing_of_cookbook_application.png|Listing_of_cookbook_application]] &lt;br /&gt;
&lt;br /&gt;
We mainly use the app directory. This is where the heart of the application is stored i.e. Models, Views and controllers.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Using scaffold to create framework&amp;lt;/h2&amp;gt;&lt;br /&gt;
Rails scaffolding is a quick way to generate some of the major pieces of an application. If you want to create the models, views, and controllers for a new resource in a single operation, scaffolding is the tool for the job.&amp;lt;br&amp;gt;&lt;br /&gt;
In Rubymine, we can create a scaffold by right clicking on the current Project-&amp;gt;New-&amp;gt;Run Rails Generator -&amp;gt;scaffold. This will open a dialog box to create a scaffold where we can enter the name and the attributes along with their data type. Once the scaffold has been created RubyMine will generate the model, view and controller files respectively. These files do not contain any specific methods but they provide a framework which is useful for general applications. The user can modify the MVC behavior according to his application requirements. Each scaffold has create, read, update and delete methods declared in the controller class. A user can modify the behavior of the controller through these methods. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;[[File:Create_Scaffold.png|Creating a new scaffold]]&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Scaffold.png|Scaffold]]          [[File:New_Scaffold_Categories.png|Creating scaffold for categories]]&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the Cookbook application we are considering a Cookbook which contains recipes for various dishes. The recipes have been divided into different categories, for example there is a recipe for ice tea which belongs to the category of beverages. There is a relationship which is created between a recipe and a category which can be expressed as an ice tea belongs to beverages. We can have many such recipes belonging to a single category, in this case many to one relationship is established. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Controllers for CookBook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
The Cookbook application starts by entering a URL in the browser which sends the request to the web server. Since our web server supports Rails application, it will hand over the control to a Rails controller.&lt;br /&gt;
A controller ‘controls’ the entire application by responding to the incoming requests as needed. For example, consider the following URL http://localhost:3000/categories/show which is decoded by the controller to know that its action named show has been invoked.&lt;br /&gt;
All the controllers inherit only the ApplicationController which in turn inherits the ActionController::Base class. ActionContoller module provides the basic support for the controllers in Rails.&lt;br /&gt;
In our Cookbook application we generate Categories controller using the scaffold mechanism which populates the controller with the basic methods such as index, create, show, new, edit, update and destroy.&lt;br /&gt;
A simple example of index method is as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    @categories = Category.all&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # index.html.erb&lt;br /&gt;
      format.json { render json: @categories }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In ruby, @ operator is used to declare instance variables. The index method makes use of @categories instance variable which is populated with all the records in the Category table. The respond_to method is used for responding to particular type of request. It contains a block and a variable |format| which determines whether the incoming request should be responded with HTML (when we are interacting with a user) or JSON format (if we are returning an object). &lt;br /&gt;
After executing the code, the controller will render a template of the same name. (For this example, the template is views/categories/index.html.erb).&lt;br /&gt;
Similarly the show method is defined as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def show&lt;br /&gt;
    @category = Category.find(params[:id])&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # show.html.erb&lt;br /&gt;
      format.json { render json: @category }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The show method is used to display a particular object. The show method needs to access database to search for the given object. This is done using the find method which takes params[:id] as argument. The params[:id] contains the id or primary key of the object which is passed as a parameter to look up in the database and the result is stored in the instance variable. The result is displayed by rendering the show.html.erb file.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Active Record&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, a program deals with objects which are mapped into database relational databases. This kind of mapping is facilitated by the Active Record.&amp;lt;br&amp;gt;Active Record maps the Rails classes in to database tables and rails objects into database records.&lt;br /&gt;
This makes it easy to perform actions on database tables by invoking the class methods. Another benefit of Active Record is database migration. Migrations can manage the evolution of a schema used by several physical databases. It’s a solution to the common problem of adding a field to make a new feature work in your local database, but being unsure of how to push that change to other developers and to the production server. With migrations, you can describe the transformations in self-contained classes that can be checked into version control systems and executed against another database that might be one, two, or five versions behind.You can find a lot more details about rails migration here[8].&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Method Pairs&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;There are couple of method pairs such as new-create and edit-update which are executed in sequence. In method pair one method is responsible for display preparation and the other method validates the data and attempts it to save in the database.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def new&lt;br /&gt;
    @category = Category.new&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # new.html.erb&lt;br /&gt;
      format.json { render json: @category }&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def create&lt;br /&gt;
    @category = Category.new(params[:category])&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      if @category.save&lt;br /&gt;
        format.html { redirect_to @category, notice: 'Category was successfully created.' }&lt;br /&gt;
        format.json { render json: @category, status: :created, location: @category }&lt;br /&gt;
      else&lt;br /&gt;
        format.html { render action: &amp;quot;new&amp;quot; }&lt;br /&gt;
        format.json { render json: @category.errors, status: :unprocessable_entity }&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The new and create methods are known as method pairs which execute in sequence. The new method is used to instantiate a new Category object whereas create method is used to successfully save the object to the database. After execution of the new method, the controller renders the new.html.erb file which displays a form to create a new Category object. The user enters the required information in the form and the object id is returned in the URL on submitting the form. Consider the following URL is generated on submitting the form: http: //localhost:3000/categories/3.&lt;br /&gt;
This means that Category object with id =3 will be created. The details of the object will be processed by the create method. &lt;br /&gt;
The controller invokes the create method to store the object in the database. An observation can be made about the @category variable in the create method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@category = Category.new(params[:category])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The parameters passed in the URL after creating a new object is stored in the params hash (denoted by params[]). In this case the params[] contains an object with category_id = 3. The params[] assigns the data of object with category_id =3 to the instance variable. The data entered in the instance variable is validated using the @category.save method. If no errors are found then the object stored in the database and a message on successful creation of the object is displayed to the user. However, if the data entered in the object contains an error, the user is redirected to the new.html.erb page to resubmit the information and the errors are displayed. &lt;br /&gt;
Another example of such method pair is the edit and update methods. The edit method displays the table entry for an existing object, while the update method is invoked when the changes have been submitted.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Models of CookBook Application&amp;lt;/h1&amp;gt;&lt;br /&gt;
Model handles the data processing in a web application. Actions interact with the model to manipulate the data. Rails is capable of handling databases and this is facilitated by the model. You can base models on the Rails ActiveRecord module which contains support databases.&lt;br /&gt;
In our Cookbook application we have two models namely: Category and Recipe. They are represented by files category.rb and recipe.rb and each file is used to describe the relation between two classes. Let’s take a look at the two models&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt; category.rb&amp;lt;/b&amp;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;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;recipe.rb&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;lt;b&amp;gt;Relationships in models&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;one-to-one&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Syllabus &amp;lt; ActiveRecord::Base&lt;br /&gt;
	belongs_to :course¬&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Course &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_one :syllabus&lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;many-many&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Student &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_and_belongs_to_many :courses&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Course &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_and_belongs_to_many :students&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In cookbook, we have many-one relationship between recipes and categories.&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Views of Cookbook application&amp;lt;/h1&amp;gt;&lt;br /&gt;
What is V in MVC? ‘V’ refers to views. Views are the actual interface that users can see. It is through views, that a user may request some data to be viewed or submits some data for processing.&amp;lt;br&amp;gt;&lt;br /&gt;
Views in rails are of the form file_name.html.erb. Here erb means embedded ruby. What html.erb denotes is some ruby code is embedded in HTML code. Like HTML, embedded ruby also uses tags to represent data. However, to distinguish it from the HTML code, ruby is embedded between &amp;lt;% %&amp;gt; if some non-assignment ruby operation is to be performed. If something needs to be displayed on screen, &amp;lt;%= %&amp;gt; is used.&lt;br /&gt;
Views are present in “app/views” folder in the rails application.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Name of the view files is same as the name of their respective controller. For e.g.:- if I have a controller movie with an action new, the corresponding view page will be new.html.erb in the app/views/movie directory.&amp;lt;br&amp;gt;&lt;br /&gt;
The cookbook application views are as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Recipe Controller Views&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
1. index.html.erb&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;nowiki&amp;gt;&amp;lt;h1&amp;gt;Listing categories&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
  &amp;lt;tr&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;Name&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;% @categories.each do |category| %&amp;gt;&lt;br /&gt;
  &amp;lt;tr&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= category.name %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Show', category %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Edit', edit_category_path(category) %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Destroy', category, method: :delete, data: { confirm: 'Are you sure?' } %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%= link_to 'New Category', new_category_path %&amp;gt;&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;% @recipes.each do |recipe| %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
Here @recipes is an instance variable of model recipe defined in index action of the controller ‘recipes’. It contains all the recipes currently present in our application. Then each recipe is taken from this instance variable and is assigned to the local variable recipe.  All the subsequent lines display the recipe details (name, description and instruction).&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%= link_to ‘Show’, recipe %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
link_to is an ruby operator which is used to redirect the control to some other page. ‘Show’ is displayed as it is on the page and is an hyperlink. It links to a page pointed by recipe.  The path is defined in the routes.rb file. &lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
2. _form.html.erb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is a partial. Partials are devices used for breaking the code in smaller, more manageable chunks. This helps in modularity and also maintain DRY principle[4]. With a partial, code for rendering a particular response is moved to its own file. So for e.g. we can use this same partial for creating and editing information, because the information involved is essentially the same.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= form_for(@recipe) do |f| %&amp;gt;&lt;br /&gt;
  &amp;lt;% if @recipe.errors.any? %&amp;gt;&lt;br /&gt;
    &amp;lt;div id=&amp;quot;error_explanation&amp;quot;&amp;gt;&lt;br /&gt;
      &amp;lt;h2&amp;gt;&amp;lt;%= pluralize(@recipe.errors.count, &amp;quot;error&amp;quot;) %&amp;gt; prohibited this recipe from being saved:&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      &amp;lt;ul&amp;gt;&lt;br /&gt;
      &amp;lt;% @recipe.errors.full_messages.each do |msg| %&amp;gt;&lt;br /&gt;
        &amp;lt;li&amp;gt;&amp;lt;%= msg %&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
      &amp;lt;% end %&amp;gt;&lt;br /&gt;
      &amp;lt;/ul&amp;gt;&lt;br /&gt;
    &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;% end %&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :name %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_field :name %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
    &amp;lt;%=  f.label :category %&amp;gt;&lt;br /&gt;
    &amp;lt;%=    select('recipe','category_id', Category.all.collect {|c| [c[:name],c[:id]]}) %&amp;gt;&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :description %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_field :description %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :instructions %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_area :instructions %&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.submit %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;&amp;lt;%= form_for(@recipe) do |f| %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
This statement means to render a form for the instance variable @recipe.&lt;br /&gt;
If this page is being used for creating a new recipe, then the values for each key in the @recipe hash will be empty. If it is used for editing a recipe, @recipe will contain information corresponding to the recipe being edited.&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;% if @recipe.errors.any? %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
While saving an recipe or updating a recipe in the database, some violations may occur. Like the recipe name is left empty or its description is empty and so on. So while saving this information, database will signal some error. The errors are sent back to this page and are caught in @recipe.errors.any? statement.&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%=    select('recipe','category_id', Category.all.collect {|c| [c[:name],c[:id]]}) %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
This statement creates a dropdown box.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;‘recipe’ : refers to the object to which the selected value is to be assigned. In this case , it is the recipe object&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;‘category_id’ : create a ‘category_id’ key in the recipe hash.&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;Category.all.collect {|c| [c[:name],c[:id]]} : recover all categories from the database and list them  in the dropdown box. The value for each name is c[:id]. Whenever a category is selected by the user, its corresponding id is assigned to the ‘category_id’ key in the ‘recipe’ object.&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
3. new.html.erb&amp;lt;br&amp;gt;&lt;br /&gt;
This page is used to create a new recipe. This page simply renders the partial described above.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;New recipe&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;%= render 'form' %&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%= render ‘form’ %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
This statement renders the partial.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
4. edit.html.erb&lt;br /&gt;
This page is used to edit a existing recipe. This page also renders the partial.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Editing recipe&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;%= render 'form' %&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Show', @recipe %&amp;gt; |&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
5. show.html.erb&lt;br /&gt;
This page is used to show a particular recipe requested by the user. The recipe requested is captured by the controller in the @recipe instance variable and is passed to this view.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;p id=&amp;quot;notice&amp;quot;&amp;gt;&amp;lt;%= notice %&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Name:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.name %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Description:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.description %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Instructions:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.instructions %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Edit', edit_recipe_path(@recipe) %&amp;gt; |&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;The Category Views are very similar to the recipe Views and can be created similarly.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Conclusion&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
MVC was first proposed by Trygve Reenskaug in 1979. He broke applications into three types of components: models, which is responsible for maintaining the state of the application; views, which are responsible for generating a user interface; and controllers, which orchestrate the applications by receiving events from outside world, interact with model, and display an appropriate view to user. This separation of concerns led to far less computing, and in turn made the code easier to write and maintain.&amp;lt;br&amp;gt;&lt;br /&gt;
Use Ruby on Rails can implement MVC to build various types of web applications. The Cookbook is an easy starter. Readers can learn the basics of Ruby on Rails from this page.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;References&amp;lt;/h1&amp;gt;&lt;br /&gt;
*[1] http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller&lt;br /&gt;
*[2] http://en.wikipedia.org/wiki/Ruby_on_Rails &lt;br /&gt;
*[3] Sam Ruby, Dave Thomas, David Heinemeier Hansson.agile web development with rails (fourth edition)&lt;br /&gt;
*[4] http://en.wikipedia.org/wiki/Don't_repeat_yourself&lt;br /&gt;
*[5] http://guides.rubyonrails.org/getting_started.html&lt;br /&gt;
*[6] http://api.rubyonrails.org/classes/ActiveRecord/Migration.html&lt;br /&gt;
*[7] http://en.wikipedia.org/wiki/Metaprogramming&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The old wiki can be found here http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch4_4d_ch.&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Listing_of_cookbook_application.png&amp;diff=66897</id>
		<title>File:Listing of cookbook application.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Listing_of_cookbook_application.png&amp;diff=66897"/>
		<updated>2012-10-04T00:41:35Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: Listing directory&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Listing directory&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w38&amp;diff=66854</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w38</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w38&amp;diff=66854"/>
		<updated>2012-10-04T00:27:22Z</updated>

		<summary type="html">&lt;p&gt;Nmsarda: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;&amp;lt;ul&amp;gt;Introduction&amp;lt;/ul&amp;gt;&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;&amp;lt;ul&amp;gt;Model View Controller (MVC) Architecture&amp;lt;/ul&amp;gt;&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The purpose of creating MVC architecture is to have clear separation between the responsibilities of business logic and representation. In a nutshell the responsibilities are mentioned as below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt; Image &amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Model-View-Controller (MVC)[1] is an architectural pattern used in software engineering.In complex computer applications that present a large amount of data to the user, a developer often wishes to separate data (model) and user interface (view) concerns, so that changes to the user interface will not affect data handling, and that the data can be reorganized without changing the user interface.&amp;lt;br&amp;gt;&lt;br /&gt;
The model-view-controller solves this problem by decoupling data access and business logic from data presentation and user interaction, by introducing an intermediate component: the controller. Having described the responsibilities of three components let’s take a look how these components interact with each other.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt; Image &amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;&amp;lt;ul&amp;gt;Ruby on Rails&amp;lt;/ul&amp;gt;&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby on Rails[2] is an MVC framework. Rails enforces a structure for application, it helps to knits the models, views, and controllers as separate chunks of functionality while the program executes. No more external configuration needed. Rails is packed with features that make you more productive, with many of the following features building on one other.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Metaprogramming[7]:&amp;lt;/b&amp;gt; Other frameworks use extensive code generation from scratch. Metaprogramming[9] techniques use programs to write programs. Ruby is one of the best languages for metaprogramming, and Rails uses this capability well. Rails also uses code generation but relies much more on metaprogramming for the heavy lifting.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Active Record:&amp;lt;/b&amp;gt; Rails introduces the Active Record[6] framework, which saves objects to the database. The Rails version of Active Record discovers the columns in a database schema and automatically attaches them to your domain objects using metaprogramming.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Convention over configuration:&amp;lt;/b&amp;gt; Most web development frameworks for .NET or Java force you to write pages of configuration code. If you follow suggested naming conventions, Rails doesn't need much configuration.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Scaffolding:&amp;lt;/b&amp;gt; You often create temporary code in the early stages of development to help get an application up quickly and see how major components work together. Rails automatically creates much of the scaffolding you'll need.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Built-in testing:&amp;lt;/b&amp;gt; Rails creates simple automated tests you can then extend. Rails also provides supporting code called harnesses and fixtures that make test cases easier to write and run. Ruby can then execute all your automated tests with the rake utility.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Three environments:&amp;lt;/b&amp;gt; Rails gives you three default environments: development, testing, and production. Each behaves slightly differently, making your entire software development cycle easier. For example, Rails creates a fresh copy of the Test database for each test run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;&amp;lt;ul&amp;gt;CookBook Application&amp;lt;/ul&amp;gt;&amp;lt;/h1&amp;gt;&lt;br /&gt;
Cookbook Application is a very basic Ruby on Rails application. It is a great way to understand the nuances that ‘Ruby’ has to offer. It is built keeping in mind that the reader is new to the world of ruby and is developing his/her first Ruby on Rails application.&lt;br /&gt;
The motive of this wiki page is to make the reader understand how the Cookbook application was built. And on the way to building this application, user will be introduced to some key concepts like Model, Views and Controllers. At the end of this page, reader must fairly be ready to start building his/her basic Ruby on Rails application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;&amp;lt;ul&amp;gt;A simple application to start -- Cookbook application&amp;lt;/ul&amp;gt;&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Start a new project. While creating a new project, remember to select Rails Application option as follows&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt; Image &amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When we create a Rails application, rails automatically help us create a brunch of files and folders as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt; Image &amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We mainly use the app directory. This is where the heart of the application is stored i.e. Models, Views and controllers.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;&amp;lt;ul&amp;gt;Using scaffold to create framework&amp;lt;/ul&amp;gt;&amp;lt;/h1&amp;gt;&lt;br /&gt;
Rails scaffolding is a quick way to generate some of the major pieces of an application. If you want to create the models, views, and controllers for a new resource in a single operation, scaffolding is the tool for the job.&amp;lt;br&amp;gt;&lt;br /&gt;
In Rubymine, we can create a scaffold by right clicking on the current Project-&amp;gt;New-&amp;gt;Run Rails Generator -&amp;gt;scaffold. This will open a dialog box to create a scaffold where we can enter the name and the attributes along with their data type. Once the scaffold has been created RubyMine will generate the model, view and controller files respectively. These files do not contain any specific methods but they provide a framework which is useful for general applications. The user can modify the MVC behavior according to his application requirements. Each scaffold has create, read, update and delete methods declared in the controller class. A user can modify the behavior of the controller through these methods. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;[[File:Create_Scaffold.png|Creating a new scaffold]]&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Scaffold.png|Scaffold]]          [[File:New_Scaffold_Categories.png|Creating scaffold for categories]]&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the Cookbook application we are considering a Cookbook which contains recipes for various dishes. The recipes have been divided into different categories, for example there is a recipe for ice tea which belongs to the category of beverages. There is a relationship which is created between a recipe and a category which can be expressed as an ice tea belongs to beverages. We can have many such recipes belonging to a single category, in this case many to one relationship is established. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;&amp;lt;ul&amp;gt;Controllers for CookBook Application&amp;lt;/ul&amp;gt;&amp;lt;/h1&amp;gt;&lt;br /&gt;
The Cookbook application starts by entering a URL in the browser which sends the request to the web server. Since our web server supports Rails application, it will hand over the control to a Rails controller.&lt;br /&gt;
A controller ‘controls’ the entire application by responding to the incoming requests as needed. For example, consider the following URL http://localhost:3000/categories/show which is decoded by the controller to know that its action named show has been invoked.&lt;br /&gt;
All the controllers inherit only the ApplicationController which in turn inherits the ActionController::Base class. ActionContoller module provides the basic support for the controllers in Rails.&lt;br /&gt;
In our Cookbook application we generate Categories controller using the scaffold mechanism which populates the controller with the basic methods such as index, create, show, new, edit, update and destroy.&lt;br /&gt;
A simple example of index method is as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def index&lt;br /&gt;
    @categories = Category.all&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # index.html.erb&lt;br /&gt;
      format.json { render json: @categories }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In ruby, @ operator is used to declare instance variables. The index method makes use of @categories instance variable which is populated with all the records in the Category table. The respond_to method is used for responding to particular type of request. It contains a block and a variable |format| which determines whether the incoming request should be responded with HTML (when we are interacting with a user) or JSON format (if we are returning an object). &lt;br /&gt;
After executing the code, the controller will render a template of the same name. (For this example, the template is views/categories/index.html.erb).&lt;br /&gt;
Similarly the show method is defined as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def show&lt;br /&gt;
    @category = Category.find(params[:id])&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # show.html.erb&lt;br /&gt;
      format.json { render json: @category }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The show method is used to display a particular object. The show method needs to access database to search for the given object. This is done using the find method which takes params[:id] as argument. The params[:id] contains the id or primary key of the object which is passed as a parameter to look up in the database and the result is stored in the instance variable. The result is displayed by rendering the show.html.erb file.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Active Record&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
In ruby, a program deals with objects which are mapped into database relational databases. This kind of mapping is facilitated by the Active Record.&amp;lt;br&amp;gt; &amp;lt;b&amp;gt;Active Record maps the Rails classes in to database tables and rails objects into database records.&amp;lt;/b&amp;gt;&lt;br /&gt;
This makes it easy to perform actions on database tables by invoking the class methods. Another benefit of Active Record is database migration. Migrations can manage the evolution of a schema used by several physical databases. It’s a solution to the common problem of adding a field to make a new feature work in your local database, but being unsure of how to push that change to other developers and to the production server. With migrations, you can describe the transformations in self-contained classes that can be checked into version control systems and executed against another database that might be one, two, or five versions behind.You can find a lot more details about rails migration here[8].&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Method Pairs&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;There are couple of method pairs such as new-create and edit-update which are executed in sequence. In method pair one method is responsible for display preparation and the other method validates the data and attempts it to save in the database.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def new&lt;br /&gt;
    @category = Category.new&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      format.html # new.html.erb&lt;br /&gt;
      format.json { render json: @category }&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def create&lt;br /&gt;
    @category = Category.new(params[:category])&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      if @category.save&lt;br /&gt;
        format.html { redirect_to @category, notice: 'Category was successfully created.' }&lt;br /&gt;
        format.json { render json: @category, status: :created, location: @category }&lt;br /&gt;
      else&lt;br /&gt;
        format.html { render action: &amp;quot;new&amp;quot; }&lt;br /&gt;
        format.json { render json: @category.errors, status: :unprocessable_entity }&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The new and create methods are known as method pairs which execute in sequence. The new method is used to instantiate a new Category object whereas create method is used to successfully save the object to the database. After execution of the new method, the controller renders the new.html.erb file which displays a form to create a new Category object. The user enters the required information in the form and the object id is returned in the URL on submitting the form. Consider the following URL is generated on submitting the form: http: //localhost:3000/categories/3.&lt;br /&gt;
This means that Category object with id =3 will be created. The details of the object will be processed by the create method. &lt;br /&gt;
The controller invokes the create method to store the object in the database. An observation can be made about the @category variable in the create method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@category = Category.new(params[:category])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The parameters passed in the URL after creating a new object is stored in the params hash (denoted by params[]). In this case the params[] contains an object with category_id = 3. The params[] assigns the data of object with category_id =3 to the instance variable. The data entered in the instance variable is validated using the @category.save method. If no errors are found then the object stored in the database and a message on successful creation of the object is displayed to the user. However, if the data entered in the object contains an error, the user is redirected to the new.html.erb page to resubmit the information and the errors are displayed. &lt;br /&gt;
Another example of such method pair is the edit and update methods. The edit method displays the table entry for an existing object, while the update method is invoked when the changes have been submitted.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;&amp;lt;ul&amp;gt;Models of CookBook Application&amp;lt;/ul&amp;gt;&amp;lt;/h1&amp;gt;&lt;br /&gt;
Model handles the data processing in a web application. Actions interact with the model to manipulate the data. Rails is capable of handling databases and this is facilitated by the model. You can base models on the Rails ActiveRecord module which contains support databases.&lt;br /&gt;
In our Cookbook application we have two models namely: Category and Recipe. They are represented by files category.rb and recipe.rb and each file is used to describe the relation between two classes. Let’s take a look at the two models&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt; category.rb&amp;lt;/b&amp;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;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;recipe.rb&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;lt;b&amp;gt;Relationships in models&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;one-to-one&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Syllabus &amp;lt; ActiveRecord::Base&lt;br /&gt;
	belongs_to :course¬&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Course &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_one :syllabus&lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;many-many&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Student &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_and_belongs_to_many :courses&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Course &amp;lt; ActiveRecord::Base&lt;br /&gt;
	has_and_belongs_to_many :students&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In cookbook, we have many-one relationship between recipes and categories.&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;&amp;lt;ul&amp;gt;Views of Cookbook application&amp;lt;/ul&amp;gt;&amp;lt;/h1&amp;gt;&lt;br /&gt;
What is V in MVC? ‘V’ refers to views. Views are the actual interface that users can see. It is through views, that a user may request some data to be viewed or submits some data for processing.&amp;lt;br&amp;gt;&lt;br /&gt;
Views in rails are of the form file_name.html.erb. Here erb means embedded ruby. What html.erb denotes is some ruby code is embedded in HTML code. Like HTML, embedded ruby also uses tags to represent data. However, to distinguish it from the HTML code, ruby is embedded between &amp;lt;% %&amp;gt; if some non-assignment ruby operation is to be performed. If something needs to be displayed on screen, &amp;lt;%= %&amp;gt; is used.&lt;br /&gt;
Views are present in “app/views” folder in the rails application.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Name of the view files is same as the name of their respective controller. For e.g.:- if I have a controller movie with an action new, the corresponding view page will be new.html.erb in the app/views/movie directory.&amp;lt;br&amp;gt;&lt;br /&gt;
The cookbook application views are as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt; Recipe Controller Views&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
1. index.html.erb&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;nowiki&amp;gt;&amp;lt;h1&amp;gt;Listing categories&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
  &amp;lt;tr&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;Name&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
    &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;% @categories.each do |category| %&amp;gt;&lt;br /&gt;
  &amp;lt;tr&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= category.name %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Show', category %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Edit', edit_category_path(category) %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;%= link_to 'Destroy', category, method: :delete, data: { confirm: 'Are you sure?' } %&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%= link_to 'New Category', new_category_path %&amp;gt;&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;% @recipes.each do |recipe| %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
Here @recipes is an instance variable of model recipe defined in index action of the controller ‘recipes’. It contains all the recipes currently present in our application. Then each recipe is taken from this instance variable and is assigned to the local variable recipe.  All the subsequent lines display the recipe details (name, description and instruction).&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%= link_to ‘Show’, recipe %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
link_to is an ruby operator which is used to redirect the control to some other page. ‘Show’ is displayed as it is on the page and is an hyperlink. It links to a page pointed by recipe.  The path is defined in the routes.rb file. &lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
2. _form.html.erb&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is a partial. Partials are devices used for breaking the code in smaller, more manageable chunks. This helps in modularity and also maintain DRY principle[4]. With a partial, code for rendering a particular response is moved to its own file. So for e.g. we can use this same partial for creating and editing information, because the information involved is essentially the same.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= form_for(@recipe) do |f| %&amp;gt;&lt;br /&gt;
  &amp;lt;% if @recipe.errors.any? %&amp;gt;&lt;br /&gt;
    &amp;lt;div id=&amp;quot;error_explanation&amp;quot;&amp;gt;&lt;br /&gt;
      &amp;lt;h2&amp;gt;&amp;lt;%= pluralize(@recipe.errors.count, &amp;quot;error&amp;quot;) %&amp;gt; prohibited this recipe from being saved:&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
      &amp;lt;ul&amp;gt;&lt;br /&gt;
      &amp;lt;% @recipe.errors.full_messages.each do |msg| %&amp;gt;&lt;br /&gt;
        &amp;lt;li&amp;gt;&amp;lt;%= msg %&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
      &amp;lt;% end %&amp;gt;&lt;br /&gt;
      &amp;lt;/ul&amp;gt;&lt;br /&gt;
    &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;% end %&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :name %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_field :name %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
    &amp;lt;%=  f.label :category %&amp;gt;&lt;br /&gt;
    &amp;lt;%=    select('recipe','category_id', Category.all.collect {|c| [c[:name],c[:id]]}) %&amp;gt;&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :description %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_field :description %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.label :instructions %&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.text_area :instructions %&amp;gt;&lt;br /&gt;
    &amp;lt;%= f.submit %&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;&amp;lt;%= form_for(@recipe) do |f| %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
This statement means to render a form for the instance variable @recipe.&lt;br /&gt;
If this page is being used for creating a new recipe, then the values for each key in the @recipe hash will be empty. If it is used for editing a recipe, @recipe will contain information corresponding to the recipe being edited.&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;% if @recipe.errors.any? %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
While saving an recipe or updating a recipe in the database, some violations may occur. Like the recipe name is left empty or its description is empty and so on. So while saving this information, database will signal some error. The errors are sent back to this page and are caught in @recipe.errors.any? statement.&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%=    select('recipe','category_id', Category.all.collect {|c| [c[:name],c[:id]]}) %&amp;gt;&amp;lt;/b&amp;gt;&lt;br /&gt;
This statement creates a dropdown box.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;‘recipe’ : refers to the object to which the selected value is to be assigned. In this case , it is the recipe object&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;‘category_id’ : create a ‘category_id’ key in the recipe hash.&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;Category.all.collect {|c| [c[:name],c[:id]]} : recover all categories from the database and list them  in the dropdown box. The value for each name is c[:id]. Whenever a category is selected by the user, its corresponding id is assigned to the ‘category_id’ key in the ‘recipe’ object.&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
3. new.html.erb&amp;lt;br&amp;gt;&lt;br /&gt;
This page is used to create a new recipe. This page simply renders the partial described above.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;New recipe&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;%= render 'form' %&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*&amp;lt;b&amp;gt;&amp;lt;%= render ‘form’ %&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
This statement renders the partial.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
4. edit.html.erb&lt;br /&gt;
This page is used to edit a existing recipe. This page also renders the partial.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Editing recipe&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;%= render 'form' %&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Show', @recipe %&amp;gt; |&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
5. show.html.erb&lt;br /&gt;
This page is used to show a particular recipe requested by the user. The recipe requested is captured by the controller in the @recipe instance variable and is passed to this view.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;p id=&amp;quot;notice&amp;quot;&amp;gt;&amp;lt;%= notice %&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Name:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.name %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Description:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.description %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
  &amp;lt;b&amp;gt;Instructions:&amp;lt;/b&amp;gt;&lt;br /&gt;
  &amp;lt;%= @recipe.instructions %&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;%= link_to 'Edit', edit_recipe_path(@recipe) %&amp;gt; |&lt;br /&gt;
&amp;lt;%= link_to 'Back', recipes_path %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;The Category Views are very similar to the recipe Views and can be created similarly.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;&amp;lt;ul&amp;gt;Conclusion&amp;lt;/ul&amp;gt;&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
MVC was first proposed by Trygve Reenskaug in 1979. He broke applications into three types of components: models, which is responsible for maintaining the state of the application; views, which are responsible for generating a user interface; and controllers, which orchestrate the applications by receiving events from outside world, interact with model, and display an appropriate view to user. This separation of concerns led to far less computing, and in turn made the code easier to write and maintain.&amp;lt;br&amp;gt;&lt;br /&gt;
Use Ruby on Rails can implement MVC to build various types of web applications. The Cookbook is an easy starter. Readers can learn the basics of Ruby on Rails from this page.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;&amp;lt;ul&amp;gt;References&amp;lt;/ul&amp;gt;&amp;lt;/h1&amp;gt;&lt;br /&gt;
*[1] http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller&lt;br /&gt;
*[2] http://en.wikipedia.org/wiki/Ruby_on_Rails &lt;br /&gt;
*[3] Sam Ruby, Dave Thomas, David Heinemeier Hansson.agile web development with rails (fourth edition)&lt;br /&gt;
*[4] http://en.wikipedia.org/wiki/Don't_repeat_yourself&lt;br /&gt;
*[5] http://guides.rubyonrails.org/getting_started.html&lt;br /&gt;
*[6] http://api.rubyonrails.org/classes/ActiveRecord/Migration.html&lt;br /&gt;
*[7] http://en.wikipedia.org/wiki/Metaprogramming&lt;/div&gt;</summary>
		<author><name>Nmsarda</name></author>
	</entry>
</feed>