Ruby On Rails


Published on

A Quick Tutorial - Ruby on Rails

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Ruby On Rails

  1. 1. Aniruddha S Deshpande Arlington,Texas, USA 1-817-714-7899 Ruby on Rails Ruby on Rails is an extremely productive web application framework written in Ruby by David Heinemeier Hansson. This is a open source Ruby framework for developing database-backed web applications. Ruby on Rails has gained popularity in the enterprise development community among both programmers and system managers. As an open source platform, Ruby is proving to offer a number of advantages for powering enterprise applications, not the least of which is a shorter development time for robust applications and the creation of denser code that's easy to work with and maintain. What is Ruby?  A High Level Programming Language  Interpreted like Perl, Python  Object-Oriented like Java  Easy to learn.  Open source (very liberal license) Rich libraries  Very easy to extend  Less Coding with fewer bugs  Large and Helpful community What is Rails?  An extremely productive web-application framework.  Includes everything needed to create a database-driven web application using the Model- View-Controller pattern.  Requires fewer total lines of code than other frameworks spend setting up their XML configuration files.  You could develop a web application at least ten times faster with Rails than you could with a typical Java framework. Who is using Ruby on Rails? Twitter, Scribd,Yellowpages,ThoughtWorks,Amazon,Cisco,Oracle,IBM ... see the link
  2. 2. Installing Ruby (Windows) Download and Run the One-Click Installer from the following link: Installing Rails (Windows) Option 1> GUI based Rails You can download InstantRails, which has everything (Ruby, Rails, MySQL v4) in it. Option 2 (Recommended)> However, If you already have Ruby and some kind of DB installed, and want to use that for Rails. Just type the following command in console (Ensure ruby is in the PATH var): gem install rails --include-dependencies This will install the latest version of Rails Integrated Development Environment 1. NetBeans offers great features to develop Ruby/Rails Applications. 2. RadRails by Aptana is another very popular RoR IDE (free basic version) 3. Eclipse can also be used for RoR Development, but requires user to download a lot of packages. The following article tells you, how to set up RoR Environment for Eclipse Keeping Rails upto-date: gem update rails --include-dependencies
  3. 3. Rails Framework RoR has a Model-View-Controller Architecture. Model: Model maintains the 'state' of the application. It’s an interface to the Database Tables. Active Record is the Rails package, which provides the ORM Layer on top the Database tables. When, working with RoR, one interacts with Model objects for Database transactions, which are translated to Database tables seamlessly by the ORM layer (Active Record). View: The View is responsible for generating the user interface, based on data from the Model or the Controller. Controller: Controllers receive events from the outside world (e.g. user input), interacts with the Model, and displays an appropriate View to the user. Action Pack is the Rails package that provides the library for View and Controller operations. The following depicts the interactions in the MVC framework.
  4. 4. Creating a Sample Rails App: One of the biggest advantages of using RoR, is the speed at which one can get a jump-start on a project, enabling the customer to get a glimpse of the end product, in the very early stages of the development cycle. 1. To create a rails project, simply type the following command in your desired workspace rails <projectName> You will see that Rails automatically creates a hierarchy of folders and other important files for you, essential for any general project. For example: Say, We want to create a project called 'demo' We type C:rails_projects> rails demo Now, go into the demo project root directory C:rails_projects> cd demo C:rails_projectsdemo> You will find a directory structure as follows: demo/ ..../app ......../controller ......../helpers ......../models ......../views ............../layouts ..../components ..../config ..../db ..../doc ..../lib ..../log ..../public ..../script ..../test ..../tmp ..../vendor README Rakefile Now, run the default web server that Rails ports with, by typing the command: C:rails_projectsdemo> ruby script/server You are done! Goto the following URL to access your application: http://localhost:3000
  5. 5. Rails will welcome you with the following screen: Displaying 'Hello World!' To write a simple Hello world app, we need just the controller and view code and not the model, since we are not dealing with any DB yet. So, create a controller called 'hello' by typing following command: C:rails_projectsdemo>ruby script/generate controller hello You will see that Rails automatically creates a HelloController in the controller folder, so open it in your IDE and have a look at it. It will be like this: class HelloController < ApplicationController end It’s an empty class that inherits from ApplicationController, so it gets all the default Controller behavior from Rails. In order for the Controller to respond to user requests, we need to add 'action' to it. Let’s add an action called 'world', which any user can call to get some response.
  6. 6. class HelloController < ApplicationController def world end end Save it! Now, go to the following URL http://localhost:3000/hello/world The Rails URL follows a convention of domain/<ControllerName>/<ActionName> But, you will get an error message, saying 'Template Missing', that is because the action does not have an associated View, so Rails could not find it. Note, that Each Controller will have a folder named after it in the Views folder. So, go to the folder names 'hello' in the Views directory and create an html file called 'world.html' Add some HTML code to display 'Hello World!' in it. Save it! Now, again go to the same URL: http://localhost:3000/hello/world Done! Rails now shall display your 'Hello World!' message Recommended Books (Must Buy) 1. Agile Web Development with Rails (By the creator of RoR) 2. Programming Ruby Online Resources 1. 2. 3. 4. 5. 6.