Agile Web Development with Rails 4th edition Beta 10 version Sam Ruby - The ebook is ready for instant download and access
Agile Web Development with Rails 4th edition Beta 10 version Sam Ruby - The ebook is ready for instant download and access
https://ebookultra.com/download/agile-web-development-with-
rails-5-1st-edition-sam-ruby/
https://ebookultra.com/download/ruby-on-rails-tutorial-learn-web-
development-with-rails-3rd-edition-michael-hartl/
https://ebookultra.com/download/build-your-own-ruby-on-rails-web-
applications-1st-edition-patrick-lenz/
https://ebookultra.com/download/practical-reporting-with-ruby-and-
rails-1st-edition-david-berube/
RailsSpace Building a Social Networking Website with Ruby
on Rails Addison Wesley Professional Ruby Series Hartl
https://ebookultra.com/download/railsspace-building-a-social-
networking-website-with-ruby-on-rails-addison-wesley-professional-
ruby-series-hartl/
https://ebookultra.com/download/practical-jruby-on-rails-
web-2-0-projects-bringing-ruby-on-rails-to-the-java-platform-1st-
edition-ola-bini/
https://ebookultra.com/download/pro-active-record-databases-with-ruby-
and-rails-1st-edition-kevin-marshall/
https://ebookultra.com/download/ruby-on-rails-background-jobs-with-
sidekiq-1-converted-edition-david-b-copeland/
https://ebookultra.com/download/ruby-on-rails-for-dummies-1st-edition-
burd/
Agile Web Development with Rails 4th edition Beta 10
version Sam Ruby Digital Instant Download
Author(s): Sam Ruby, Dave Thomas, David Heinemeier Hansson
ISBN(s): 9781934356548, 1934356549
Edition: Fourth Edition
File Details: PDF, 14.89 MB
Year: 2011
Language: english
Prepared exclusively for Anton Fonarev
Beta
Book
Agile publishing for agile developers
Be warned. The book has not had a full technical edit, so it will contain
errors. It has not been copyedited, so it will be full of typos and other weird-
ness. And there’s been no effort spent doing layout, so you’ll find bad page
breaks, over-long lines with little black rectangles, incorrect hyphenations,
and all the other ugly things that you wouldn’t expect to see in a finished
book. We can’t be held liable if you use this book to try to create a spiffy
application and you somehow end up with a strangely shaped farm imple-
ment instead. Despite all this, we think you’ll enjoy it!
Sam Ruby
Dave Thomas
David Heinemeier Hansson
Leon Breedt
with
Mike Clark
James Duncan Davidson
Justin Gehtland
Andreas Schwarz
Every precaution was taken in the preparation of this book. However, the publisher assumes no
responsibility for errors or omissions, or for damages that may result from the use of information
(including program listings) contained herein.
Our Pragmatic courses, workshops, and other products can help you and your team create better
software and have more fun. For more information, as well as the latest Pragmatic titles, please
visit us at http://www.pragprog.com.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any
form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the
prior consent of the publisher.
ISBN-10: 1-934356-54-9
ISBN-13: 978-1-934356-54-8
Printed on acid-free paper.
B10.0 printing, October 28, 2010
Version: 2010-10-28
Acknowledgements 16
Introduction 18
Rails Simply Feels Right . . . . . . . . . . . . . . . . . . . . . . . . . 18
Rails Is Agile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Who This Book Is For . . . . . . . . . . . . . . . . . . . . . . . . . . 21
How To Read This Book . . . . . . . . . . . . . . . . . . . . . . . . . 21
1 Installing Rails 26
1.1 Installing on Windows . . . . . . . . . . . . . . . . . . . . . . 26
1.2 Installing on Mac OS X . . . . . . . . . . . . . . . . . . . . . 28
1.3 Installing on Linux . . . . . . . . . . . . . . . . . . . . . . . . 29
1.4 Choosing a Rails Version . . . . . . . . . . . . . . . . . . . . 30
1.5 Setting Up Your Development Environment . . . . . . . . . 31
1.6 Rails and Databases . . . . . . . . . . . . . . . . . . . . . . . 35
1.7 What We Just Did . . . . . . . . . . . . . . . . . . . . . . . . 36
2 Instant Gratification 37
2.1 Creating a New Application . . . . . . . . . . . . . . . . . . . 37
2.2 Hello, Rails! . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
2.3 Linking Pages Together . . . . . . . . . . . . . . . . . . . . . 46
2.4 What We Just Did . . . . . . . . . . . . . . . . . . . . . . . . 48
4 Introduction to Ruby 57
4.1 Ruby Is an Object-Oriented Language . . . . . . . . . . . . 57
4.2 Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
4.3 Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
4.4 Organizing Structures . . . . . . . . . . . . . . . . . . . . . . 65
4.5 Marshaling Objects . . . . . . . . . . . . . . . . . . . . . . . 68
4.6 Pulling It All Together . . . . . . . . . . . . . . . . . . . . . . 68
4.7 Ruby Idioms . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Report erratum
Prepared exclusively for Anton Fonarev this copy is (B10.0 printing, October 28, 2010)
CONTENTS 7
Report erratum
Prepared exclusively for Anton Fonarev this copy is (B10.0 printing, October 28, 2010)
CONTENTS 8
22 Caching 369
22.1 Page Caching . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
22.2 Expiring Pages . . . . . . . . . . . . . . . . . . . . . . . . . . 372
22.3 Fragment Caching . . . . . . . . . . . . . . . . . . . . . . . . 378
23 Migrations 384
23.1 Creating and Running Migrations . . . . . . . . . . . . . . . 384
23.2 Anatomy of a Migration . . . . . . . . . . . . . . . . . . . . . 387
23.3 Managing Tables . . . . . . . . . . . . . . . . . . . . . . . . . 391
23.4 Advanced Migrations . . . . . . . . . . . . . . . . . . . . . . 395
23.5 When Migrations Go Bad . . . . . . . . . . . . . . . . . . . . 400
23.6 Schema Manipulation Outside Migrations . . . . . . . . . . 401
Report erratum
Prepared exclusively for Anton Fonarev this copy is (B10.0 printing, October 28, 2010)
CONTENTS 9
A Bibliography 444
Index 445
Report erratum
Prepared exclusively for Anton Fonarev this copy is (B10.0 printing, October 28, 2010)
Changes in the Beta Releases
Beta 10—October 28
This beta introduces a chapter on plugins and completes the first draft. Plug-
ins are not merely an afterthought or an advanced feature of Rails, with Rails
3.0 it is a fully architected way to augment or even replace base Rails func-
tionality.
This also completes the first draft. If you spot something missing, now would
be an excellent time to report it via the forums or via an errata. After a few
weeks of addressing comments it will be onto production where formatting
and typographical and indexing glitches will be resolved.
This draft has also been tested against the Rails 3.0.1 release. No changes
were needed to make the code in this book work against that release.
Beta 9—October 6
With this beta comes a new chapter on Rails’ dependencies. Understanding
these dependencies are as important as understanding Rails itself. Introducing
this chapter has produced a minor shifting of content: some text that originally
was present in the rather large chapter on Action View has moved into this one.
Additional shifts are expected in the next beta: all such will be noted here.
While running with edge rails directly from git is not recommended at this
time, those that do run such may spot that csrf_meta_tag has been renamed to
csrf_meta_tags in that release. This does not affect any scenario in the book.
As always, thanks for all of the wonderful feedback via the errata, forums, and
other venues. At this time I would like to specifically thank Leonel S, Martin
Zoller, and Jim Puls.
Beta 8—September 9
Rails 3.0 final has shipped! Even better news: no API changes that affect the
book were introduced in the process. In one case, namely in the use of an
Action View helper from a standalone library, you will want to be using the
New with this beta are three new chapters completing the coverage of the
externals of Rails. Caching covers how to effectively optimize your applica-
tion by eliminating the overhead of re-computing results that rarely change.
Migrations covers how to maintain your schemas. And finally, Non-Browser
Applications shows you how to access some or all of Rails functions either
locally or remotely.
As we enter into the home stretch, your feedback becomes all the more impor-
tant. There is an Report Erratum link at the bottom right of every page. Use it
when you spot something, even if you aren’t sure! If you would like to start a
discussion, the forum is a better place for that.
Beta 7—August 25
We have a new release candidate of Rails, as well as an official release of Ruby
1.9.2. I’m pleased to report that once again, no changes were made to any Rails
API that affect the book. Furthermore, the regression that in the first release
candidate which broke the ability to build the guides has been addressed.
New with this beta is a chapter on Action View, which covers templates, helpers,
layouts, and partials. At this point, all three parts of the Model/View/Controller
architecture are covered. Next up will be a chapter on accessing Rails applica-
tions from outside of a web server, either directly via APIs or as a web service.
As always, thanks for all of the wonderful feedback via the errata, forums, and
other venues. At this time I would like to specifically thank Kim Shrier, Don
Smith, mltsy, and Jason Catena.
Beta 6—July 27
The big news is that the release candidate for Rails has officially shipped. The
better news is that no API changes were made to Rails that affect the book.
Hopefully at this point releases of Rails will be made more quickly, and the API
will remain stable.
New with this beta is a chapter on Action Dispatch and Action Controller,
which covers both dispatching of requests to controllers, as well as controllers
themselves. At this point, two of the three parts on the Model/View/Controller
architecture are complete. Next up will be a chapter on Views.
Once again, thanks for all of the wonderful feedback via the errata, forums,
and other venues – keep it coming!
Report erratum
Prepared exclusively for Anton Fonarev this copy is (B10.0 printing, October 28, 2010)
B ETA 5—J UNE 28 12
Beta 5—June 28
As I write this, the release candidate which was originally due on June 8th has
still not shipped, and we have decided to release a beta anyway. At the current
time, the contents of the book work with beta 4 and with the latest version of
Rails from github, but things could change between now and when the release
candidate ships.
The changes to Rails that affected this book in beta 4 were the requirement
to specify the keyword ’new’ when creating a new application with the ’rails’
command, and the fact that Rails 3.0 no longer works on Ruby 1.9.1. Ruby
1.9.2 preview 3, however, has come out and Rails 3.0 works just fine on it.
This beta has been updated to reflect these changes.
This beta also adds a chapter dedicated to Active Record, a topic which covered
three chapters in edition 3. The content has been updated to reflect Rails 3
APIs, and in particular ARel functionality. The content has been streamlined
to focus only on APIs that everybody needs to know, as well as content that
was adequately covered in Part II. It also reads less like a reference manual,
and more like a guide. Feedback welcome. In particular, please let me know if
something you feel is essential was not covered.
Again, thanks for all of the excellent errata. At this point, we are up to over
300 errata comments from over 80 individuals. I’d like to specifically thank
Seth Arnold, David Hadley, Will Bowlin, Victor Marius Costan, Kristian Riiber
Mandrup, Joe Straitiff, and Andy Brice.
Beta 4—May 26
This beta adds two chapters. The first recaps what was learned in part 2:
model, view, controller, configuration, testing, and deployment. It then contin-
ues with an explanation on how to generate documentation for your applica-
tion.
Chapter 18 is also new with this beta: it goes directory by directory through
your Rails application, describing what goes into each. You will see how to
generate documentation for Rails itself, how to build a Rake task, more infor-
mation on configuration options and naming conventions. This all sets the
stage for the chapters that follow.
Report erratum
Prepared exclusively for Anton Fonarev this copy is (B10.0 printing, October 28, 2010)
B ETA 3—M AY 11 13
Beta 3—May 11
This beta adds a deployment chapter which takes you through the installation,
configuration, and usage of a number of tools: Apache, Capistrano, MySQL,
and Passenger; as well as (mildly) deeper usage of Git and Bundler.
There’s not been another beta of Rails yet, so this is just a FYI at this point,
but usage of {{name}} syntax in i18n strings will be deprecated; the preferred
syntax is now %{name}.
As always thanks for all of the wonderful feedback via the errata, forums, and
other venues – keep it coming!
Beta 2—May 3
Thanks for all of the excellent feedback. To date we’ve gotten over 100 com-
ments from over 30 individuals. Special thanks go out to Trung LE, David
Hadley, Manuel E Vidaurre Arenas, Wayne Conrad, and Steve Nicholson. A lot
of the changes you’ll see in this second beta are the result of this input. Please
keep it up, as every comment helps us make this book better!
This new release adds coverage of sending mail and integration testing in the
new chapter “Task H: Sending Mail.” You’ll learn how to send mail, how to
function test mail, and how to integration test an end-to-end scenario span-
ning adding a product to a cart to the sending of a confirmation email.
Only one change to Rails affects the book this go around: Rails will be changing
the way that I18N and HTML safe strings interact. The text in the book has
been partially updated to reflect the new direction, but will continue to work
with Beta 3.
Report erratum
Prepared exclusively for Anton Fonarev this copy is (B10.0 printing, October 28, 2010)
Preface to the Fourth Edition
When Dave asked me to join as a coauthor of the third edition of this book,
I was thrilled. After all, it was from the first printing of the first edition of
this book that I had learned Rails. Dave and I also have much in common.
Although he prefers Emacs and Mac OS X and my preferences tend toward Vim
and Ubuntu, we both share a love for the command line and getting our fin-
gers dirty with code—starting with tangible examples before diving into heavy
theory.
Since the time the third edition was published (and, in fact, since the first, sec-
ond and third editions), much has changed. Rails is in the process of being sig-
nificantly refactored, mostly internally. A number of features that were used in
previous examples have been initially deprecated and subsequently removed.
New features have been added, and much experience has been obtained as to
what the best practices are for using Rails. Rails now also works on Ruby 1.9,
and each of the examples have been tested with Ruby 1.8.7 and Ruby 1.9.2.
This has led to a reorganization of the book. Many newcomers to Rails have
not had the pleasure of being introduced to Ruby, so this section has been
promoted from an appendix to a chapter in the first section. We follow this
section with a step-by-step walk through of building a real application, which
has been updated and streamlined to focus on current best practices. But the
biggest change is in the final section: as it is no longer practical to cover the
entire ecosystem of Rails given both its breadth and rate of change, what this
section is now focused on is to provide an overall perspective of the landscape,
enabling you, the reader, to know what to look for and where to find plug-
ins and related tools to address common needs that go far beyond what the
framework itself contains.
Sam Ruby
April 2010
Report erratum
Prepared exclusively for Anton Fonarev this copy is (B10.0 printing, October 28, 2010)
Acknowledgements
You’d think that producing a new edition of a book would be easy. After all,
you already have all the text. It’s just a tweak to some code here and a minor
wording change there, and you’re done. You’d think....
It’s difficult to tell exactly, but our impression is that creating each edition
of Agile Web Development with Rails took about as much effort as the first
edition. Rails is constantly evolving and, as it does, so has this book. Parts
of the Depot application were rewritten several times, and all of the narrative
was updated. The emphasis on REST and the avoidance of features as they
become deprecated have repeatedly changed the structure of the book as what
was once hot became just lukewarm.
So, this book would not exist without a massive amount of help from the
Ruby and Rails communities. Each edition of this book has been released as
a beta book: early versions were posted as PDFs, and people made comments
online. And comment they did: more than 1,200 suggestions and bug reports
were posted. The vast majority ended up being incorporated, making this book
immeasurably more useful than it would have been. Thank you all, both for
supporting the beta book program and for contributing so much valuable
feedback.
The Rails core team has been incredibly helpful, answering questions, check-
ing out code fragments, and fixing bugs. A big thank you to the following:
We’d like to thank the folks who contributed the specialized chapters to the
book: Leon Breedt, Mike Clark, James Duncan Davidson, Justin Gehtland,
and Andreas Schwarz.
Sam Ruby
April 2010
rubys@intertwingly.net
Dave Thomas
November 2006
dave@pragprog.com
Report erratum
Prepared exclusively for Anton Fonarev this copy is (B10.0 printing, October 28, 2010)
In this chapter, we’ll see
• What rails is,
• agile practices,
• who the book is for, and
• how to read the book.
Introduction
Ruby on Rails is a framework that makes it easier to develop, deploy, and
maintain web applications. During the months that followed its initial release,
Rails went from being an unknown toy to being a worldwide phenomenon and,
more important, it has become the framework of choice for the implementation
of a wide range of so-called Web 2.0 applications.
Why is that?
But easy on its own doesn’t cut it. We’re talking about professional developers
writing real-world websites. They wanted to feel that the applications they
were developing would stand the test of time—that they were designed and
implemented using modern, professional techniques. So, these developers dug
into Rails and discovered it wasn’t just a tool for hacking out sites.
For example, all Rails applications are implemented using the Model-View-
Controller (MVC) architecture. Java developers are used to frameworks such
as Tapestry and Struts, which are based on MVC. But Rails takes MVC further:
when you develop in Rails, you start with a working application, there’s a
place for each piece of code, and all the pieces of your application interact in a
standard way.
Professional programmers write tests. And again, Rails delivers. All Rails appli-
cations have testing support baked right in. As you add functionality to the
code, Rails automatically creates test stubs for that functionality. The frame-
work makes it easy to test applications, and as a result, Rails applications
tend to get tested.
Rails takes Ruby to the limit, extending it in novel ways that make a pro-
grammer’s life easier. This makes our programs shorter and more readable.
It also allows us to perform tasks that would normally be done in external
configuration files inside the codebase instead. This makes it far easier to see
what’s happening. The following code defines the model class for a project.
Don’t worry about the details for now. Instead, just think about how much
information is being expressed in a few lines of code.
class Project < ActiveRecord::Base
belongs_to :portfolio
has_one :project_manager
has_many :milestones
has_many :deliverables, :through => :milestones
Two other philosophical underpinnings keep Rails code short and readable:
DRY and convention over configuration. DRY stands for don’t repeat yourself :
every piece of knowledge in a system should be expressed in just one place.
Rails uses the power of Ruby to bring that to life. You’ll find very little duplica-
tion in a Rails application; you say what you need to say in one place—a place
often suggested by the conventions of the MVC architecture—and then move
on. For programmers used to other web frameworks, where a simple change
to the schema could involve them in half a dozen or more code changes, this
was a revelation.
Convention over configuration is crucial, too. It means that Rails has sensi-
ble defaults for just about every aspect of knitting together your application.
Follow the conventions, and you can write a Rails application using less code
than a typical Java web application uses in XML configuration. If you need to
override the conventions, Rails makes that easy, too.
Developers coming to Rails found something else, too. Rails isn’t playing catch-
up with the new de facto web standards; it’s helping define them. And Rails
makes it easy for developers to integrate features such as Ajax and RESTful
interfaces into their code, because support is built in. (And if you’re not famil-
iar with Ajax and REST interfaces, never fear—we’ll explain them later in the
book.)
Report erratum
Prepared exclusively for Anton Fonarev this copy is (B10.0 printing, October 28, 2010)
R AILS I S A GILE 20
Developers are worried about deployment, too. They found that with Rails you
can deploy successive releases of your application to any number of servers
with a single command (and roll them back equally easily should the release
prove to be somewhat less than perfect).
Rails Is Agile
The title of this book is Agile Web Development with Rails. You may be sur-
prised to discover that we don’t have explicit sections on applying agile prac-
tices X, Y, and Z to Rails coding.
The reason is both simple and subtle. Agility is part of the fabric of Rails.
Let’s look at the values expressed in the Agile Manifesto as a set of four pref-
erences:1
Rails is all about individuals and interactions. There are no heavy toolsets,
no complex configurations, and no elaborate processes. There are just small
groups of developers, their favorite editors, and chunks of Ruby code. This
leads to transparency; what the developers do is reflected immediately in what
the customer sees. It’s an intrinsically interactive process.
Report erratum
Prepared exclusively for Anton Fonarev this copy is (B10.0 printing, October 28, 2010)
Other documents randomly have
different content
supremazia del potere sacerdotale sui pubblici studii. E per
mantenere una continua ingerenza sulle scuole, concesse larghi
poteri all'arcidiacono. Il quale soleva essere un prelato, scelto dal
papa come suo rappresentante nella città di Bologna e chiamato
Cancellier Maggiore dello Studio, investito della facoltà di laureare in
tutte le scienze; di assolvere dottori e scolari incorsi nella scomunica
per aver percosso i chierici; di nominare in sua assenza un vicario, e
di partecipare ad un emolumento sulle promozioni.
L'arcidiacono in Bologna, e il vescovo nelle altre università,
partecipavano insieme al Rettore e ai dottori alla giurisdizione civile e
criminale, ed era lasciata libertà alla parte di scegliere fra questi tre
poteri, il proprio giudice.
L'autorità dell'arcidiacono in Bologna fu accresciuta dai papi che
succedettero ad Onorio III, i quali liberarono tal dignitario dai vincoli
delle leggi canoniche e in parte dagli oneri della gerarchia
ecclesiastica [166]. Celestino V concesse all'arcidiacono, perchè la sua
presenza fosse utile all'università, di riscuotere tutti i frutti delle
parrocchie a lui sottoposte senza obbligo di residenza. Lo stesso
privilegio venne dipoi conferito anche da Bonifazio VIII nel 1294 e da
papa Benedetto nel 1341 [167].
Un'autorità così estesa come quella dell'arcidiacono era mal
conciliabile colla indipendenza di cui godeva l'università, e perciò
frequenti discordie avvenivano fra il potere ecclesiastico che, in onta
agli statuti e alle consuetudini, vantava diritti di precedenza, ed i
Rettori che rappresentavano legalmente il supremo potere
scolastico [168].
Nell'università di Padova il potere ecclesiastico esercitò la sua
influenza in limiti assai più ristretti, perchè la repubblica di Venezia,
dalla quale dipendeva, non soffriva che altre autorità s'ingerissero
della vigilanza di quello Studio. Sebbene qualche volta si trovi
ricordato fin al secolo XV il vescovo in luogo del Rettore e incaricato
di sostituirlo nel grado scolastico; nel 1426 con lettere ducali fu tolto
questo abuso.
Nel 1437 il Senato decretò ancora che le controversie che nascevano
tra i collegi e che solevansi sottoporre alla decisione del vescovo,
fossero in avvenire portate dinanzi al pretore della città eccetto
quelle relative al collegio dei teologi [169].
Nel secolo successivo la storia di quell'università ci offre esempi assai
più rilevanti di emancipazione dall'autorità ecclesiastica. Nel 1564 il
Rettore dei giuristi a nome dei cisalpini scrisse all'imperatore
Massimiliano perchè inducesse il Senato di Venezia di mandare agli
scolari cisalpini che volevano prendere i gradi scolastici e la laurea, di
non fare professione di fede cattolica come aveva prescritto il
pontefice Pio IV. Di più per favorire gli stranieri non cattolici, si
fondarono nuovi collegi universitarii, che per contrapporli a quelli già
esistenti nei quali aveva influenza il potere ecclesiastico, furono detti
veneti (Collegia Veneta).
Da questi esempi e da molti altri che si potrebbero riferire,
desumiamo che l'ingerenza della Chiesa nelle università non era mai
uniforme, ma variava da una città ad un'altra secondo le diverse
costituzioni politiche. Quando lo Stato cominciò a prender parte
diretta all'ordinamento degli studii e a regolarne l'esercizio con leggi
speciali, l'autorità ecclesiastica nelle scuole andò sempre scemando,
finchè non rimase al clero che una parziale e limitata ingerenza negli
studii di teologia e di diritto canonico.
CAPITOLO SECONDO
1º Il Rettore;
2º Il Sindaco;
3º I Consiglieri;
4º Il Notaro;
5º Gli Attuari o Archivisti;
6º Il Massaro o Tesoriere;
7º I Peziarii;
8º Gli Stazionari;
9º I Bidelli;
10º I copisti, i miniatori, i rilegatori di libri, ecc.
· · · · · · · · · · · · · · · ·
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookultra.com