Ruby
Ruby is an open source, object oriented scripting language introduced by Yukihiro Matsumoto (known widely as Matz) in 1994 and was officially released by Matz in 1995. It is a scripting language with features similar to the Python and Perl.
Rails
Rails framework was designed and given by David Heinemeier Hansson in 1994. It was developed under the MIT License. The web application framework given by David Heinemeier Hansson was named as Ruby on Rails.
Ruby on Rails
Ruby and Rails are taken together because Ruby is the base foundation for Rails ( Rails is a full-stack web application framework written in Ruby) so they have a sort of parent child relationship with each other.
Ruby on Rails is an environment which provide us productive framework along with the concepts of Object Oriented programming and hence is a very efficient way of developing fully featured web applications in less time and with less coding.
For working with Ruby on Rails, we can use the InstantRails installer which installs Ruby, Rails, the Apache web server and MySQL in one step.
When we unzip InstantRails software in our machine, we find a file InstantRails.exe in the folder where we have unzipped the files of InstantRails (I have unzipped the files in D:\InstantRails folder). When we double click on this InstantRails.exe file, we get an icon in the form of capital I on the task bar. When we right click on this icon in the task bar, we get a popup window from where we select the option : Open Ruby Console window as shown :
We get a command prompt where we can start building our application. Since, we our going to make our web application by name : shopping, we give following command :
D:\InstantRails\rails_apps>rails shopping
This command generates many files in their respective folders as shown below :
We can see that a list of the files under respective directories are created by Rails. All these files and sub directories are created in a folder named : shopping. The folders that are created are as shown :
The \app directory, for example, contains \controllers, \helpers, \models and \views. The \views directory itself contains a subdirectory, \layouts, and so on. We go into our rail application directory :
D:\InstantRails\rails_apps>cd shopping
Now, to run the server, can give either of following command :
Here script is a directory which was created when we executed the rails command and server is the name of a code file which runs the WEBrick server.
Now fire up a web browser. Enter the host name, followed by a colon and the port number into its address bar. The host name is localhost and the port number by default is 3000. Here is an example:
http://localhost:3000/
The browser should now display a page welcoming us aboard Rails as shown :
Creating a Controller
The Controller is the part of the application between the View (what appears in the browser) and the Model (Data). Lets create a Controller named : Welcome.
We open another ruby console window (by right clicking on this icon in the task bar, and selecting the option : Open Ruby Console window ) and go to our shopping folder and run the following command :
Several files and directories are created as shown above.
Rails has parsed the name Welcome to welcome into lowercase words. If our controller is of two words say WelcomeWorld, it will be parsed into welcome_world i.e. separated by an underscore. It is so because of the configuration by convention approach used by Rails.
We will first make use of welcome_controller.rb file created in \shopping\app\controllers folder. Open this file in a text editor. This empty method has been auto-generated:
class WelcomeController < ApplicationController
end
Inside this class we can write some code to be executed when a certain page is displayed.
Suppose, we edit this file and make its contents appear as :
class WelcomeController < ApplicationController
def index
render :text => "Welcome to our Shopping Mall "
end
def exit
render :text => "Thanks for visiting"
end
end
This controller file now contains two methods: index and exit. Each method contains a single line of code. Render is a method which takes a Hash as an argument; the Hash itself containing a key-value pair comprising a symbol and a string. It can be rewritten like this:
def index
render( { :text => " Welcome to our Shopping Mall " } )
end
Here in above methods : index and exit, we are just rendering (outputiing) a certain text to the browser. That is, when the browser accesses the web site and passes the method name to it, the respective text is rendered on the screen.
Open the web browser and enter the full address of the two functions : index or exit. The address becomes : http://localhost:3000), plus the name of the controller (/welcome) and finally the name of a specific method (/index or /exit) : http://localhost:3000/welcome/index
The output that we get is :
Similarly, if we give following address in the browser : http://localhost:3000/ welcome /exit
The output that we get is :
In fact, Rails uses the index method as a default so we can even omit that part of the URL. That is, index method is automatically invoked even by following address : http://localhost:3000/welcome