+ All Categories
Home > Documents > RUBY ON RAILS Caner Çebi Emre Gürhan Işık H. Yılmaz Öztürk Hande İzmirlioğlu Özge...

RUBY ON RAILS Caner Çebi Emre Gürhan Işık H. Yılmaz Öztürk Hande İzmirlioğlu Özge...

Date post: 03-Jan-2016
Category:
Upload: luke-stewart
View: 227 times
Download: 0 times
Share this document with a friend
37
RUBY ON RAILS Caner Çebi Emre Gürhan Işık H. Yılmaz Öztürk Hande İzmirlioğlu Özge Bekleyen
Transcript
Page 1: RUBY ON RAILS Caner Çebi Emre Gürhan Işık H. Yılmaz Öztürk Hande İzmirlioğlu Özge Bekleyen.

RUBY ON RAILS

•Caner Çebi•Emre Gürhan Işık•H. Yılmaz Öztürk•Hande İzmirlioğlu•Özge Bekleyen

Page 2: RUBY ON RAILS Caner Çebi Emre Gürhan Işık H. Yılmaz Öztürk Hande İzmirlioğlu Özge Bekleyen.

A dynamic, pure object oriented programming language.

The code written in Ruby is not compiled into the machine language, it is stored as text for the Ruby interpreter, which takes this text and interprets it to make the machine work.

WHAT İS RUBY?

Page 3: RUBY ON RAILS Caner Çebi Emre Gürhan Işık H. Yılmaz Öztürk Hande İzmirlioğlu Özge Bekleyen.

In 1993 a japaneese guy Yukihiro Matsumoto ("Matz") started to work on Ruby.

He is inspired by Perl, Smalltalk and Lisp. It is named as a gemstone because the

creator is inspired by Perl too much . In 1993, Summer: First “Hello, world!”

program puts "Hello World!“ worked and its public release is in 1995. 2003, August 4: 1.8.0 is released. 2007,March:1.8.6 is released.

HİSTORY OF RUBY

Page 4: RUBY ON RAILS Caner Çebi Emre Gürhan Işık H. Yılmaz Öztürk Hande İzmirlioğlu Özge Bekleyen.

According to TIOBE index, it is ranked as 9th in terms of its growth among other programming languages. Beside being free of charge it is free to use, copy, modify and distribute.

Everything including classes and types (integers, booleans) are objects.

Flexible:class Numeric  def minus(x)    self.-(x)  endend

SOME DETAİLS AND FEATURES

Page 5: RUBY ON RAILS Caner Çebi Emre Gürhan Işık H. Yılmaz Öztürk Hande İzmirlioğlu Özge Bekleyen.

Inheritance with mixins, singleton methods, dynamic dispatch.

Ex: class MyArray

  include Enumerableend Exception handling:raise ArgumentError, "Illegal arguments!" begin# Do somethingrescue# Handle

exceptionend

FEATURES OF RUBY CONT.

Page 6: RUBY ON RAILS Caner Çebi Emre Gürhan Işık H. Yılmaz Öztürk Hande İzmirlioğlu Özge Bekleyen.

It has four levels of variable scope: local, global, class and instance and scope is resoluted by $ and @.

Iterators and closures. operator overloading. has a support for introspection, reflection

and metaprogramming It is highly portable can load extension libraries dynamically. automatic garbage collection for all Ruby

objects

FEATURES OF RUBY CONT.

Page 7: RUBY ON RAILS Caner Çebi Emre Gürhan Işık H. Yılmaz Öztürk Hande İzmirlioğlu Özge Bekleyen.

class Person

def initialize(name, age)

@name, @age = name, age

end

def <=>(person) # Comparison operator for sorting @age <=> person.age

end

def to_s

"#@name (#@age)"

end

attr_reader :name, :age

end

group = [Person.new("Jon", 20),Person.new("Marcus", 63), Person.new("Ash", 16) ]

puts group.sort.reverse

SAMPLE OF A CLASS

Page 8: RUBY ON RAILS Caner Çebi Emre Gürhan Işık H. Yılmaz Öztürk Hande İzmirlioğlu Özge Bekleyen.

SAMPLE OF A CLASS

Output will be Marcus (63) Jon (20) Ash (16)

Page 9: RUBY ON RAILS Caner Çebi Emre Gürhan Işık H. Yılmaz Öztürk Hande İzmirlioğlu Özge Bekleyen.

CRİTİCİSM

Among other dynamic scripting languages like Perl, PHP and Pyhton its speed is much less.

It has some limitations on threading method(green threads) it uses.

It doesnt have support for Unicode.

Page 10: RUBY ON RAILS Caner Çebi Emre Gürhan Işık H. Yılmaz Öztürk Hande İzmirlioğlu Özge Bekleyen.

WHAT İS RUBY ON RAİLS?

Ruby on Rails (RoR) is the super-productive new way to develop full featured web applications.

Rails is the name of the web application framework, and Ruby is the language used to create web sites.

It creates skeleton applications that the programmer can modify easily. So the duration of creating complex web sites can be decreased by using RoR.

Page 11: RUBY ON RAILS Caner Çebi Emre Gürhan Işık H. Yılmaz Öztürk Hande İzmirlioğlu Özge Bekleyen.

PROPERTİES OF RAİLS

“Convention over configuration (CoC).” No configuration files.

“Don’t repeat yourself (DRY).” The programmer does not need to specify the

columns of the database etc. There is no need to write SQL queries. It is similar to the J2EE architecture. It is one

of the most appropriate languages for Ajax.

Page 12: RUBY ON RAILS Caner Çebi Emre Gürhan Işık H. Yılmaz Öztürk Hande İzmirlioğlu Özge Bekleyen.

INSTALLATİON To create web pages on Rails, the following

components should be installed: Ruby RubyGems: Standard Ruby package manager. Rails: If RubyGems is already installed, Rails can be

installed with a command through command window.

MySql: For the database applications. (Other databases can also be used, but some changes must be made in the related files Rails creates.)

o RadRails is an eclipse plug-in, which can be used to write web sites in Ruby.

Apache can also be used as web server in Rails applications.

Page 13: RUBY ON RAILS Caner Çebi Emre Gürhan Işık H. Yılmaz Öztürk Hande İzmirlioğlu Özge Bekleyen.

MODULES OF RAİLS

These are the modules required in the installation period of Rails. Active Record: Ruby’s approach to accessing

data in database. A database table is wrapped into a class and used in the code.

Action Pack: Used to ease the web-request routing, handling, and response.

Action Mailer: Used to simplify the creation of mail service classes.

Active Support: Utility classes and extension to the standard library that were required by Rails.

Page 14: RUBY ON RAILS Caner Çebi Emre Gürhan Işık H. Yılmaz Öztürk Hande İzmirlioğlu Özge Bekleyen.

COMPARİSON OF RUBY ON RAİLS AND J2EE

Figure of Rails and J2EE Stack.

Page 15: RUBY ON RAILS Caner Çebi Emre Gürhan Işık H. Yılmaz Öztürk Hande İzmirlioğlu Özge Bekleyen.

COMPARİSON OF RUBY ON RAİLS AND J2EE

Rails & J2EE both have a container in common in which the application code will execute. (Webrick in Rails, Tomcat Servlet Container in J2EE).

Struts' ActionServlet and Rails' DispatchServlet are both examples of the Front Controller pattern; as such, they both provide the same functionality(accept HTTP requests, parse the URL, and forward processing of the request to an appropriate action).

The main difference between the two front controllers is how they determine the action that processes a particular request.

Page 16: RUBY ON RAILS Caner Çebi Emre Gürhan Işık H. Yılmaz Öztürk Hande İzmirlioğlu Özge Bekleyen.

COMPARİSON OF RUBY ON RAİLS AND J2EE

Struts uses an XML file to externalize the mappings of specific requests to Action classes.

Rails takes a different approach. Instead of relying upon a configuration file to map requests to actions, it discovers the appropriate action based on the URL requested.

Example:

Page 17: RUBY ON RAILS Caner Çebi Emre Gürhan Işık H. Yılmaz Öztürk Hande İzmirlioğlu Özge Bekleyen.

In both Rails and Struts, the action acts as a bridge between the front controller and the model. The developer provides an implementation of an action in order to provide application-specific processing of a request. The front controller is responsible for accepting the request and passing it off to a specific action.

Rails and Struts action hierarchy:

COMPARİSON OF RUBY ON RAİLS AND J2EE

Page 18: RUBY ON RAILS Caner Çebi Emre Gürhan Işık H. Yılmaz Öztürk Hande İzmirlioğlu Özge Bekleyen.

Struts requires that the developer extend Action and override execute() in order to process the request.

The front controller calls the execute() method and passes it a number of useful objects, including the HTTP request and response objects.

ActionForm is a class that conveniently transfers and validates form-related input to and from the view, and ActionMapping contains the configuration information for the mapping as described in the XML.

The execute() method returns an ActionForward object that Struts uses to determine the component that continues processing the request. Generally, this component is a JSP, but ActionForward can also point in other actions.

COMPARİSON OF RUBY ON RAİLS AND J2EE

Page 19: RUBY ON RAILS Caner Çebi Emre Gürhan Işık H. Yılmaz Öztürk Hande İzmirlioğlu Özge Bekleyen.

In Rails, you must extend ActionController::Base for the model to participate in the processing of a request.

Developers need not be concerned with the threading issues that are present in Struts, and as a result, the session, request, header, and parameters are all accessible as instance members of the ActionController. ActionControllers are also a logical place to group all processing of specific domain logic.

Extending ActionController::Base:

COMPARİSON OF RUBY ON RAİLS AND J2EE

01 class OrderController < ActionController::Base

02 def list

03 @orders = Order.find_all // Find all orders and set instance variable

04 // framework automatically forwards to list.rhtml

05 end

06

07 def delete

08 id = @params["id"] // Get the order id from the request

09 Order.delete(id) // Delete the order

10 redirect_to :action => "list" // Forward to the list action (list method)

11 end

12 end

Page 20: RUBY ON RAILS Caner Çebi Emre Gürhan Işık H. Yılmaz Öztürk Hande İzmirlioğlu Özge Bekleyen.

A persistence framework is used to move data to and from the database in the application layer.

Both Hibernate and Rails persistence frameworks can be classified as object/relational mapping (ORM) tools, meaning that they take an object view of the data and map it to tables in a relational database.

Hibernate is based on the Data Mapper pattern, where a specific mapper class, the Session, is responsible for persisting and retrieving data to and from the database.

Rails' ORM framework is called Active Record and is based upon the design pattern of the same name.

COMPARİSON OF RUBY ON RAİLS AND J2EE

Page 21: RUBY ON RAILS Caner Çebi Emre Gürhan Işık H. Yılmaz Öztürk Hande İzmirlioğlu Özge Bekleyen.

Active Record doesn't require a mapping file, as Hibernate does; in fact, a developer working with Active Record doesn't need to code getters or setters, or even the properties of the class. Through some nifty lexical analysis, Active Record is able to determine that the Order class will map to the ORDERS table in the database.

The one line of code in the class body of Order defines its relationship to the Item object. has_many is a static method call for which the symbol :items is a parameter. ActiveRecord uses :items to discover the Item domain object and in turn maps the Item object back to the ITEMS table in the database.

COMPARİSON OF RUBY ON RAİLS AND J2EE

01 class Order < ActiveRecord::Base

02 has_many :items

03 end

Page 22: RUBY ON RAILS Caner Çebi Emre Gürhan Işık H. Yılmaz Öztürk Hande İzmirlioğlu Özge Bekleyen.

CREATİNG A PROJECT WİTH RAİLS

The creation of the skeleton of a web page using Rails is made through the command line.

There are some key points for creating the page and the database. They are the conventions, which are necessary because they are used instead of the configuration files (the CoC rule).

Page 23: RUBY ON RAILS Caner Çebi Emre Gürhan Işık H. Yılmaz Öztürk Hande İzmirlioğlu Özge Bekleyen.

CREATİNG A PROJECT WİTH RAİLS

Go to a directory through the command line, where you want to store your RoR project. Then execute the following command: rails project_name

With this command, Rails creates all the necessary files and folders.

Page 24: RUBY ON RAILS Caner Çebi Emre Gürhan Işık H. Yılmaz Öztürk Hande İzmirlioğlu Özge Bekleyen.

CREATED FİLES AND FOLDERS app: Contains the code of the application

models views controllers helpers

components: Self-contained mini-applications that can bundle together controllers, models, and views.

config: Contains configuration files for the Rails environment (database, routing map, etc.).

db: Contains the database schema. doc: Contains the documentation of the application. lib: Initially empty. Used for the generic library files.

Page 25: RUBY ON RAILS Caner Çebi Emre Gürhan Işık H. Yılmaz Öztürk Hande İzmirlioğlu Özge Bekleyen.

CREATED FİLES AND FOLDERS log: Contains the log files of the application.

Divided into four subfolders: development production server test.

public: Used for the non-ruby files like html files, images, javascript files, css stylesheets, etc.

script: Helper scripts for automation and generation.

test: Contains test files. vendor: Contains external libraries that the

application depends on

Page 26: RUBY ON RAILS Caner Çebi Emre Gürhan Işık H. Yılmaz Öztürk Hande İzmirlioğlu Özge Bekleyen.

CREATİNG A PROJECT WİTH RAİLS To see the project we have created in a

browser, we should run the server, which can be done with this command: ruby script/server

After this command, we can see our web site at the address “localhost:3000”.

If we want to connect our web site to a database and display the data in a page, we should obey some rules while we create the tables in the database and the corresponding pages.

Page 27: RUBY ON RAILS Caner Çebi Emre Gürhan Işık H. Yılmaz Öztürk Hande İzmirlioğlu Özge Bekleyen.

CREATİNG A PROJECT WİTH RAİLS In the database, the table names should be in

plural, and the pages we create for that tables should be in the singular form of the same word. The name of the table, which is used to store the

course list, should be “courses”, whereas the page we want to create to list the courses should have the name “course”, otherwise Rails warns you to name the page and the table in an appropriate form.

In a table, if we make the first column “id” (starting with small ‘i‘) and make it the primary key, Rails generates the necessary code to access the database automatically.

Page 28: RUBY ON RAILS Caner Çebi Emre Gürhan Işık H. Yılmaz Öztürk Hande İzmirlioğlu Özge Bekleyen.

CREATİNG A PROJECT WİTH RAİLS To create a page for the “courses” table in

the database, we should change the “database.yml” file in the folder “config” and give the necessary information (database name, host, etc.) to automatically connect to our database by Rails.

After giving the database information to the application, we can generate the skeleton of the page by executing this command: ruby script/generate scaffold course course

We give the singular form of the table’s name to this command.

Page 29: RUBY ON RAILS Caner Çebi Emre Gürhan Işık H. Yılmaz Öztürk Hande İzmirlioğlu Özge Bekleyen.

CREATİNG A PROJECT WİTH RAİLS We can see the generated page in this

address: localhost:3000/course

This is the simplest form of listing a table’s contents and editing or deleting them in a web site.

If we check the directory “app/views/course” in our project folder, we can see 5 different files with the extension “rhtml”, which are “list, show, new, edit, _form”.

Page 30: RUBY ON RAILS Caner Çebi Emre Gürhan Işık H. Yılmaz Öztürk Hande İzmirlioğlu Özge Bekleyen.

CREATİNG A PROJECT WİTH RAİLS “rhtml” is like jsp, where Ruby codes are

scripted into the html codes. So we can change the views of automatically generated pages by modifying these files. list.rhtml: It is the page we see at

“localhost:3000/course". It lists the rows of the table. show.rhtml: It is the page to show the details of a

record in the database. edit.rhtml: It is the page to edit a data in the

database. new.rhtml: It is the page for adding a new record to

the database. _form.rhtml: It is the common page of the “new” and

“edit” pages, because they have the same structure.

Page 31: RUBY ON RAILS Caner Çebi Emre Gürhan Işık H. Yılmaz Öztürk Hande İzmirlioğlu Özge Bekleyen.

CREATİNG A PROJECT WİTH RAİLS

If we have two tables related to each other, we can specify the relation between them by modifying the files in the directory app/models. In the models folder, there exist files, which have the extension “.rb”, for each table in the database. They are created after the scaffolding.

Page 32: RUBY ON RAILS Caner Çebi Emre Gürhan Işık H. Yılmaz Öztürk Hande İzmirlioğlu Özge Bekleyen.

CREATİNG A PROJECT WİTH RAİLS If we have a “students” table and it has a

column “courseId”, which we want to relate to the id column of the “courses” table, we should make following changes in these files: student.rb:

belongs_to :course course.rb:

has_many :students

Now we can access from one table’s record to the other table’s records in the rhtml files.

We do not specify the relations in the database.

Page 33: RUBY ON RAILS Caner Çebi Emre Gürhan Işık H. Yılmaz Öztürk Hande İzmirlioğlu Özge Bekleyen.

CREATİNG A PROJECT WİTH RAİLS

Displaying the content of one column in a drop down list and taking the selected element is also very easy in Rails. If we want to select a course from the drop down

list and edit a student’s information or add a new student according to that choice, changing the content of the “_form.rhtml” file in the “app/views/student” directory will be enoug. Because both “new” and “edit” pages use that form as base, both of them will be changed.

Page 34: RUBY ON RAILS Caner Çebi Emre Gürhan Işık H. Yılmaz Öztürk Hande İzmirlioğlu Özge Bekleyen.

CREATİNG A PROJECT WİTH RAİLS

In the “_form.rhtml” file, all column names in the database are represented as labels and below them, the textboxes for the appropriate fields are set. If we want to see a drop down list instead of a textbox, we need to change this row <%= text_field 'student', courseId' %></p>

Into this form: <%= select("student", "courseId",

Course.find(:all, :order =>'name').collect {|c| [c.name, c.id]}) %></p>

Page 35: RUBY ON RAILS Caner Çebi Emre Gürhan Işık H. Yılmaz Öztürk Hande İzmirlioğlu Özge Bekleyen.

CREATİNG A PROJECT WİTH RAİLS

Almost every web site has a left menu and a header. If we want to do these with Rails, we need to go to the folder “app/views/layouts”.

The folder contains “rhtml” files for each of the pages, which are generated automatically when the pages are generated. These files are html files with Ruby codes. So we can design them using html tags.

Page 36: RUBY ON RAILS Caner Çebi Emre Gürhan Işık H. Yılmaz Öztürk Hande İzmirlioğlu Özge Bekleyen.

CREATİNG A PROJECT WİTH RAİLS While these files are generated, <%= yield

%> is added to the file automatically. It represents the actual code of the page, which is stored in the views folder. If we design the page using html tags, the page’s actual content will be set to the place where we put this “<%= yield %>” command.

Page 37: RUBY ON RAILS Caner Çebi Emre Gürhan Işık H. Yılmaz Öztürk Hande İzmirlioğlu Özge Bekleyen.

REFERENCES http://www.rubyonrails.org/ http://rubyforge.org/ http://en.wikipedia.org/wiki/Ruby_on_Rails http://www.yazilimgrubu.com/ruby-on-railse-

giris-1/ http://www-128.ibm.com/developerworks/linu

x/library/wa-rubyonrails/?ca=dgr-lnxw16RubyAndJ2EE

Rails Cookbook, Rob Orsini, O’Reilly


Recommended