RUBY ON RAILS - PowerPoint PPT Presentation

1 / 37
About This Presentation
Title:

RUBY ON RAILS

Description:

doc: Contains the documentation of the application. lib: Initially empty. ... To see the project we have created in a browser, we should run the server, which ... – PowerPoint PPT presentation

Number of Views:125
Avg rating:3.0/5.0
Slides: 38
Provided by: Han8150
Category:
Tags: rails | ruby

less

Transcript and Presenter's Notes

Title: RUBY ON RAILS


1
RUBY ON RAILS
  • Caner Çebi
  • Emre Gürhan Isik
  • H. Yilmaz Öztürk
  • Hande Izmirlioglu
  • Özge Bekleyen

2
What is Ruby?
  • 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.

3
History of Ruby
  • 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,March1.8.6 is released.

4
Some Details and features
  • 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

5
Features of ruby cont.
  • Inheritance with mixins, singleton methods,
    dynamic dispatch.
  • Ex
  • class MyArray  include Enumerable
  • end
  • Exception handling
  • raise ArgumentError, "Illegal arguments!"
  • begin Do somethingrescue Handle exceptionend

6
Features of ruby cont.
  • It has four levels of variable scope local,
    global, class and instance and scope is resoluted
    by and _at_.
  • 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

7
Sample of a class
  • class Person
  • def initialize(name, age)
  • _at_name, _at_age name, age
  • end
  • def ltgt(person) Comparison operator for
    sorting _at_age ltgt person.age
  • end
  • def to_s
  • "_at_name (_at_age)"
  • end
  • attr_reader name, age
  • end
  • group Person.new("Jon", 20),Person.new("Marcus
    ", 63), Person.new("Ash", 16)
  • puts group.sort.reverse

8
Sample of a class
  • Output will be
  • Marcus (63)
  • Jon (20)
  • Ash (16)

9
Criticism
  • 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
What is Ruby on Rails?
  • 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
Properties of Rails
  • Convention over configuration (CoC).
  • No configuration files.
  • Dont 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
Installation
  • 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.)
  • 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
Modules of Rails
  • These are the modules required in the
    installation period of Rails.
  • Active Record Rubys 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
Comparison of Ruby on Rails and J2EE
  • Figure of Rails and J2EE Stack.

15
Comparison of Ruby on Rails 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
Comparison of Ruby on Rails 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
Comparison of Ruby on Rails and J2EE
  • 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

18
Comparison of Ruby on Rails and J2EE
  • 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.

19
Comparison of Ruby on Rails and J2EE
  • In Rails, you must extend ActionControllerBase
    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 ActionControllerBase

01 class OrderController lt ActionControllerBase
02 def list 03 _at_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 _at_params"id" // Get the
order id from the request 09
Order.delete(id) // Delete the
order 10 redirect_to action gt "list"
// Forward to the list action (list method) 11
end 12 end
20
Comparison of Ruby on Rails and J2EE
  • 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.

21
Comparison of Ruby on Rails and J2EE
  • 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.

01 class Order lt ActiveRecordBase 02 has_many
items 03 end
22
Creating a project with Rails
  • 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
Creating a project with Rails
  • 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
Creating a project with Rails
  • 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 localhost3000.
  • 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
Creating a project with Rails
  • 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
Creating a project with Rails
  • 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 tables name to
    this command.

29
Creating a project with Rails
  • We can see the generated page in this address
  • localhost3000/course
  • This is the simplest form of listing a tables
    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
Creating a project with Rails
  • 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
    localhost3000/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
Creating a project with Rails
  • 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
Creating a project with Rails
  • 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 tables record to the
    other tables records in the rhtml files.
  • We do not specify the relations in the database.

33
Creating a project with Rails
  • 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 students 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
Creating a project with Rails
  • 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
  • lt text_field 'student', courseId' gtlt/pgt
  • Into this form
  • lt select("student", "courseId",
    Course.find(all, order gt'name').collect c
    c.name, c.id) gtlt/pgt

35
Creating a project with Rails
  • 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
Creating a project with Rails
  • While these files are generated, lt yield gt 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 pages actual content will be set
    to the place where we put this lt yield gt
    command.

37
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/linux/librar
    y/wa-rubyonrails/?cadgr-lnxw16RubyAndJ2EE
  • Rails Cookbook, Rob Orsini, OReilly
Write a Comment
User Comments (0)
About PowerShow.com