Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "RUBY ON RAILS Caner Çebi Emre Gürhan Işık H. Yılmaz Öztürk Hande İzmirlioğlu Özge Bekleyen."— Presentation transcript:

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

2 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. W HAT IS R UBY ?

3 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 ISTORY OF R UBY

4 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) end end S OME D ETAILS AND FEATURES

5 Inheritance with mixins, singleton methods, dynamic dispatch. Ex: class MyArray include Enumerable end Exception handling: raise ArgumentError, "Illegal arguments!" begin # Do something rescue # Handle exception end F EATURES OF RUBY CONT.

6 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 F EATURES OF RUBY CONT.

7 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 S AMPLE OF A CLASS

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

9 C RITICISM 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.

10 W HAT IS R UBY ON R AILS ? 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.

11 P ROPERTIES OF R AILS “ 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.

12 I NSTALLATION 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.

13 M ODULES OF R AILS 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.

14 C OMPARISON OF R UBY ON R AILS AND J2EE Figure of Rails and J2EE Stack.

15 C OMPARISON OF R UBY ON R AILS 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.

16 C OMPARISON OF R UBY ON R AILS 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:

17 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: C OMPARISON OF R UBY ON R AILS AND J2EE

18 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. C OMPARISON OF R UBY ON R AILS AND J2EE

19 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: C OMPARISON OF R UBY ON R AILS 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

20 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. C OMPARISON OF R UBY ON R AILS AND J2EE

21 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. C OMPARISON OF R UBY ON R AILS AND J2EE 01 class Order < ActiveRecord::Base 02has_many :items 03 end

22 C REATING A PROJECT WITH R AILS 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).

23 C REATING A PROJECT WITH R AILS 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.

24 CREATED FILES 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.

25 CREATED FILES 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

26 C REATING A PROJECT WITH R AILS 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.

27 C REATING A PROJECT WITH R AILS 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.

28 C REATING A PROJECT WITH R AILS 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.

29 C REATING A PROJECT WITH R AILS 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”.

30 C REATING A PROJECT WITH R AILS “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.

31 C REATING A PROJECT WITH R AILS 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.

32 C REATING A PROJECT WITH R AILS 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.

33 C REATING A PROJECT WITH R AILS 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.

34 C REATING A PROJECT WITH R AILS 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 Into this form: 'name').collect {|c| [c.name, c.id]}) %>

35 C REATING A PROJECT WITH R AILS 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.

36 C REATING A PROJECT WITH R AILS While these files are generated, 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 “ ” command.

37 R EFERENCES 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/linux/library/wa- rubyonrails/?ca=dgr-lnxw16RubyAndJ2EE Rails Cookbook, Rob Orsini, O’Reilly


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

Similar presentations


Ads by Google