47
Irma & Robert

Ruby On Rails

Embed Size (px)

DESCRIPTION

Ruby On Rails. Irma & Robert. Ruby On Rails. Irma & Robert. What is Ruby On Rails ?. Ruby on Rails is an open source web application framework for the Ruby programming language. It is often referred to as "Rails" or "RoR". - PowerPoint PPT Presentation

Citation preview

Page 1: Ruby On Rails

Irma &Robert

Page 2: Ruby On Rails

Irma &Robert

Page 3: Ruby On Rails

Ruby on Rails is an open source web application framework for the Ruby programming language. It is often referred to as "Rails" or "RoR".

It is intended to be used with an Agile development methodology, which is often utilized by web developers for its suitability for short, client-driven projects.

Page 4: Ruby On Rails

Ruby on Rails was created by David Heinemeier Hansson from his work on Basecamp, a project management tool by 37signals (now a web application company).

Heinemeier Hansson first released Rails as open source in July 2004, but did not share commit rights to the project until February 2005.

Page 5: Ruby On Rails

In August 2006 the framework reached a milestone when Apple announced that it would ship Ruby on Rails with Mac OS X v10.5 "Leopard", which was released in October 2007.

Like many contemporary web frameworks, Rails uses the Model-View-Controller (MVC) architecture pattern to organize application programming. Ruby on Rails features several tools intended to make commonplace development tasks easier "out of the box".

Page 6: Ruby On Rails

Rails provides scaffolding which can automatically construct some of the models and views needed for a basic website. A simple ruby web server (WEBrick) and Rake build system are also included. By including these common tools with the Rails framework, a basic development environment is in effect provided with all versions of the software.

Page 7: Ruby On Rails

Ruby on Rails relies on a web server to run it. Mongrel is generally preferred over WEBrick ,but it can also be run by Lighttpd, Apache with CGI (or FastCGI, mod ruby), and many others. Rails is also noteworthy for its extensive use of JavaScript libraries Prototype and Script.aculo.us for Ajax.

Page 8: Ruby On Rails

Rails initially utilized lightweight SOAP(Simple Object Access Protocol) for web services; this was later replaced by RESTful web services.

Since version 2.0, Ruby on Rails by default offers both HTML and XML as output formats. The latter is the facility for RESTful web services.

Page 9: Ruby On Rails

Ruby on Rails is separated into various packages, namely ActiveRecord (an object-relational mapping system for database access), ActiveResource (provides web services), ActionPack, ActiveSupport,ActiveView and ActionMailer. Prior to version 2.0, Rails also included the Action Web Service package which is now replaced by Active Resource. Apart from standard packages, developers can make plug-ins to extend existing packages.

Page 10: Ruby On Rails

Ruby on Rails is intended to emphasize Convention over Configuration (CoC), and the rapid development principle of Don't repeat yourself (DRY).

"Convention over Configuration" means a developer only needs to specify unconventional aspects of the application.

Page 11: Ruby On Rails

"Don't repeat yourself" means that information is located in a single, unambiguous place.

For example, using the ActiveRecord module of Rails, the developer does not need to specify database column names in class definitions.

Instead, Ruby on Rails can retrieve this information from the database.

Page 12: Ruby On Rails

Rails version 2.2 was released on November 21st, 2008 . The most notable features specified in 2.2 are an internationalization framework, thread safety, easy access to HTTP cashing, compatibility with Ruby 1.9 and JRuby, and new documentation.

Users can utilize the most recent unreleased versions, known as Edge Rails.

These users are typically more advanced, and are willing to deal with an unstable architecture for access to additional features.

Page 13: Ruby On Rails

The first step is to visit the Ruby On Rails Org. to download the newest revision.

Page 14: Ruby On Rails

There are some items that must be configured before Ruby is installed. Make sure that any and all application that are using port 80 are turn off. The most likely services are IIS, Apache and MySQL need to be disable or turn off.

This is a simple task go to the systems files in the Settings, Control Panel, Administrative Tools, Services and pick the appropriate application. Right click the service and turn off or disable.

Page 15: Ruby On Rails

I downloaded the files to the program files and saved in the Ruby On Rails folder.

Page 16: Ruby On Rails
Page 17: Ruby On Rails
Page 18: Ruby On Rails

Ruby is a web frame work which is a set of libraries.

Ruby is a scripting language that can be control by command line or XHTML.

Ruby is dynamic typing that allows for changes on a variable during run time. Ruby does not care what type of data it maybe. It will interpret any thing.

Page 19: Ruby On Rails

The first lesson is to use command line. I made a shortcut for Ruby on the desktop. Click the icon.

Page 20: Ruby On Rails

Once the icon is click the instant Rails window will appear.

Page 21: Ruby On Rails

Next click the I and chose the Rails Applications, open Ruby Console Window.

Page 22: Ruby On Rails

Then the command prompt will appear.

Page 23: Ruby On Rails

First type IRB and from this point it will be all cmd line instructions.

Page 24: Ruby On Rails

puts "Welcome to Ruby on Rails"

Page 25: Ruby On Rails

myvar = 5.7 puts "the value of myvar is #{myvar}" myvar = myvar.round puts "the value of myvar is rounded

#{myvar}" myvar = 5.4 puts "the value of myvar is #{myvar}" myvar.round puts "the rounded vaue of myvar

#{myvar}"

Page 26: Ruby On Rails

fruits = ["mango", "orange","apple","pear"] puts "The length of the fruits array is

#{fruits.length}" puts "The first fruit is #{fruits[0]}" puts "The first fruit is #{fruits[-1]}" fruits.reverse! puts "The first fruit is #{fruits.length}" puts "The first fruit is #{fruits[0]}" puts "The first fruit is #{fruits[-1]}" food = {"mango" => "fruits", "banana" => "fruit",

"onion" => "vegetable"} puts "The first fruit is #{food["mango"]}" puts "The first fruit is #{food["onion"]}"

Page 27: Ruby On Rails

class Point @@num_points = 0 def initialize(x,y) @x = x @y = y @@num_points +=1 end def to_s return "x #{@x}; y: #{@y}" end def num_points return @@num_points end end p = Point.new(8,9) q = Point.new(1,1) puts "the value of p is '#{p}'" puts "the value of q is '#{q}'" puts "the number of points created is #{p.num_points}"

Page 28: Ruby On Rails

Ruby on Rails has two classes, ActionController and ActionView, that work together to process a client request and render a view.

To generate a controller in Rails, you can use the built-in Controller generator by typing ruby script/generate controller name.

A Ruby on Rails application must be run from a web server Instant Rails comes with a built-in web server named Mongrel,

which is easy to use to test Rails applications on the local machine.

Page 29: Ruby On Rails

To make the controller in Ruby you use the built in Controller Generator. First go to the file where you will put the controller then type

ruby script/generate controller Name of controller

class Testa2aController < ApplicationControllerdef indexrender :text => "Welcome to the test file !"endend

Page 30: Ruby On Rails
Page 31: Ruby On Rails

Ruby provides a program called ERb (Embedded Ruby) Allows to put Ruby code inside HTML file You need to know only two things to prepare an ERb

document:◦ to execute Ruby code, enclose it between <% and %>◦ to get the result of the code execution to be printed out, as

part of the output, enclose the code between <%= and %> Here is an example, the code is saved in demo.rb file. Run the

program using the command line utility erb

Page 32: Ruby On Rails

when you use the RAILS helper script to create your application, it creates the entire directory structure for the application.

Except for minor changes between releases, every Rails project will have the same structure, with the same naming conventions

Page 33: Ruby On Rails

app : organizes application components. It’s got subdirectories that hold the view, controller and models

components: holds components tiny self-contained applications that bundle model, view and controller

config: contains small amount of configuration code that the application will need

db: you can manage the relational database with scripts you create and place in this directory

doc: this directory holds all the RubyDoc-generated Rails and application documentation

lib: you’ll put libraries here, unless explicitly belong elsewhere

log: errors logs go here public: has web files that don’t change, such as JavaScript

files (public/javascript), graphics ( public/images), stylesheets (public/ stylesheets) ad HTML files (public).

Page 34: Ruby On Rails

script: holds scripts to launch and manage the various tools that you’ll use with Rails

test: the tests you write and those Rails create for you tmp: Rails uses this directory to hold temporary files for

intermediate processing vendor: libraries provided by third-party vendors Apart from these directories there will be two files available

in your project directory:◦ README: this file contains a basic detail about Rails

Application and description of the directory structure explained above

◦ Rakefile: this file is similar to Unix Makefile which helps with building, packaging and testing the Rails code. This will be used by rake utility supplied along with Ruby installation

Page 35: Ruby On Rails

Workflow for creating Rails applications use the RAILS command to create the basic skeleton of the

application create the database on the MySQL server to hold your data configure the application to know where your database is

located and the login credentials for it create Rails Active Records (Models) because they are the

business objects you’ll be working with in your application generate Migrations that makes creating and maintaining

the database tables and columns easy write Controller Code to put a life in your application create Views to present your data through User Interface

Lets start with creating our library application

Page 36: Ruby On Rails

1. Go to Locomotive and create the application

2. This will create a subdirectory for the library application containing a complete directory tree of folders and files for an empty Rails application.

Page 37: Ruby On Rails

Start the server. This application runs on port 3012 Now open your browser and go to

◦ http://localhost:3012

Page 38: Ruby On Rails

Configuring database.yml◦ \library\config subdirectory of the Rails Application you

created.◦ This file has the configuration section for Mysql

databases. Create the databases using MySQL Administrator

Page 39: Ruby On Rails

Create the Active Record Files for our entities for library application, issue the following command from the top level of the application directory

When you use the generate tool, Rails creates the actual model file that holds all the methods unique to the model and the rules you define

Apart from creating many other files and directories, this will create files named book.rb and subject.rb

Page 40: Ruby On Rails

When you have more than one model in your Rails Application, you would need to create connection between those models.

Indicate these associations by adding declarations to your models ◦ modify book.rb and subject.rb

the implementation of validations is done in a Rails model◦ modify book.rb

Page 41: Ruby On Rails

Rails Migrations allows you to use ruby to define changes to your database schema, making it possible to use version control system to keep things synchronized with the actual code

Create the Migrations:

Notice that here you are using lower case for book and subject and using the plural form while creating migrations

Page 42: Ruby On Rails

Edit the code to tell it what to do:◦ go to db/migrate subdirectory and edit each file◦ modify 001_boks.rb◦ modify 002_subjects.rb

Page 43: Ruby On Rails

Run the migration◦ to do this go to terminal, go to the application directory

and run the rake command

◦ This will create a "schema_info" table if it doesn't exist which tracks the current version of the database - each new migration will be a new version, and any new migrations will be run until your database is at the current version.

Page 44: Ruby On Rails

The Rails controller is the logical center of your application. It coordinates the interaction between the user, the views, and the model.

We will create just one controller here:

let's define few method stubs in book_controller.rb.

Page 45: Ruby On Rails
Page 46: Ruby On Rails

Creating view file for LIST method◦ create and edit a file called list.rhtml

Creating view file for NEW method◦ create and edit a file called new.rhtml

Creating view file for SHOW method◦ create and edit a file called show.rhtml

Creating view file for EDIT method◦ create and edit a file called edit.rhtml

Creating view file for DELETE method◦ you do not need to write any view code for delete method◦ modify list.rhtml

Creating view file for SHOW_SUBJECTS method◦ create and edit a file called show_subjecs.rhtml in the app/views/book directory

Page 47: Ruby On Rails