SlideShare a Scribd company logo
Ruby on Rails Tutorial
  Learn Rails by Example


      Michael Hartl
2
Contents

1   Introduction                                                                                                                                                                       5
    1.1 Technology . . . . . .     .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   5
    1.2 Sample code and such .     .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   6
         1.2.1 Code samples .      .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   6
         1.2.2 And such . . .      .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   7
    1.3 Figures and tables . . .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   7
    1.4 Math . . . . . . . . . .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   9

2   Lorem ipsum                                                                                                                                                                        11
    2.1 Lorem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .                                                                              11
    2.2 Ipsum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .                                                                              12




                                                                           3
4   CONTENTS
Chapter 1

Introduction

This is a sample document for what will become the Ruby on Rails Tutorial book. It is by no means a real
book yet—there’s even some lorem ipsum text in what follows (see, e.g., Box 1.1 and Chapter 2). Its purpose
is to show that all the elements are in place to produce a pleasing final product, and to give a hint of what that
final product might eventually look like.


Box 1.1. Cicero dixit

    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et
dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex
ea commodo consequat.
    Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Ex-
cepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.




1.1        Technology
Rails Tutorial is written in PolyTEXnic, a dialect of the LTEX technical typesetting system (which in turn
                                                          A

is based on TEX). I wrote PolyTEXnic because I wanted to produce both beautiful PDF (and hence print)
documents and pretty HTML documents at the same time, from the same source file. As a result, this
sample document is available both as HTML and as a PDF download. Both formats come with plenty of rich
book-y goodness, including a linked table of contents; numbered sections, tables, figures, and code listings;
linked cross references; syntax highlighting; numbered, shaded sidebars (Box 1.1); footnotes1 ; and even math
(Section 1.4).
    Currently, PolyTEXnic is only available for my private use, but I plan to release it as an open-source
project once it’s battle-tested and ready for general consumption.

  1 Like   this.


                                                       5
6                                                                         CHAPTER 1. INTRODUCTION

1.2     Sample code and such
Often, Rails books include lots of code samples and such. Let’s look at some examples of how PolyTEXnic
makes them both useful and pretty.

1.2.1    Code samples
We’ll likely have some Ruby code in our Rails tutorial, with snippets such as the following:
Listing 1.1. Part of the ClassMethods module in ActiveRecord::Validations.


module ClassMethods
  .
  .
  .
  # File rails-2.3.2/activerecord/lib/active_record/validations.rb, line 511
  def validates_presence_of(*attr_names)
    configuration = { :on => :save }
    configuration.update(attr_names.extract_options!)

    # can’t use validates_each here, because it cannot cope with
    # nonexistent attributes, while errors.add_on_empty can
    send(validation_method(configuration[:on]), configuration) do |record|
      record.errors.add_on_blank(attr_names, configuration[:message])
    end
  end
  .
  .
  .
end


    Note that Listing 1.1 is both syntax-highlighted and numbered—and, as you can see, it can be cross-
referenced as well.
    Of course, we’re not limited to vanilla Ruby; let’s also include some embedded Ruby:
Listing 1.2. The index.html.erb file for the People controller.


<%- column_div :type => :primary do -%>
  <% unless @people.empty? -%>
    <h2>People</h2>
    <%= will_paginate %>
    <ul class="list people">
      <%= render :partial => @people %>
    </ul>
    <%= will_paginate %>
  <% end -%>
<%- end -%>

<%- column_div :type => :secondary do -%>
1.3. FIGURES AND TABLES                                                                                      7


  <%= render :partial => ’searches/box’ %>
  <%= render :partial => ’shared/minifeed’ %>
<%- end -%>


   In fact, we can include code in virtually any language and get nice syntax highlighting for free via the
excellent Pygments program:


;; Common Lisp
;; Return the square of the given number.
(defun square (x)
  "Calculates the square of the single-float x."
  (declare (single-float x) (optimize (speed 3) (debug 0) (safety 1)))
  (* x x))


    Note here that we have a nice highlighted code block, but no listing number. This is useful for short
snippets that aren’t likely ever to be referenced.


1.2.2      And such
Code isn’t everything; sometimes you just want to show shell commands. No problem:


$ rake -T db
(in /Users/mhartl/rails/rails_tutorial)
rake backup:db                       # Backup the current database.
rake db:avatars:delete               # Delete all the avatars.
rake db:avatars:load                 # Make sample avatars.
.
.
.


     We can also include console sessions:


>>   a = 1
=>   1
>>   puts a
1
=>   nil




1.3      Figures and tables
It’s easy to include figures, such as screenshots (Fig. 1.1). (These are particularly useful for a tutorial.) We
can also make tables. I don’t have nice styling yet, but you get the idea (Table 1.1).
8                                                 CHAPTER 1. INTRODUCTION




    Figure 1.1: A screenshot of the Rails Tutorial site.




                  Foo bar     Baz Quux
                 foo bar      baz quux


                 Table 1.1: An ugly table.
1.4. MATH                                                                                                                               9

1.4        Math
While LTEX is great for writing a programming book, DocBook might have worked as well, and rumor has
       A

it DocBook can be converted to HTML. So why bother writing my own system? I went with LTEX for two
                                                                                        A

main reasons:

   1. I don’t know DocBook, but I know LTEX well, so I could be confident in being able to hack something
                                       A

      together.
   2. LTEX is great at math typesetting.
      A


    Though I expect Rails Tutorial will have little or no math, eventually I’d like to use this system to write
math-heavy articles and books, and DocBook can’t do math typesetting. So, in reality, the second consid-
eration alone was decisive, and—if you care about math typesetting—it’s not hard to see why; consider,
for example, the exponential decay factor e−t/τ , or the time-independent Schr¨ dinger equation in quantum
                                                                                 o
mechanics:

                                                          ¯2
                                                          h     2
                                                      −             +V     ψ = Eψ
                                                          2m
Both these math examples look great by construction in the PDF version of this document (since LTEX is a
                                                                                                     A

master math typesetter), but if you’re viewing them on the web you’ll see that they look great there, too.
    How does this work? I began with the only place I’d seen nice math typesetting on the web: Wikipedia’s
math articles.2 This had always been a mystery, because all the nice math typesetting I’d ever seen was
produced by LTEX(or by TEX itself). So how does Wikipedia do it? Well, Wikipedia is built on top of
               A

MediaWiki, and MediaWiki comes bundled with a program called texvc that converts wiki math markup to
pretty PNGs using—wait for it—LTEX. So PolyTEXnic, like MediaWiki, just uses texvc under the hood.3
                                   A




   2 Not  quite true: MathWorld looks nice, too, but it’s closed-source.
   3 Ifyou look at the MediaWiki source to find out how it does math conversion, you’ll see that it’s literally just a shell call to texvc,
so that’s what I did as well.
10   CHAPTER 1. INTRODUCTION
Chapter 2

Lorem ipsum

We certainly expect to have more than one chapter in this book. Here’s the start of a second one.


2.1     Lorem
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et
dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex
ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat
nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim
id est laborum.

Listing 2.1. The Forum model.


# == Schema Information
# Schema version: 20080916002106
#
# Table name: forums
#
# id            :integer(4)      not null, primary key
# name          :string(255)
# description :text
# topics_count :integer(4)       default(0), not null
# created_at    :datetime
# updated_at    :datetime
#

class Forum < ActiveRecord::Base
  attr_accessible :name, :description

  has_many :topics, :order => "created_at DESC", :dependent => :destroy
  has_many :posts, :through => :topics


  validates_length_of :name, :maximum => 255, :allow_nil => true


                                                     11
12                                                                          CHAPTER 2. LOREM IPSUM


  validates_length_of :description, :maximum => 1000, :allow_nil => true
end




2.2     Ipsum
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et
dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex
ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat
nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim
id est laborum.

More Related Content

Viewers also liked

Ruby + Rails
Ruby + RailsRuby + Rails
Ruby + Rails
betabeers
 
Learning Rails
Learning RailsLearning Rails
Learning Rails
David Francisco
 
Ruby on Rails 5: Top 5 Features
Ruby on Rails 5: Top 5 FeaturesRuby on Rails 5: Top 5 Features
Ruby on Rails 5: Top 5 Features
PhraseApp
 
Dev ops.continuous delivery - Ibon Landa (Plain Concepts)
Dev ops.continuous delivery - Ibon Landa (Plain Concepts)Dev ops.continuous delivery - Ibon Landa (Plain Concepts)
Dev ops.continuous delivery - Ibon Landa (Plain Concepts)
betabeers
 
Rails course day 5
Rails course day 5Rails course day 5
Rails course day 5
Al Sayed Gamal
 
Ionic Hybrid Mobile Application
Ionic Hybrid Mobile ApplicationIonic Hybrid Mobile Application
Ionic Hybrid Mobile Application
Al Sayed Gamal
 
Introducción a scrum - Rodrigo Corral (Plain Concepts)
Introducción a scrum - Rodrigo Corral (Plain Concepts)Introducción a scrum - Rodrigo Corral (Plain Concepts)
Introducción a scrum - Rodrigo Corral (Plain Concepts)
betabeers
 
Enterprise Architectures with Ruby (and Rails)
Enterprise Architectures with Ruby (and Rails)Enterprise Architectures with Ruby (and Rails)
Enterprise Architectures with Ruby (and Rails)
Konstantin Gredeskoul
 

Viewers also liked (8)

Ruby + Rails
Ruby + RailsRuby + Rails
Ruby + Rails
 
Learning Rails
Learning RailsLearning Rails
Learning Rails
 
Ruby on Rails 5: Top 5 Features
Ruby on Rails 5: Top 5 FeaturesRuby on Rails 5: Top 5 Features
Ruby on Rails 5: Top 5 Features
 
Dev ops.continuous delivery - Ibon Landa (Plain Concepts)
Dev ops.continuous delivery - Ibon Landa (Plain Concepts)Dev ops.continuous delivery - Ibon Landa (Plain Concepts)
Dev ops.continuous delivery - Ibon Landa (Plain Concepts)
 
Rails course day 5
Rails course day 5Rails course day 5
Rails course day 5
 
Ionic Hybrid Mobile Application
Ionic Hybrid Mobile ApplicationIonic Hybrid Mobile Application
Ionic Hybrid Mobile Application
 
Introducción a scrum - Rodrigo Corral (Plain Concepts)
Introducción a scrum - Rodrigo Corral (Plain Concepts)Introducción a scrum - Rodrigo Corral (Plain Concepts)
Introducción a scrum - Rodrigo Corral (Plain Concepts)
 
Enterprise Architectures with Ruby (and Rails)
Enterprise Architectures with Ruby (and Rails)Enterprise Architectures with Ruby (and Rails)
Enterprise Architectures with Ruby (and Rails)
 

Similar to rails_tutorial

Getting started erlang
Getting started erlangGetting started erlang
Getting started erlang
Kwanzoo Dev
 
Wise Document Translator Report
Wise Document Translator ReportWise Document Translator Report
Wise Document Translator Report
Raouf KESKES
 
Sample thesis
Sample thesisSample thesis
Sample thesis
kmmanuel
 
Document Object Model (DOM) Level 1 Specification
Document Object Model (DOM) Level 1 SpecificationDocument Object Model (DOM) Level 1 Specification
Document Object Model (DOM) Level 1 Specification
Otakism
 
rails.html
rails.htmlrails.html
rails.html
tutorialsruby
 
rails.html
rails.htmlrails.html
rails.html
tutorialsruby
 
Te xworks manual
Te xworks manualTe xworks manual
MapReduce in Cloud Computing
MapReduce in Cloud ComputingMapReduce in Cloud Computing
MapReduce in Cloud Computing
Mohammad Mustaqeem
 
robert-kovacsics-part-ii-dissertation
robert-kovacsics-part-ii-dissertationrobert-kovacsics-part-ii-dissertation
robert-kovacsics-part-ii-dissertation
Robert Kovacsics
 
A Little Bit Of Help With Maple
A Little Bit Of Help With MapleA Little Bit Of Help With Maple
A Little Bit Of Help With Maple
Nathan Mathis
 
Machine Vision Toolbox for MATLAB (Relese 3)
Machine Vision Toolbox for MATLAB (Relese 3)Machine Vision Toolbox for MATLAB (Relese 3)
Machine Vision Toolbox for MATLAB (Relese 3)
CHIH-PEI WEN
 
The Function Pointer Tutorials
The Function Pointer TutorialsThe Function Pointer Tutorials
The Function Pointer Tutorials
Nont Banditwong
 
Thesis
ThesisThesis
Climb - Property-based dispatch in functional languages [Report]
Climb - Property-based dispatch in functional languages [Report]Climb - Property-based dispatch in functional languages [Report]
Climb - Property-based dispatch in functional languages [Report]
Christopher Chedeau
 
A gentle introduction to Latex
A gentle introduction to LatexA gentle introduction to Latex
A gentle introduction to Latex
Emmanuel Abatih
 
Cimlvojt 2013bach (1)
Cimlvojt 2013bach (1)Cimlvojt 2013bach (1)
Cimlvojt 2013bach (1)
Universidad Tecnológica del Perú
 
Key-Value Stores: a practical overview
Key-Value Stores: a practical overviewKey-Value Stores: a practical overview
Key-Value Stores: a practical overview
Marc Seeger
 
Perl-crash-course
Perl-crash-coursePerl-crash-course
Perl-crash-course
tutorialsruby
 
Perl-crash-course
Perl-crash-coursePerl-crash-course
Perl-crash-course
tutorialsruby
 
Perl-crash-course
Perl-crash-coursePerl-crash-course
Perl-crash-course
tutorialsruby
 

Similar to rails_tutorial (20)

Getting started erlang
Getting started erlangGetting started erlang
Getting started erlang
 
Wise Document Translator Report
Wise Document Translator ReportWise Document Translator Report
Wise Document Translator Report
 
Sample thesis
Sample thesisSample thesis
Sample thesis
 
Document Object Model (DOM) Level 1 Specification
Document Object Model (DOM) Level 1 SpecificationDocument Object Model (DOM) Level 1 Specification
Document Object Model (DOM) Level 1 Specification
 
rails.html
rails.htmlrails.html
rails.html
 
rails.html
rails.htmlrails.html
rails.html
 
Te xworks manual
Te xworks manualTe xworks manual
Te xworks manual
 
MapReduce in Cloud Computing
MapReduce in Cloud ComputingMapReduce in Cloud Computing
MapReduce in Cloud Computing
 
robert-kovacsics-part-ii-dissertation
robert-kovacsics-part-ii-dissertationrobert-kovacsics-part-ii-dissertation
robert-kovacsics-part-ii-dissertation
 
A Little Bit Of Help With Maple
A Little Bit Of Help With MapleA Little Bit Of Help With Maple
A Little Bit Of Help With Maple
 
Machine Vision Toolbox for MATLAB (Relese 3)
Machine Vision Toolbox for MATLAB (Relese 3)Machine Vision Toolbox for MATLAB (Relese 3)
Machine Vision Toolbox for MATLAB (Relese 3)
 
The Function Pointer Tutorials
The Function Pointer TutorialsThe Function Pointer Tutorials
The Function Pointer Tutorials
 
Thesis
ThesisThesis
Thesis
 
Climb - Property-based dispatch in functional languages [Report]
Climb - Property-based dispatch in functional languages [Report]Climb - Property-based dispatch in functional languages [Report]
Climb - Property-based dispatch in functional languages [Report]
 
A gentle introduction to Latex
A gentle introduction to LatexA gentle introduction to Latex
A gentle introduction to Latex
 
Cimlvojt 2013bach (1)
Cimlvojt 2013bach (1)Cimlvojt 2013bach (1)
Cimlvojt 2013bach (1)
 
Key-Value Stores: a practical overview
Key-Value Stores: a practical overviewKey-Value Stores: a practical overview
Key-Value Stores: a practical overview
 
Perl-crash-course
Perl-crash-coursePerl-crash-course
Perl-crash-course
 
Perl-crash-course
Perl-crash-coursePerl-crash-course
Perl-crash-course
 
Perl-crash-course
Perl-crash-coursePerl-crash-course
Perl-crash-course
 

More from tutorialsruby

&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
tutorialsruby
 
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>TopStyle Help &amp; &lt;b>Tutorial&lt;/b>
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>
tutorialsruby
 
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
tutorialsruby
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
tutorialsruby
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
tutorialsruby
 
Standardization and Knowledge Transfer – INS0
Standardization and Knowledge Transfer – INS0Standardization and Knowledge Transfer – INS0
Standardization and Knowledge Transfer – INS0
tutorialsruby
 
xhtml_basics
xhtml_basicsxhtml_basics
xhtml_basics
tutorialsruby
 
xhtml_basics
xhtml_basicsxhtml_basics
xhtml_basics
tutorialsruby
 
xhtml-documentation
xhtml-documentationxhtml-documentation
xhtml-documentation
tutorialsruby
 
xhtml-documentation
xhtml-documentationxhtml-documentation
xhtml-documentation
tutorialsruby
 
CSS
CSSCSS
CSS
CSSCSS
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa0602690047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
tutorialsruby
 
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa0602690047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
tutorialsruby
 
HowTo_CSS
HowTo_CSSHowTo_CSS
HowTo_CSS
tutorialsruby
 
HowTo_CSS
HowTo_CSSHowTo_CSS
HowTo_CSS
tutorialsruby
 
BloggingWithStyle_2008
BloggingWithStyle_2008BloggingWithStyle_2008
BloggingWithStyle_2008
tutorialsruby
 
BloggingWithStyle_2008
BloggingWithStyle_2008BloggingWithStyle_2008
BloggingWithStyle_2008
tutorialsruby
 
cascadingstylesheets
cascadingstylesheetscascadingstylesheets
cascadingstylesheets
tutorialsruby
 
cascadingstylesheets
cascadingstylesheetscascadingstylesheets
cascadingstylesheets
tutorialsruby
 

More from tutorialsruby (20)

&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
 
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>TopStyle Help &amp; &lt;b>Tutorial&lt;/b>
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>
 
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
 
Standardization and Knowledge Transfer – INS0
Standardization and Knowledge Transfer – INS0Standardization and Knowledge Transfer – INS0
Standardization and Knowledge Transfer – INS0
 
xhtml_basics
xhtml_basicsxhtml_basics
xhtml_basics
 
xhtml_basics
xhtml_basicsxhtml_basics
xhtml_basics
 
xhtml-documentation
xhtml-documentationxhtml-documentation
xhtml-documentation
 
xhtml-documentation
xhtml-documentationxhtml-documentation
xhtml-documentation
 
CSS
CSSCSS
CSS
 
CSS
CSSCSS
CSS
 
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa0602690047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
 
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa0602690047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
 
HowTo_CSS
HowTo_CSSHowTo_CSS
HowTo_CSS
 
HowTo_CSS
HowTo_CSSHowTo_CSS
HowTo_CSS
 
BloggingWithStyle_2008
BloggingWithStyle_2008BloggingWithStyle_2008
BloggingWithStyle_2008
 
BloggingWithStyle_2008
BloggingWithStyle_2008BloggingWithStyle_2008
BloggingWithStyle_2008
 
cascadingstylesheets
cascadingstylesheetscascadingstylesheets
cascadingstylesheets
 
cascadingstylesheets
cascadingstylesheetscascadingstylesheets
cascadingstylesheets
 

Recently uploaded

GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
Tomaz Bratanic
 
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying AheadDigital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Wask
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Safe Software
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
Zilliz
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
ssuserfac0301
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
tolgahangng
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Malak Abu Hammad
 
Webinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data WarehouseWebinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data Warehouse
Federico Razzoli
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
Zilliz
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
Zilliz
 
Project Management Semester Long Project - Acuity
Project Management Semester Long Project - AcuityProject Management Semester Long Project - Acuity
Project Management Semester Long Project - Acuity
jpupo2018
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
Mariano Tinti
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
Brandon Minnick, MBA
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Alpen-Adria-Universität
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Speck&Tech
 

Recently uploaded (20)

GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
 
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying AheadDigital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying Ahead
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
 
Webinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data WarehouseWebinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data Warehouse
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
 
Project Management Semester Long Project - Acuity
Project Management Semester Long Project - AcuityProject Management Semester Long Project - Acuity
Project Management Semester Long Project - Acuity
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
 

rails_tutorial

  • 1. Ruby on Rails Tutorial Learn Rails by Example Michael Hartl
  • 2. 2
  • 3. Contents 1 Introduction 5 1.1 Technology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.2 Sample code and such . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 1.2.1 Code samples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 1.2.2 And such . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 1.3 Figures and tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 1.4 Math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 2 Lorem ipsum 11 2.1 Lorem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 2.2 Ipsum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 3
  • 4. 4 CONTENTS
  • 5. Chapter 1 Introduction This is a sample document for what will become the Ruby on Rails Tutorial book. It is by no means a real book yet—there’s even some lorem ipsum text in what follows (see, e.g., Box 1.1 and Chapter 2). Its purpose is to show that all the elements are in place to produce a pleasing final product, and to give a hint of what that final product might eventually look like. Box 1.1. Cicero dixit Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Ex- cepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 1.1 Technology Rails Tutorial is written in PolyTEXnic, a dialect of the LTEX technical typesetting system (which in turn A is based on TEX). I wrote PolyTEXnic because I wanted to produce both beautiful PDF (and hence print) documents and pretty HTML documents at the same time, from the same source file. As a result, this sample document is available both as HTML and as a PDF download. Both formats come with plenty of rich book-y goodness, including a linked table of contents; numbered sections, tables, figures, and code listings; linked cross references; syntax highlighting; numbered, shaded sidebars (Box 1.1); footnotes1 ; and even math (Section 1.4). Currently, PolyTEXnic is only available for my private use, but I plan to release it as an open-source project once it’s battle-tested and ready for general consumption. 1 Like this. 5
  • 6. 6 CHAPTER 1. INTRODUCTION 1.2 Sample code and such Often, Rails books include lots of code samples and such. Let’s look at some examples of how PolyTEXnic makes them both useful and pretty. 1.2.1 Code samples We’ll likely have some Ruby code in our Rails tutorial, with snippets such as the following: Listing 1.1. Part of the ClassMethods module in ActiveRecord::Validations. module ClassMethods . . . # File rails-2.3.2/activerecord/lib/active_record/validations.rb, line 511 def validates_presence_of(*attr_names) configuration = { :on => :save } configuration.update(attr_names.extract_options!) # can’t use validates_each here, because it cannot cope with # nonexistent attributes, while errors.add_on_empty can send(validation_method(configuration[:on]), configuration) do |record| record.errors.add_on_blank(attr_names, configuration[:message]) end end . . . end Note that Listing 1.1 is both syntax-highlighted and numbered—and, as you can see, it can be cross- referenced as well. Of course, we’re not limited to vanilla Ruby; let’s also include some embedded Ruby: Listing 1.2. The index.html.erb file for the People controller. <%- column_div :type => :primary do -%> <% unless @people.empty? -%> <h2>People</h2> <%= will_paginate %> <ul class="list people"> <%= render :partial => @people %> </ul> <%= will_paginate %> <% end -%> <%- end -%> <%- column_div :type => :secondary do -%>
  • 7. 1.3. FIGURES AND TABLES 7 <%= render :partial => ’searches/box’ %> <%= render :partial => ’shared/minifeed’ %> <%- end -%> In fact, we can include code in virtually any language and get nice syntax highlighting for free via the excellent Pygments program: ;; Common Lisp ;; Return the square of the given number. (defun square (x) "Calculates the square of the single-float x." (declare (single-float x) (optimize (speed 3) (debug 0) (safety 1))) (* x x)) Note here that we have a nice highlighted code block, but no listing number. This is useful for short snippets that aren’t likely ever to be referenced. 1.2.2 And such Code isn’t everything; sometimes you just want to show shell commands. No problem: $ rake -T db (in /Users/mhartl/rails/rails_tutorial) rake backup:db # Backup the current database. rake db:avatars:delete # Delete all the avatars. rake db:avatars:load # Make sample avatars. . . . We can also include console sessions: >> a = 1 => 1 >> puts a 1 => nil 1.3 Figures and tables It’s easy to include figures, such as screenshots (Fig. 1.1). (These are particularly useful for a tutorial.) We can also make tables. I don’t have nice styling yet, but you get the idea (Table 1.1).
  • 8. 8 CHAPTER 1. INTRODUCTION Figure 1.1: A screenshot of the Rails Tutorial site. Foo bar Baz Quux foo bar baz quux Table 1.1: An ugly table.
  • 9. 1.4. MATH 9 1.4 Math While LTEX is great for writing a programming book, DocBook might have worked as well, and rumor has A it DocBook can be converted to HTML. So why bother writing my own system? I went with LTEX for two A main reasons: 1. I don’t know DocBook, but I know LTEX well, so I could be confident in being able to hack something A together. 2. LTEX is great at math typesetting. A Though I expect Rails Tutorial will have little or no math, eventually I’d like to use this system to write math-heavy articles and books, and DocBook can’t do math typesetting. So, in reality, the second consid- eration alone was decisive, and—if you care about math typesetting—it’s not hard to see why; consider, for example, the exponential decay factor e−t/τ , or the time-independent Schr¨ dinger equation in quantum o mechanics: ¯2 h 2 − +V ψ = Eψ 2m Both these math examples look great by construction in the PDF version of this document (since LTEX is a A master math typesetter), but if you’re viewing them on the web you’ll see that they look great there, too. How does this work? I began with the only place I’d seen nice math typesetting on the web: Wikipedia’s math articles.2 This had always been a mystery, because all the nice math typesetting I’d ever seen was produced by LTEX(or by TEX itself). So how does Wikipedia do it? Well, Wikipedia is built on top of A MediaWiki, and MediaWiki comes bundled with a program called texvc that converts wiki math markup to pretty PNGs using—wait for it—LTEX. So PolyTEXnic, like MediaWiki, just uses texvc under the hood.3 A 2 Not quite true: MathWorld looks nice, too, but it’s closed-source. 3 Ifyou look at the MediaWiki source to find out how it does math conversion, you’ll see that it’s literally just a shell call to texvc, so that’s what I did as well.
  • 10. 10 CHAPTER 1. INTRODUCTION
  • 11. Chapter 2 Lorem ipsum We certainly expect to have more than one chapter in this book. Here’s the start of a second one. 2.1 Lorem Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Listing 2.1. The Forum model. # == Schema Information # Schema version: 20080916002106 # # Table name: forums # # id :integer(4) not null, primary key # name :string(255) # description :text # topics_count :integer(4) default(0), not null # created_at :datetime # updated_at :datetime # class Forum < ActiveRecord::Base attr_accessible :name, :description has_many :topics, :order => "created_at DESC", :dependent => :destroy has_many :posts, :through => :topics validates_length_of :name, :maximum => 255, :allow_nil => true 11
  • 12. 12 CHAPTER 2. LOREM IPSUM validates_length_of :description, :maximum => 1000, :allow_nil => true end 2.2 Ipsum Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.