SlideShare ist ein Scribd-Unternehmen logo
1 von 12
Downloaden Sie, um offline zu lesen
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.

Weitere ähnliche Inhalte

Andere mochten auch

Ruby + Rails
Ruby + RailsRuby + Rails
Ruby + Railsbetabeers
 
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 FeaturesPhraseApp
 
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
 
Ionic Hybrid Mobile Application
Ionic Hybrid Mobile ApplicationIonic Hybrid Mobile Application
Ionic Hybrid Mobile ApplicationAl 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
 

Andere mochten auch (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)
 

Ähnlich wie rails_tutorial

Getting started erlang
Getting started erlangGetting started erlang
Getting started erlangKwanzoo Dev
 
Wise Document Translator Report
Wise Document Translator ReportWise Document Translator Report
Wise Document Translator ReportRaouf KESKES
 
Sample thesis
Sample thesisSample thesis
Sample thesiskmmanuel
 
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 SpecificationOtakism
 
robert-kovacsics-part-ii-dissertation
robert-kovacsics-part-ii-dissertationrobert-kovacsics-part-ii-dissertation
robert-kovacsics-part-ii-dissertationRobert 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 MapleNathan 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 TutorialsNont Banditwong
 
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 LatexEmmanuel Abatih
 
Key-Value Stores: a practical overview
Key-Value Stores: a practical overviewKey-Value Stores: a practical overview
Key-Value Stores: a practical overviewMarc Seeger
 

Ähnlich wie 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
 

Mehr von 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 – INS0tutorialsruby
 
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa0602690047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269tutorialsruby
 
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa0602690047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269tutorialsruby
 
BloggingWithStyle_2008
BloggingWithStyle_2008BloggingWithStyle_2008
BloggingWithStyle_2008tutorialsruby
 
BloggingWithStyle_2008
BloggingWithStyle_2008BloggingWithStyle_2008
BloggingWithStyle_2008tutorialsruby
 
cascadingstylesheets
cascadingstylesheetscascadingstylesheets
cascadingstylesheetstutorialsruby
 
cascadingstylesheets
cascadingstylesheetscascadingstylesheets
cascadingstylesheetstutorialsruby
 

Mehr von 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
 

Kürzlich hochgeladen

Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditSkynet Technologies
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...AliaaTarek5
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 

Kürzlich hochgeladen (20)

Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance Audit
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 

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.