SlideShare uma empresa Scribd logo
1 de 42
An Introduction to
Ruby on Rails
Outline
 What is Ruby?
 Tool for Ruby.
 What is Rails?
 Major Ruby features.
 Rails overview.
 Sample RoR application and also with Spree
  gem.
 Where to go for more information.


                 An Introduction to Ruby and Rails   2
What is Ruby?

    Ruby is a pure object oriented programming language. It was
    created on February 24, 1993 by Yukihiro Matsumoto of Japan.
    Ruby is a general-purpose, interpreted programming language like
    PERL and Python.

What is irb

    Interactive Ruby (irb) provides a shell(command prompt) for
    experimentation. Within the IRb shell, you can immediately view
    expression results, line by line.




                          An Introduction to Ruby and Rails            3
What is Rails?

    Rails is a web application development framework written in the
    Ruby language. It is designed to make programming web
    applications easier by making assumptions about what every
    developer needs to get started.


    It allows you to write less code while accomplishing more than
    many other languages and frameworks.




                          An Introduction to Ruby and Rails           4
Must have tool #1: irb
 Interactive   ruby console:
  Experiment    on the fly
  $irb
  …




                   An Introduction to Ruby and Rails   5
Ruby in a nutshell :
 Like all interpreted scripting languages, you can
  put code into a file.
 $vi test1.rb
   puts “Hello ruby”

 $ruby test1.rb
o/p: Hello ruby



                   An Introduction to Ruby and Rails   6
Objects are everywhere
   Everything in ruby is an object:




                     An Introduction to Ruby and Rails   7
Objects have methods




  Bang on the tab key in irb to see the methods that are available for
  each object.


                         An Introduction to Ruby and Rails               8
Variables
   Local variables - start with lower case:
      foo
      bar
   Global variables - start with dollar sign:
      $foo
      $bar
   Constants and Classes – start with capital letter:
      CONSTANT
      Class
   Instance variables – start with at sign:
      @foo
      @bar
   Class variables – start with double at sign:
      @@foo
      @@bar




                               An Introduction to Ruby and Rails   9
Arrays




         An Introduction to Ruby and Rails   10
Hashes




         An Introduction to Ruby and Rails   11
Blocks & Iterators




              An Introduction to Ruby and Rails   12
It’s easy to build classes
ruby> class Fruit
                                  def set_kind(k) # a writer
                    @kind = k
                end
        def get_kind          # a reader
                                  @kind
                    end
                             end


ruby> f1 = Fruit.new
  #<Fruit:0xfd7e7c8c>
ruby> f1.set_kind("peach") # use the writer
                       An Introduction to Ruby and Rails       13
  "peach"
Other notes on Classes

   Ruby only has single inheritance(<). This makes things
    simpler.




                      An Introduction to Ruby and Rails      14
Example:

class Mammal
 def breathe
   puts "inhale and exhale"
 end
end

class Cat < Mammal
 def speak
   puts "Meow"
                An Introduction to Ruby and Rails   15
Few gotchas
   Despite the principle of least surprise:
      Zero   isn’t false:




      No   increment operator (foo++). Instead use:
         foo += 1
         foo = foo + 1




                             An Introduction to Ruby and Rails   16
RubyGems Manual

 http://docs.rubygems.org/
 Examples:
       gem install paperclip --version ">= 2.7.0"
       gem uninstall paperclip
       gem list
    …




                       An Introduction to Ruby and Rails   17
Want to learn more Ruby?
Simple and beginner’s tutorial:

  http://www.troubleshooters.com/codecorn/ruby/basictutorial.htm




                          An Introduction to Ruby and Rails        18
   Browser sends requests
   Controller interacts with model
   Model checks with the database
   Controller invokes view
   View renders next browser screen


                         An Introduction to Ruby and Rails   19
Quick Rails Demo:

 $ rails new RoRApp
  $ cd RoRApp
(Use an Aptana studio IDE)

   We have 3 environments( in config/database.yml)
        1.Development
        2.Test
        3.Production




                         An Introduction to Ruby and Rails   20
Configuring the Database:
 If we want to configure the database as
  mysql2(default database is sqllite), open the file
  config/database.yml and modify the database name
  and options.
development:
  adapter: mysql2
  encoding: utf8
  database: db/development/dev
  username: root
  Password: '123456'
 And also we need to add myspl2 gem in Gemfile as

  gem 'mysql2'



                  An Introduction to Ruby and Rails   21
Now we need to bundle update which installs the mysql2
gem for our application.
  $ bundle update


Create a Database :
    Now we have our database is configured, it’s time
     to have Rails create an empty database. We can do
     this by running a rake command:
        $ rake db:create




                     An Introduction to Ruby and Rails   22
1.rails s (or) (default port is 3000)
        2.rails server (or)
        3.rails s -p 4000

    Verify whether the rails app is working properly
    or not by browsing http://localhost:3000

   Here the default page is rendering from
    public/index.html




                    An Introduction to Ruby and Rails   23
   If we want to display a user defined text or
    anything in our home page, we need to create a
    controller and a view

       $ rails generate controller home index

    Rails will create several files, including
    app/views/home/index.html.erb   and
    app/controllers/home_controller.rb

   To make this index file as the home page, 1st we
    need to delete the default public/index.html page
     $ rm public/index.html




                    An Introduction to Ruby and Rails   25
   Now, we have to tell Rails where your actual home
    page is located. For that open the file
    config/routes.rb and edit as
       root :to => "home#index"

   Check whether our home page is rendering proper
    page or not, for that we need to start the rails
    serve as
     $ rails s




                    An Introduction to Ruby and Rails   26
Scaffolding
    Rails scaffolding is a quick way to generate some
    of the major pieces of an application. If you want
    to create the models, views, and controllers for a
    new resource in a single operation, scaffolding is
    the tool for the job.


Example:
Creating a Resource for sessionApp:
    We can start by generating a scaffold for the Post
    resource: this will represent a single blog
    posting.
$   rails generate scaffold Post name:string title:string content:text




                         An Introduction to Ruby and Rails           27
File                                            Purpose

db/migrate/20120606184725_create_p              Migration to create the posts table in
osts.rb                                         your database (your name will include a
                                                different timestamp)

app/models/post.rb                              The Post model

config/routes.rb                                Edited to include routing information for
                                                posts

app/controllers/posts_controller.rb             The Posts controller

app/views/posts/index.html.erb                  A view to display an index of all posts

app/views/posts/edit.html.erb                   A view to edit an existing post

app/views/posts/show.html.erb                   A view to display a single post

app/views/posts/new.html.erb                    A view to create a new post


                             An Introduction to Ruby and Rails                       28
Running a Migration:
   One of the products of the rails generate scaffold
    command is a database migration. Migrations are
    Ruby classes that are designed to make it simple
    to create and modify database tables. Rails uses
    rake commands to run migrations, and it’s possible
    to undo a migration ($ rake db:migrate rollback)
    after it’s been applied to your database.




                    An Introduction to Ruby and Rails   29
   If we look in the
    db/migrate/20120606184725_create_posts.rb

    class CreatePosts < ActiveRecord::Migration
      def change
        create_table :posts do |t|
          t.string :name
          t.string :title
          t.text :content
          t.timestamps
      end
    end
   At this point, we can use a rake command to run
    the migration:
    $ rake db:migrate


                      An Introduction to Ruby and Rails   30
Rails will execute this migration command and tell
  you it created the Posts table.
== CreatePosts: migrating
  ==================================================
  ==
-- create_table(:posts)
   -> 0.0019s
== CreatePosts: migrated (0.0020s)
  ===========================================




                  An Introduction to Ruby and Rails   31
Adding a Link:
   We can add a link to the home page. Open
    app/views/home/index.html.erb and modify it as
    follows:
       <h1>Hello, Rails!</h1>
       <%= link_to "New Post", posts_path %>

When we run the server it displays the home page as




                    An Introduction to Ruby and Rails   32
The Model:
   The model file, app/models/post.rb is

class Post < ActiveRecord::Base
  attr_accessible :content, :name, :title
end

   Active Record supplies a great deal of
    functionality to our Rails models for free,
    including basic database CRUD (Create, Read,
    Update, Destroy) operations, data validation,




                    An Introduction to Ruby and Rails   33
Adding Some Validation:
   Rails includes methods to help you validate the
    data that you send to models. Open the app/models/
    post.rb file and edit it:
    class Post < ActiveRecord::Base
          attr_accessible :content, :name, :title
          validates :name, :presence => true
          validates :title, :presence => true,
                            :length => {:minimum => 5}
    end




                    An Introduction to Ruby and Rails   34
Listing All Posts
   How the application is showing us the list of
    Posts.
   Open the file app/controllers/posts_controller.rb
    and look at the index action:

    def index
          @posts = Post.all
          respond_to do |format|
          format.html # index.html.erb
          format.json { render :json => @posts }
          end
    end


                    An Introduction to Ruby and Rails   35
   The HTML format(format.html) looks for a
    view in app/views/posts/ with a name that
    corresponds to the action name(index).
    Rails makes all of the instance variables
    from the action available to the view.
    Here’s app/views/posts/index.html.erb:




                  An Introduction to Ruby and Rails   36
<h1>Listing posts</h1>
<table>
  <tr>    <th>Name</th>
         <th>Title</th>
         <th>Content</th>
         <th></th>
         <th></th>
          <th></th>
  </tr>
<% @posts.each do |post| %>
  <tr>
         <td><%= post.name %></td>
          <td><%= post.title %></td>
          <td><%= post.content %></td>
          <td><%= link_to 'Show', post %></td>
          <td><%= link_to 'Edit', edit_post_path(post) %></td>
          <td><%= link_to 'Destroy', post, :confirm => 'Are you sure?',
                                            :method => :delete %></td>
  </tr>
<% end %>
</table>
<br />
<%= link_to 'New post', new_post_path %>


                                 An Introduction to Ruby and Rails        37
Creating an App using spree:

   Spree is a full featured commerce platform written
    for the Ruby on Rails framework. It is designed to
    make programming commerce applications easier by
    making several assumptions about what most
    developers needs to get started. Spree is a
    production ready store.




                    An Introduction to Ruby and Rails   38
 Now we are going to see how we create
e-commerce(spree) application.

$ rails new SpreeApp
$ cd SpreeApp
$ spree install
Would you like to install the default gateways? (yes/no) [yes] yes
Would you like to run the migrations? (yes/no) [yes] yes
Would you like to load the seed data? (yes/no) [yes] yes
Would you like to load the sample data? (yes/no) [yes] yes
Admin Email [spree@example.com]
Admin Password [spree123]
Would you like to precompile assets? (yes/no) [yes] yes
$ rails server

                            An Introduction to Ruby and Rails        39
An Introduction to Ruby and Rails   40
Where to go for more information
   Books:




   Online material:
      First edition of Pickaxe online for free
      http://www.ruby-doc.org/
      why’s (poignant) guide to Ruby
      http://rubyonrails.org/


        Planet Ruby on Rails




                                 An Introduction to Ruby and Rails   41
An Introduction to Ruby and Rails   42

Mais conteúdo relacionado

Mais procurados

Introduction to Ruby on Rails
Introduction to Ruby on RailsIntroduction to Ruby on Rails
Introduction to Ruby on RailsAmit Patel
 
Ruby On Rails Introduction
Ruby On Rails IntroductionRuby On Rails Introduction
Ruby On Rails IntroductionThomas Fuchs
 
Ruby on Rails workshop for beginner
Ruby on Rails workshop for beginnerRuby on Rails workshop for beginner
Ruby on Rails workshop for beginnerUmair Amjad
 
Ruby on Rails introduction
Ruby on Rails introduction Ruby on Rails introduction
Ruby on Rails introduction Tran Hung
 
Security Goodness with Ruby on Rails
Security Goodness with Ruby on RailsSecurity Goodness with Ruby on Rails
Security Goodness with Ruby on RailsSource Conference
 
Ruby on Rails 2.1 What's New
Ruby on Rails 2.1 What's NewRuby on Rails 2.1 What's New
Ruby on Rails 2.1 What's NewLibin Pan
 
Introduction to Rails - presented by Arman Ortega
Introduction to Rails - presented by Arman OrtegaIntroduction to Rails - presented by Arman Ortega
Introduction to Rails - presented by Arman Ortegaarman o
 
Ruby on Rails - An overview
Ruby on Rails -  An overviewRuby on Rails -  An overview
Ruby on Rails - An overviewThomas Asikis
 
Migrating PriceChirp to Rails 3.0: The Pain Points
Migrating PriceChirp to Rails 3.0: The Pain PointsMigrating PriceChirp to Rails 3.0: The Pain Points
Migrating PriceChirp to Rails 3.0: The Pain PointsSteven Evatt
 
A Toda Maquina Con Ruby on Rails
A Toda Maquina Con Ruby on RailsA Toda Maquina Con Ruby on Rails
A Toda Maquina Con Ruby on RailsRafael García
 
Developing cross platform desktop application with Ruby
Developing cross platform desktop application with RubyDeveloping cross platform desktop application with Ruby
Developing cross platform desktop application with RubyAnis Ahmad
 
Be Happy With Ruby on Rails - Ecosystem
Be Happy With Ruby on Rails - EcosystemBe Happy With Ruby on Rails - Ecosystem
Be Happy With Ruby on Rails - EcosystemLucas Renan
 
Java, Ruby & Rails
Java, Ruby & RailsJava, Ruby & Rails
Java, Ruby & RailsPeter Lind
 
Ruby - a tester's best friend
Ruby - a tester's best friendRuby - a tester's best friend
Ruby - a tester's best friendPeter Lind
 
JRuby in Java Projects
JRuby in Java ProjectsJRuby in Java Projects
JRuby in Java Projectsjazzman1980
 

Mais procurados (20)

Introduction to Ruby on Rails
Introduction to Ruby on RailsIntroduction to Ruby on Rails
Introduction to Ruby on Rails
 
Ruby On Rails Introduction
Ruby On Rails IntroductionRuby On Rails Introduction
Ruby On Rails Introduction
 
Ruby on Rails workshop for beginner
Ruby on Rails workshop for beginnerRuby on Rails workshop for beginner
Ruby on Rails workshop for beginner
 
Ruby on Rails introduction
Ruby on Rails introduction Ruby on Rails introduction
Ruby on Rails introduction
 
Security Goodness with Ruby on Rails
Security Goodness with Ruby on RailsSecurity Goodness with Ruby on Rails
Security Goodness with Ruby on Rails
 
Debugging on rails
Debugging on railsDebugging on rails
Debugging on rails
 
Ruby on Rails 2.1 What's New
Ruby on Rails 2.1 What's NewRuby on Rails 2.1 What's New
Ruby on Rails 2.1 What's New
 
Introduction to Rails - presented by Arman Ortega
Introduction to Rails - presented by Arman OrtegaIntroduction to Rails - presented by Arman Ortega
Introduction to Rails - presented by Arman Ortega
 
Ruby on Rails - An overview
Ruby on Rails -  An overviewRuby on Rails -  An overview
Ruby on Rails - An overview
 
How DSL works on Ruby
How DSL works on RubyHow DSL works on Ruby
How DSL works on Ruby
 
Migrating PriceChirp to Rails 3.0: The Pain Points
Migrating PriceChirp to Rails 3.0: The Pain PointsMigrating PriceChirp to Rails 3.0: The Pain Points
Migrating PriceChirp to Rails 3.0: The Pain Points
 
Aspose pdf
Aspose pdfAspose pdf
Aspose pdf
 
A Toda Maquina Con Ruby on Rails
A Toda Maquina Con Ruby on RailsA Toda Maquina Con Ruby on Rails
A Toda Maquina Con Ruby on Rails
 
Developing cross platform desktop application with Ruby
Developing cross platform desktop application with RubyDeveloping cross platform desktop application with Ruby
Developing cross platform desktop application with Ruby
 
Be Happy With Ruby on Rails - Ecosystem
Be Happy With Ruby on Rails - EcosystemBe Happy With Ruby on Rails - Ecosystem
Be Happy With Ruby on Rails - Ecosystem
 
Java, Ruby & Rails
Java, Ruby & RailsJava, Ruby & Rails
Java, Ruby & Rails
 
Java presentation
Java presentationJava presentation
Java presentation
 
Ruby - a tester's best friend
Ruby - a tester's best friendRuby - a tester's best friend
Ruby - a tester's best friend
 
JRuby in Java Projects
JRuby in Java ProjectsJRuby in Java Projects
JRuby in Java Projects
 
Laravel ppt
Laravel pptLaravel ppt
Laravel ppt
 

Destaque (12)

Plataforma Spree Commerce
Plataforma Spree CommercePlataforma Spree Commerce
Plataforma Spree Commerce
 
Introduction to Ruby on Rails
Introduction to Ruby on RailsIntroduction to Ruby on Rails
Introduction to Ruby on Rails
 
Zen card
Zen cardZen card
Zen card
 
UBERCART UTVT E4
UBERCART UTVT E4UBERCART UTVT E4
UBERCART UTVT E4
 
Af commerce
Af commerceAf commerce
Af commerce
 
Comercio electrónico.
Comercio electrónico.Comercio electrónico.
Comercio electrónico.
 
Spree commerce
Spree commerceSpree commerce
Spree commerce
 
Zeuscart
ZeuscartZeuscart
Zeuscart
 
Introduction to Ruby
Introduction to RubyIntroduction to Ruby
Introduction to Ruby
 
Virtuemart
VirtuemartVirtuemart
Virtuemart
 
Ruby on Rails Presentation
Ruby on Rails PresentationRuby on Rails Presentation
Ruby on Rails Presentation
 
Comercio Electrónico con Drupal y Ubercart
Comercio Electrónico con Drupal y UbercartComercio Electrónico con Drupal y Ubercart
Comercio Electrónico con Drupal y Ubercart
 

Semelhante a Introduction to Ruby on Rails

What's new and great in Rails 3 - Matt Gauger - Milwaukee Ruby Users Group De...
What's new and great in Rails 3 - Matt Gauger - Milwaukee Ruby Users Group De...What's new and great in Rails 3 - Matt Gauger - Milwaukee Ruby Users Group De...
What's new and great in Rails 3 - Matt Gauger - Milwaukee Ruby Users Group De...Matt Gauger
 
Ruby on Rails
Ruby on RailsRuby on Rails
Ruby on RailsDelphiCon
 
Building Application with Ruby On Rails Framework
Building Application with Ruby On Rails FrameworkBuilding Application with Ruby On Rails Framework
Building Application with Ruby On Rails FrameworkEdureka!
 
Ruby Rails Web Development
Ruby Rails Web DevelopmentRuby Rails Web Development
Ruby Rails Web DevelopmentSonia Simi
 
Ruby on Rails : First Mile
Ruby on Rails : First MileRuby on Rails : First Mile
Ruby on Rails : First MileGourab Mitra
 
Intro to Ruby on Rails
Intro to Ruby on RailsIntro to Ruby on Rails
Intro to Ruby on Railselpizoch
 
Building Application With Ruby On Rails Framework
Building Application With Ruby On Rails FrameworkBuilding Application With Ruby On Rails Framework
Building Application With Ruby On Rails FrameworkVineet Chaturvedi
 
Instruments ruby on rails
Instruments ruby on railsInstruments ruby on rails
Instruments ruby on railspmashchak
 
Introduction to Ruby on Rails
Introduction to Ruby on RailsIntroduction to Ruby on Rails
Introduction to Ruby on RailsAlessandro DS
 
Ruby On Rails Tutorial
Ruby On Rails TutorialRuby On Rails Tutorial
Ruby On Rails Tutorialsunniboy
 
Rails
RailsRails
RailsSHC
 
Connecting the Worlds of Java and Ruby with JRuby
Connecting the Worlds of Java and Ruby with JRubyConnecting the Worlds of Java and Ruby with JRuby
Connecting the Worlds of Java and Ruby with JRubyNick Sieger
 
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Railsanides
 
Ruby on Rails survival guide of an aged Java developer
Ruby on Rails survival guide of an aged Java developerRuby on Rails survival guide of an aged Java developer
Ruby on Rails survival guide of an aged Java developergicappa
 

Semelhante a Introduction to Ruby on Rails (20)

What's new and great in Rails 3 - Matt Gauger - Milwaukee Ruby Users Group De...
What's new and great in Rails 3 - Matt Gauger - Milwaukee Ruby Users Group De...What's new and great in Rails 3 - Matt Gauger - Milwaukee Ruby Users Group De...
What's new and great in Rails 3 - Matt Gauger - Milwaukee Ruby Users Group De...
 
Supa fast Ruby + Rails
Supa fast Ruby + RailsSupa fast Ruby + Rails
Supa fast Ruby + Rails
 
Ruby on Rails
Ruby on RailsRuby on Rails
Ruby on Rails
 
Dev streams2
Dev streams2Dev streams2
Dev streams2
 
Building Application with Ruby On Rails Framework
Building Application with Ruby On Rails FrameworkBuilding Application with Ruby On Rails Framework
Building Application with Ruby On Rails Framework
 
Ruby Rails Web Development
Ruby Rails Web DevelopmentRuby Rails Web Development
Ruby Rails Web Development
 
Ruby on Rails : First Mile
Ruby on Rails : First MileRuby on Rails : First Mile
Ruby on Rails : First Mile
 
Intro to Ruby on Rails
Intro to Ruby on RailsIntro to Ruby on Rails
Intro to Ruby on Rails
 
Rail3 intro 29th_sep_surendran
Rail3 intro 29th_sep_surendranRail3 intro 29th_sep_surendran
Rail3 intro 29th_sep_surendran
 
Building Application With Ruby On Rails Framework
Building Application With Ruby On Rails FrameworkBuilding Application With Ruby On Rails Framework
Building Application With Ruby On Rails Framework
 
rails.html
rails.htmlrails.html
rails.html
 
rails.html
rails.htmlrails.html
rails.html
 
Instruments ruby on rails
Instruments ruby on railsInstruments ruby on rails
Instruments ruby on rails
 
Introduction to Ruby on Rails
Introduction to Ruby on RailsIntroduction to Ruby on Rails
Introduction to Ruby on Rails
 
Ruby On Rails Tutorial
Ruby On Rails TutorialRuby On Rails Tutorial
Ruby On Rails Tutorial
 
Rails
RailsRails
Rails
 
Connecting the Worlds of Java and Ruby with JRuby
Connecting the Worlds of Java and Ruby with JRubyConnecting the Worlds of Java and Ruby with JRuby
Connecting the Worlds of Java and Ruby with JRuby
 
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Rails
 
Ruby on Rails survival guide of an aged Java developer
Ruby on Rails survival guide of an aged Java developerRuby on Rails survival guide of an aged Java developer
Ruby on Rails survival guide of an aged Java developer
 
FGCU Camp Talk
FGCU Camp TalkFGCU Camp Talk
FGCU Camp Talk
 

Último

FULL ENJOY - 9953040155 Call Girls in Dwarka Mor | Delhi
FULL ENJOY - 9953040155 Call Girls in Dwarka Mor | DelhiFULL ENJOY - 9953040155 Call Girls in Dwarka Mor | Delhi
FULL ENJOY - 9953040155 Call Girls in Dwarka Mor | DelhiMalviyaNagarCallGirl
 
Faridabad Call Girls : ☎ 8527673949, Low rate Call Girls
Faridabad Call Girls : ☎ 8527673949, Low rate Call GirlsFaridabad Call Girls : ☎ 8527673949, Low rate Call Girls
Faridabad Call Girls : ☎ 8527673949, Low rate Call Girlsashishs7044
 
Retail Store Scavanger Hunt - Foundation College Park
Retail Store Scavanger Hunt - Foundation College ParkRetail Store Scavanger Hunt - Foundation College Park
Retail Store Scavanger Hunt - Foundation College Parkjosebenzaquen
 
STAR Scholars Program Brand Guide Presentation
STAR Scholars Program Brand Guide PresentationSTAR Scholars Program Brand Guide Presentation
STAR Scholars Program Brand Guide Presentationmakaiodm
 
Laxmi Nagar Call Girls : ☎ 8527673949, Low rate Call Girls
Laxmi Nagar Call Girls : ☎ 8527673949, Low rate Call GirlsLaxmi Nagar Call Girls : ☎ 8527673949, Low rate Call Girls
Laxmi Nagar Call Girls : ☎ 8527673949, Low rate Call Girlsashishs7044
 
TOP BEST Call Girls In SECTOR 62 (Noida) ꧁❤ 8375860717 ❤꧂ Female Escorts Serv...
TOP BEST Call Girls In SECTOR 62 (Noida) ꧁❤ 8375860717 ❤꧂ Female Escorts Serv...TOP BEST Call Girls In SECTOR 62 (Noida) ꧁❤ 8375860717 ❤꧂ Female Escorts Serv...
TOP BEST Call Girls In SECTOR 62 (Noida) ꧁❤ 8375860717 ❤꧂ Female Escorts Serv...door45step
 
Value Aspiration And Culture Theory of Architecture
Value Aspiration And Culture Theory of ArchitectureValue Aspiration And Culture Theory of Architecture
Value Aspiration And Culture Theory of ArchitectureDarrenMasbate
 
FULL ENJOY -9953040155 Call Girls In Aiims Metro
FULL ENJOY -9953040155 Call Girls In Aiims MetroFULL ENJOY -9953040155 Call Girls In Aiims Metro
FULL ENJOY -9953040155 Call Girls In Aiims MetroMalviyaNagarCallGirl
 
Hauz Khas Call Girls : ☎ 8527673949, Low rate Call Girls
Hauz Khas Call Girls : ☎ 8527673949, Low rate Call GirlsHauz Khas Call Girls : ☎ 8527673949, Low rate Call Girls
Hauz Khas Call Girls : ☎ 8527673949, Low rate Call Girlsashishs7044
 
9654467111 Call Girls In Noida Sector 62 Short 1500 Night 6000
9654467111 Call Girls In Noida Sector 62 Short 1500 Night 60009654467111 Call Girls In Noida Sector 62 Short 1500 Night 6000
9654467111 Call Girls In Noida Sector 62 Short 1500 Night 6000Sapana Sha
 
FULL ENJOY - 9953040155 Call Girls in Gandhi Vihar | Delhi
FULL ENJOY - 9953040155 Call Girls in Gandhi Vihar | DelhiFULL ENJOY - 9953040155 Call Girls in Gandhi Vihar | Delhi
FULL ENJOY - 9953040155 Call Girls in Gandhi Vihar | DelhiMalviyaNagarCallGirl
 
Zagor VČ OP 055 - Oluja nad Haitijem.pdf
Zagor VČ OP 055 - Oluja nad Haitijem.pdfZagor VČ OP 055 - Oluja nad Haitijem.pdf
Zagor VČ OP 055 - Oluja nad Haitijem.pdfStripovizijacom
 
Benjamin Portfolio Process Work Slideshow
Benjamin Portfolio Process Work SlideshowBenjamin Portfolio Process Work Slideshow
Benjamin Portfolio Process Work Slideshowssuser971f6c
 
Indian High Profile Call Girls In Sector 18 Noida 8375860717 Escorts Service
Indian High Profile Call Girls In Sector 18 Noida 8375860717 Escorts ServiceIndian High Profile Call Girls In Sector 18 Noida 8375860717 Escorts Service
Indian High Profile Call Girls In Sector 18 Noida 8375860717 Escorts Servicedoor45step
 
Pragati Maidan Call Girls : ☎ 8527673949, Low rate Call Girls
Pragati Maidan Call Girls : ☎ 8527673949, Low rate Call GirlsPragati Maidan Call Girls : ☎ 8527673949, Low rate Call Girls
Pragati Maidan Call Girls : ☎ 8527673949, Low rate Call Girlsashishs7044
 
Olivia Cox. intertextual references.pptx
Olivia Cox. intertextual references.pptxOlivia Cox. intertextual references.pptx
Olivia Cox. intertextual references.pptxLauraFagan6
 
Subway Stand OFF storyboard by Raquel Acosta
Subway Stand OFF storyboard by Raquel AcostaSubway Stand OFF storyboard by Raquel Acosta
Subway Stand OFF storyboard by Raquel Acostaracosta58
 
Jagat Puri Call Girls : ☎ 8527673949, Low rate Call Girls
Jagat Puri Call Girls : ☎ 8527673949, Low rate Call GirlsJagat Puri Call Girls : ☎ 8527673949, Low rate Call Girls
Jagat Puri Call Girls : ☎ 8527673949, Low rate Call Girlsashishs7044
 
Anand Vihar Call Girls : ☎ 8527673949, Low rate Call Girls
Anand Vihar Call Girls : ☎ 8527673949, Low rate Call GirlsAnand Vihar Call Girls : ☎ 8527673949, Low rate Call Girls
Anand Vihar Call Girls : ☎ 8527673949, Low rate Call Girlsashishs7044
 
Dilshad Garden Call Girls : ☎ 8527673949, Low rate Call Girls
Dilshad Garden Call Girls : ☎ 8527673949, Low rate Call GirlsDilshad Garden Call Girls : ☎ 8527673949, Low rate Call Girls
Dilshad Garden Call Girls : ☎ 8527673949, Low rate Call Girlsashishs7044
 

Último (20)

FULL ENJOY - 9953040155 Call Girls in Dwarka Mor | Delhi
FULL ENJOY - 9953040155 Call Girls in Dwarka Mor | DelhiFULL ENJOY - 9953040155 Call Girls in Dwarka Mor | Delhi
FULL ENJOY - 9953040155 Call Girls in Dwarka Mor | Delhi
 
Faridabad Call Girls : ☎ 8527673949, Low rate Call Girls
Faridabad Call Girls : ☎ 8527673949, Low rate Call GirlsFaridabad Call Girls : ☎ 8527673949, Low rate Call Girls
Faridabad Call Girls : ☎ 8527673949, Low rate Call Girls
 
Retail Store Scavanger Hunt - Foundation College Park
Retail Store Scavanger Hunt - Foundation College ParkRetail Store Scavanger Hunt - Foundation College Park
Retail Store Scavanger Hunt - Foundation College Park
 
STAR Scholars Program Brand Guide Presentation
STAR Scholars Program Brand Guide PresentationSTAR Scholars Program Brand Guide Presentation
STAR Scholars Program Brand Guide Presentation
 
Laxmi Nagar Call Girls : ☎ 8527673949, Low rate Call Girls
Laxmi Nagar Call Girls : ☎ 8527673949, Low rate Call GirlsLaxmi Nagar Call Girls : ☎ 8527673949, Low rate Call Girls
Laxmi Nagar Call Girls : ☎ 8527673949, Low rate Call Girls
 
TOP BEST Call Girls In SECTOR 62 (Noida) ꧁❤ 8375860717 ❤꧂ Female Escorts Serv...
TOP BEST Call Girls In SECTOR 62 (Noida) ꧁❤ 8375860717 ❤꧂ Female Escorts Serv...TOP BEST Call Girls In SECTOR 62 (Noida) ꧁❤ 8375860717 ❤꧂ Female Escorts Serv...
TOP BEST Call Girls In SECTOR 62 (Noida) ꧁❤ 8375860717 ❤꧂ Female Escorts Serv...
 
Value Aspiration And Culture Theory of Architecture
Value Aspiration And Culture Theory of ArchitectureValue Aspiration And Culture Theory of Architecture
Value Aspiration And Culture Theory of Architecture
 
FULL ENJOY -9953040155 Call Girls In Aiims Metro
FULL ENJOY -9953040155 Call Girls In Aiims MetroFULL ENJOY -9953040155 Call Girls In Aiims Metro
FULL ENJOY -9953040155 Call Girls In Aiims Metro
 
Hauz Khas Call Girls : ☎ 8527673949, Low rate Call Girls
Hauz Khas Call Girls : ☎ 8527673949, Low rate Call GirlsHauz Khas Call Girls : ☎ 8527673949, Low rate Call Girls
Hauz Khas Call Girls : ☎ 8527673949, Low rate Call Girls
 
9654467111 Call Girls In Noida Sector 62 Short 1500 Night 6000
9654467111 Call Girls In Noida Sector 62 Short 1500 Night 60009654467111 Call Girls In Noida Sector 62 Short 1500 Night 6000
9654467111 Call Girls In Noida Sector 62 Short 1500 Night 6000
 
FULL ENJOY - 9953040155 Call Girls in Gandhi Vihar | Delhi
FULL ENJOY - 9953040155 Call Girls in Gandhi Vihar | DelhiFULL ENJOY - 9953040155 Call Girls in Gandhi Vihar | Delhi
FULL ENJOY - 9953040155 Call Girls in Gandhi Vihar | Delhi
 
Zagor VČ OP 055 - Oluja nad Haitijem.pdf
Zagor VČ OP 055 - Oluja nad Haitijem.pdfZagor VČ OP 055 - Oluja nad Haitijem.pdf
Zagor VČ OP 055 - Oluja nad Haitijem.pdf
 
Benjamin Portfolio Process Work Slideshow
Benjamin Portfolio Process Work SlideshowBenjamin Portfolio Process Work Slideshow
Benjamin Portfolio Process Work Slideshow
 
Indian High Profile Call Girls In Sector 18 Noida 8375860717 Escorts Service
Indian High Profile Call Girls In Sector 18 Noida 8375860717 Escorts ServiceIndian High Profile Call Girls In Sector 18 Noida 8375860717 Escorts Service
Indian High Profile Call Girls In Sector 18 Noida 8375860717 Escorts Service
 
Pragati Maidan Call Girls : ☎ 8527673949, Low rate Call Girls
Pragati Maidan Call Girls : ☎ 8527673949, Low rate Call GirlsPragati Maidan Call Girls : ☎ 8527673949, Low rate Call Girls
Pragati Maidan Call Girls : ☎ 8527673949, Low rate Call Girls
 
Olivia Cox. intertextual references.pptx
Olivia Cox. intertextual references.pptxOlivia Cox. intertextual references.pptx
Olivia Cox. intertextual references.pptx
 
Subway Stand OFF storyboard by Raquel Acosta
Subway Stand OFF storyboard by Raquel AcostaSubway Stand OFF storyboard by Raquel Acosta
Subway Stand OFF storyboard by Raquel Acosta
 
Jagat Puri Call Girls : ☎ 8527673949, Low rate Call Girls
Jagat Puri Call Girls : ☎ 8527673949, Low rate Call GirlsJagat Puri Call Girls : ☎ 8527673949, Low rate Call Girls
Jagat Puri Call Girls : ☎ 8527673949, Low rate Call Girls
 
Anand Vihar Call Girls : ☎ 8527673949, Low rate Call Girls
Anand Vihar Call Girls : ☎ 8527673949, Low rate Call GirlsAnand Vihar Call Girls : ☎ 8527673949, Low rate Call Girls
Anand Vihar Call Girls : ☎ 8527673949, Low rate Call Girls
 
Dilshad Garden Call Girls : ☎ 8527673949, Low rate Call Girls
Dilshad Garden Call Girls : ☎ 8527673949, Low rate Call GirlsDilshad Garden Call Girls : ☎ 8527673949, Low rate Call Girls
Dilshad Garden Call Girls : ☎ 8527673949, Low rate Call Girls
 

Introduction to Ruby on Rails

  • 2. Outline  What is Ruby?  Tool for Ruby.  What is Rails?  Major Ruby features.  Rails overview.  Sample RoR application and also with Spree gem.  Where to go for more information. An Introduction to Ruby and Rails 2
  • 3. What is Ruby?  Ruby is a pure object oriented programming language. It was created on February 24, 1993 by Yukihiro Matsumoto of Japan. Ruby is a general-purpose, interpreted programming language like PERL and Python. What is irb  Interactive Ruby (irb) provides a shell(command prompt) for experimentation. Within the IRb shell, you can immediately view expression results, line by line. An Introduction to Ruby and Rails 3
  • 4. What is Rails?  Rails is a web application development framework written in the Ruby language. It is designed to make programming web applications easier by making assumptions about what every developer needs to get started.  It allows you to write less code while accomplishing more than many other languages and frameworks. An Introduction to Ruby and Rails 4
  • 5. Must have tool #1: irb  Interactive ruby console: Experiment on the fly $irb … An Introduction to Ruby and Rails 5
  • 6. Ruby in a nutshell :  Like all interpreted scripting languages, you can put code into a file.  $vi test1.rb puts “Hello ruby”  $ruby test1.rb o/p: Hello ruby An Introduction to Ruby and Rails 6
  • 7. Objects are everywhere  Everything in ruby is an object: An Introduction to Ruby and Rails 7
  • 8. Objects have methods Bang on the tab key in irb to see the methods that are available for each object. An Introduction to Ruby and Rails 8
  • 9. Variables  Local variables - start with lower case:  foo  bar  Global variables - start with dollar sign:  $foo  $bar  Constants and Classes – start with capital letter:  CONSTANT  Class  Instance variables – start with at sign:  @foo  @bar  Class variables – start with double at sign:  @@foo  @@bar An Introduction to Ruby and Rails 9
  • 10. Arrays An Introduction to Ruby and Rails 10
  • 11. Hashes An Introduction to Ruby and Rails 11
  • 12. Blocks & Iterators An Introduction to Ruby and Rails 12
  • 13. It’s easy to build classes ruby> class Fruit def set_kind(k) # a writer @kind = k end def get_kind # a reader @kind end end ruby> f1 = Fruit.new #<Fruit:0xfd7e7c8c> ruby> f1.set_kind("peach") # use the writer An Introduction to Ruby and Rails 13 "peach"
  • 14. Other notes on Classes  Ruby only has single inheritance(<). This makes things simpler. An Introduction to Ruby and Rails 14
  • 15. Example: class Mammal def breathe puts "inhale and exhale" end end class Cat < Mammal def speak puts "Meow" An Introduction to Ruby and Rails 15
  • 16. Few gotchas  Despite the principle of least surprise:  Zero isn’t false:  No increment operator (foo++). Instead use:  foo += 1  foo = foo + 1 An Introduction to Ruby and Rails 16
  • 17. RubyGems Manual  http://docs.rubygems.org/  Examples:  gem install paperclip --version ">= 2.7.0"  gem uninstall paperclip  gem list … An Introduction to Ruby and Rails 17
  • 18. Want to learn more Ruby? Simple and beginner’s tutorial: http://www.troubleshooters.com/codecorn/ruby/basictutorial.htm An Introduction to Ruby and Rails 18
  • 19. Browser sends requests  Controller interacts with model  Model checks with the database  Controller invokes view  View renders next browser screen An Introduction to Ruby and Rails 19
  • 20. Quick Rails Demo:  $ rails new RoRApp $ cd RoRApp (Use an Aptana studio IDE)  We have 3 environments( in config/database.yml) 1.Development 2.Test 3.Production An Introduction to Ruby and Rails 20
  • 21. Configuring the Database:  If we want to configure the database as mysql2(default database is sqllite), open the file config/database.yml and modify the database name and options. development: adapter: mysql2 encoding: utf8 database: db/development/dev username: root Password: '123456'  And also we need to add myspl2 gem in Gemfile as gem 'mysql2' An Introduction to Ruby and Rails 21
  • 22. Now we need to bundle update which installs the mysql2 gem for our application. $ bundle update Create a Database :  Now we have our database is configured, it’s time to have Rails create an empty database. We can do this by running a rake command: $ rake db:create An Introduction to Ruby and Rails 22
  • 23. 1.rails s (or) (default port is 3000) 2.rails server (or) 3.rails s -p 4000  Verify whether the rails app is working properly or not by browsing http://localhost:3000  Here the default page is rendering from public/index.html An Introduction to Ruby and Rails 23
  • 24.
  • 25. If we want to display a user defined text or anything in our home page, we need to create a controller and a view $ rails generate controller home index Rails will create several files, including app/views/home/index.html.erb and app/controllers/home_controller.rb  To make this index file as the home page, 1st we need to delete the default public/index.html page $ rm public/index.html An Introduction to Ruby and Rails 25
  • 26. Now, we have to tell Rails where your actual home page is located. For that open the file config/routes.rb and edit as root :to => "home#index"  Check whether our home page is rendering proper page or not, for that we need to start the rails serve as $ rails s An Introduction to Ruby and Rails 26
  • 27. Scaffolding Rails scaffolding is a quick way to generate some of the major pieces of an application. If you want to create the models, views, and controllers for a new resource in a single operation, scaffolding is the tool for the job. Example: Creating a Resource for sessionApp: We can start by generating a scaffold for the Post resource: this will represent a single blog posting. $ rails generate scaffold Post name:string title:string content:text An Introduction to Ruby and Rails 27
  • 28. File Purpose db/migrate/20120606184725_create_p Migration to create the posts table in osts.rb your database (your name will include a different timestamp) app/models/post.rb The Post model config/routes.rb Edited to include routing information for posts app/controllers/posts_controller.rb The Posts controller app/views/posts/index.html.erb A view to display an index of all posts app/views/posts/edit.html.erb A view to edit an existing post app/views/posts/show.html.erb A view to display a single post app/views/posts/new.html.erb A view to create a new post An Introduction to Ruby and Rails 28
  • 29. Running a Migration:  One of the products of the rails generate scaffold command is a database migration. Migrations are Ruby classes that are designed to make it simple to create and modify database tables. Rails uses rake commands to run migrations, and it’s possible to undo a migration ($ rake db:migrate rollback) after it’s been applied to your database. An Introduction to Ruby and Rails 29
  • 30. If we look in the db/migrate/20120606184725_create_posts.rb class CreatePosts < ActiveRecord::Migration def change create_table :posts do |t| t.string :name t.string :title t.text :content t.timestamps end end  At this point, we can use a rake command to run the migration: $ rake db:migrate An Introduction to Ruby and Rails 30
  • 31. Rails will execute this migration command and tell you it created the Posts table. == CreatePosts: migrating ================================================== == -- create_table(:posts) -> 0.0019s == CreatePosts: migrated (0.0020s) =========================================== An Introduction to Ruby and Rails 31
  • 32. Adding a Link:  We can add a link to the home page. Open app/views/home/index.html.erb and modify it as follows: <h1>Hello, Rails!</h1> <%= link_to "New Post", posts_path %> When we run the server it displays the home page as An Introduction to Ruby and Rails 32
  • 33. The Model:  The model file, app/models/post.rb is class Post < ActiveRecord::Base attr_accessible :content, :name, :title end  Active Record supplies a great deal of functionality to our Rails models for free, including basic database CRUD (Create, Read, Update, Destroy) operations, data validation, An Introduction to Ruby and Rails 33
  • 34. Adding Some Validation:  Rails includes methods to help you validate the data that you send to models. Open the app/models/ post.rb file and edit it: class Post < ActiveRecord::Base attr_accessible :content, :name, :title validates :name, :presence => true validates :title, :presence => true, :length => {:minimum => 5} end An Introduction to Ruby and Rails 34
  • 35. Listing All Posts  How the application is showing us the list of Posts.  Open the file app/controllers/posts_controller.rb and look at the index action: def index @posts = Post.all respond_to do |format| format.html # index.html.erb format.json { render :json => @posts } end end An Introduction to Ruby and Rails 35
  • 36. The HTML format(format.html) looks for a view in app/views/posts/ with a name that corresponds to the action name(index). Rails makes all of the instance variables from the action available to the view. Here’s app/views/posts/index.html.erb: An Introduction to Ruby and Rails 36
  • 37. <h1>Listing posts</h1> <table> <tr> <th>Name</th> <th>Title</th> <th>Content</th> <th></th> <th></th> <th></th> </tr> <% @posts.each do |post| %> <tr> <td><%= post.name %></td> <td><%= post.title %></td> <td><%= post.content %></td> <td><%= link_to 'Show', post %></td> <td><%= link_to 'Edit', edit_post_path(post) %></td> <td><%= link_to 'Destroy', post, :confirm => 'Are you sure?', :method => :delete %></td> </tr> <% end %> </table> <br /> <%= link_to 'New post', new_post_path %> An Introduction to Ruby and Rails 37
  • 38. Creating an App using spree:  Spree is a full featured commerce platform written for the Ruby on Rails framework. It is designed to make programming commerce applications easier by making several assumptions about what most developers needs to get started. Spree is a production ready store. An Introduction to Ruby and Rails 38
  • 39.  Now we are going to see how we create e-commerce(spree) application. $ rails new SpreeApp $ cd SpreeApp $ spree install Would you like to install the default gateways? (yes/no) [yes] yes Would you like to run the migrations? (yes/no) [yes] yes Would you like to load the seed data? (yes/no) [yes] yes Would you like to load the sample data? (yes/no) [yes] yes Admin Email [spree@example.com] Admin Password [spree123] Would you like to precompile assets? (yes/no) [yes] yes $ rails server An Introduction to Ruby and Rails 39
  • 40. An Introduction to Ruby and Rails 40
  • 41. Where to go for more information  Books:  Online material:  First edition of Pickaxe online for free  http://www.ruby-doc.org/  why’s (poignant) guide to Ruby  http://rubyonrails.org/  Planet Ruby on Rails An Introduction to Ruby and Rails 41
  • 42. An Introduction to Ruby and Rails 42