Put your site development on rails with Ruby!
by Barry Burd
USE RUBY TO DEVELOP WEB APPS, BLOGS, AND MORE
Dr. Barry Burd received an M.S. degree in Computer Science at Rutgers
University and a Ph.D. in Mathematics at the University of Illinois. As a teaching
assistant in Champaign-Urbana, Illinois, he was elected to the universitywide
“List of Teachers Ranked as Excellent by Their Students” five times.
Since 1980, Dr. Burd has been a professor in the Department of Mathematics
and Computer Science at Drew University in Madison, New Jersey. When he’s
not lecturing at Drew University, Dr. Burd leads training courses for professional
programmers in business and industry. He has lectured at conferences
in the United States, Europe, Australia, and Asia. He is the author of several
articles and books, including Java For Dummies, 4th Edition, and JSP:
JavaServer Pages, both from Wiley Publishing, Inc.
Dr. Burd lives in Madison, New Jersey, with his wife and two children. In his
spare time, he enjoys being a workaholic.
Author’s Acknowledgments
Many thanks to Paul Levesque who worked so closely with me on this
project, and thanks to Katie Feltman who headed up the project at Wiley.
And to Andy Cummings who steers the For Dummies series, thanks. And, yes,
thanks to copy editors Mary Lagu and Virginia Sanders. Also, thanks to Laura
Lewin, agent at StudioB. Thanks, and thanks again to Jay Zimmerman and the
speakers in the No Fluff, Just Stuff Symposium for opening my eyes to Ruby
on Rails. And to Charles Nutter and Thomas Enebo, who bridge the gap
between Ruby and Java, thanks. Of course, Matt Kent, Kyle Shank, and Marc
Baumbach, thanks for the use of RadRails, both inside and outside of this
book. I extend thanks to Stefan Reichert with his Wicked Shell. To Francis
Hwang and the members of the Ruby-NYC group, I say thanks. Thanks indeed
to Frank Greco and his New York Java Special Interest Group and to Mike
Redlich and the gang at the Amateur Computer Group of New Jersey because
without them I wouldn’t know anything about object-relational mapping.
Thanks. And special thanks to Sam and Jennie, and of course, to Harriet,
thanks I say thanks I will Thanks.
Introduction
“Ruby on Rails? What’s that?” asks my uncle. “You write about this
stuff for dummies? You mean those black and yellow books that everyone buys?”
“Yes, like the one I’m quoting you in,” I say. “Please check your spelling as you speak.”
“I will. But what’s Ruby on Rails? Is it the 6:05 train to Poughkeepsie? Is it the
name of an old vaudeville act? Is it a pop singer? A rock band? Is it a rare
stone from India? Is it the codename of an informer in a political scandal?”
“No.”
“Is it the name of an exotic cocktail? A species of bird? An animal act in a
circus? A John D. MacDonald title?”
Finally, I interrupt. “Ruby on Rails is a computer thing.”
“What kind of computer thing?” he asks.
“It’s a framework for creating applications with Web interfaces to databases.”
“Oh, yeah?” he says. “Your nephew from Brooklyn, he read Getting Ahead in
Politics For Dummies. He loved the book. Did you write that one?”
How to Use This Book
As a computer book author, I strive not to be full of myself. I have no illusions
that you plan on reading this book from cover to cover. I read sections and
chapters out of order when I buy a computer book. Why would I expect you to
approach my book any differently? And even if I read something in Chapter 2,
who says I remember it when I read Chapter 11?
I write each section with these thoughts in mind. In the middle of Chapter 12,
I might want you to remember some nugget of knowledge that I introduce in
Chapter 4. If I use that nugget over and over again in Chapters 5, 7, 8, and 9,
I don’t remind you about it in Chapter 12. But for other nuggets — ones that
you don’t read about repeatedly in this book — I provide cross references.
So in general, my advice is
Read what interests you; skip what doesn’t interest you.
If you already know something, don’t bother reading about it.
If you’re curious, don’t be afraid to skip ahead. You can always sneak a
peek at an earlier chapter if you really need to do so.
How This Book Is Organized
This book is divided into subsections, which are grouped into sections,
which come together to make chapters, which are lumped finally into four
parts. (When you write a book, you get to know your book’s structure pretty
well. After months of writing, you find yourself dreaming in sections and
chapters when you go to bed at night.) The parts of the book are listed here.
Part I: Nuts and Bolts
This part is your executive briefing. It includes a chapter that answers the
question “What is Ruby on Rails?” and a chapter with a complete set of
instructions on installing and running the software. It also has a jump-start
chapter and a chapter with details about the RadRails integrated development
environment.
Part II: Creating Code
Chapters 5 through 7 cover Ruby and HTML. Some of the material in Part II
might be familiar to you. If so, you can skip some sections or read this stuff
quickly. But don’t read too quickly. Ruby is a little different from some other
programming languages, and you might stumble upon some exciting new ideas.
Part III: Real Rails
This third part cuts to the chase. Rails has three components — Action
Controller, Action View, and Active Record. The controller controls things
(of course), the view displays things, and Active Record maintains all the data.
Chapters 8 through 13 cover these three components and describe some
interesting applications along the way.
Part IV: The Part of Tens
The Part of Tens is a little Ruby on Rails candy store. In the Part of Tens, you
can find lists — online resources, hints about Ruby, and other interesting goodies.
Product details
Price
|
|
---|---|
File Size
| 10,074 KB |
Pages
|
348 p |
File Type
|
PDF format |
ISBN
| 978-0-470-08120-4 |
Copyright
| 2007 by Wiley Publishing, Inc |
Contents at a Glance
Introduction
Part I: Nuts and Bolts
Chapter 1: Welcome to the World of Ruby on Rails
Chapter 2: Installing the Software
Chapter 3: Details on Rails
Chapter 4: Using RadRails
Part II: Creating Code
Chapter 5: Ruby One’s Day
Chapter 6: Ruby Two’s Day
Chapter 7: Weaving the Web
Part III: Real Rails
Chapter 8: Action-Packed Adventures
Chapter 9: Some Things You Can Do with Models
Chapter 10: I’ve Been Working on the Rails Code
Chapter 11: Image Is Everything
Chapter 12: More Model Magic
Chapter 13: Cool Things on Rails
Part IV: The Part of Tens
Chapter 14: Ten (Times Two) Great Web Sites
Chapter 15: Ten Features That Set Ruby Apart
Chapter 16: Ten Pivotal Ruby on Rails Concepts
Chapter 17: Ten Ways to Override Rails Defaults
Index
Table of Contents
Introduction..................................................................1
How to Use This Book .....................................................................................1
Conventions Used in This Book .....................................................................2
What You Don’t Have to Read ........................................................................2
Foolish Assumptions .......................................................................................3
How This Book Is Organized...........................................................................4
Part I: Nuts and Bolts .............................................................................4
Part II: Creating Code .............................................................................4
Part III: Real Rails....................................................................................5
Part IV: The Part of Tens........................................................................5
Icons Used in This Book..................................................................................5
Where to Go from Here....................................................................................6
Part I: Nuts and Bolts....................................................7
Chapter 1: Welcome to the World of Ruby on Rails . . . . . . . . . . . . . . . .9
The Software Development Process............................................................11
Agility .....................................................................................................12
Databases and the World Wide Web ..................................................12
Throwing frameworks at the problem...............................................13
Along Comes Ruby on Rails..........................................................................13
Why Ruby? ............................................................................................14
Why Rails? .............................................................................................17
Let’s Get Going................................................................................................19
Chapter 2: Installing the Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .21
Six Pieces of Software ....................................................................................22
Installing the Ruby Interpreter .....................................................................22
Testing the Ruby installation ..............................................................24
Troubleshooting the Ruby installation..............................................25
Installing Rails ................................................................................................26
Installing Java .................................................................................................27
Installing RadRails..........................................................................................28
Creating a RadRails shortcut on your desktop ................................30
Testing RadRails ...................................................................................31
Troubleshooting the RadRails installation........................................33
Configuring RadRails............................................................................33
Installing MySQL.............................................................................................36
Installing MySQL Administrator...................................................................40
Testing your MySQL installation ........................................................40
Troubleshooting your database connection.....................................41
Chapter 3: Details on Rails . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .47
Creating a Database .......................................................................................48
Creating a New Ruby on Rails Project .........................................................50
Running Your New Rails Project (Already!) ................................................53
Creating a Model ............................................................................................55
Creating a Database Table.............................................................................58
Creating a Scaffold .........................................................................................61
Using the New Web Interface........................................................................63
Chapter 4: Using RadRails . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .67
Words, Words, Words ....................................................................................67
What’s inside a view or an editor? .....................................................69
Understanding the big picture............................................................71
Some Common RadRails Tasks ....................................................................72
Changing the perspective....................................................................72
Showing a view .....................................................................................74
Using a wizard to create something...................................................76
Using the Generators view to create something ..............................78
Editing an existing file..........................................................................80
Running a Ruby program.....................................................................81
Visiting a URL........................................................................................82
Customizing RadRails ..........................................................................83
Troubleshooting the Run of a Ruby Program.............................................84
Does your Ruby code have a syntax error? ......................................85
Does your Ruby code have a semantic error?..................................85
Did you tell RadRails where to find a Ruby interpreter?.................86
Did you point RadRails to the correct location
of the Ruby interpreter?...................................................................86
Part II: Creating Code..................................................87
Chapter 5: Ruby One’s Day . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .89
Hello, Again .....................................................................................................90
A glimpse of a Ruby method ...............................................................90
Variables and values ............................................................................91
Ruby strings ..........................................................................................92
Working with Values.......................................................................................93
Displaying values..................................................................................94
Assigning values ...................................................................................94
Going with the Flow .......................................................................................95
Getting input from the keyboard........................................................96
Using keywords.....................................................................................97
Flowing the other way .........................................................................98
Going with the glow (or glowing with the flow) ...............................98
Bunches of Things........................................................................................100
Arrays...................................................................................................100
Hashes..................................................................................................102
Using Methods..............................................................................................104
Methods, methods everywhere........................................................106
Please pass the hash..........................................................................108
What’s the symbolism?......................................................................109
Chapter 6: Ruby Two’s Day . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .113
Objects and Classes.....................................................................................113
Creating objects..................................................................................115
Adding another file’s code to your own file’s code........................115
Classes, objects, and database tables .............................................116
Objects Have Methods ................................................................................117
Ruby’s handy iterators ......................................................................118
Finding iterators where you least expect them..............................121
Enhancing Classes........................................................................................122
Open classes .......................................................................................123
Being selfish ........................................................................................123
Defining subclasses............................................................................124
Creating a Module ........................................................................................127
Chapter 7: Weaving the Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .129
The Working of the Web ..............................................................................129
The Web developer’s point of view..................................................130
The Hypertext Transfer Protocol .....................................................131
Web pages ...........................................................................................132
Your HTML Starter Kit .................................................................................134
Start tags..............................................................................................136
End tags, empty tags, and paired tags.............................................137
If it feels good, do it............................................................................138
Entities .................................................................................................138
Comments and declarations .............................................................139
HTML Elements ............................................................................................140
Displaying images...............................................................................140
Using tables to align things ...............................................................142
Creating an HTML form .....................................................................144
Using form elements ..........................................................................147
Part III: Real Rails ....................................................153
Chapter 8: Action-Packed Adventures . . . . . . . . . . . . . . . . . . . . . . . . .155
Model/View/Controller ................................................................................155
Creating a controller and a view.......................................................157
Why you shouldn’t rename files.......................................................159
The Rails Way of Life....................................................................................161
Convention over configuration.........................................................161
Don’t Repeat Yourself (DRY).............................................................162
Writing What You Want Where You Want It ..............................................163
Sending text to the console...............................................................163
The art of Web server redirection....................................................165
Making the controller do the work ..................................................166
The Controller Shakes Hands with the View ............................................167
Using parameters ...............................................................................169
Getting parameters from a form.......................................................172
Dividing the Work of the View ....................................................................173
Creating and using a partial (a partial what?) ................................175
A view’s little helper...........................................................................176
Chapter 9: Some Things You Can Do with Models . . . . . . . . . . . . . . .177
A Web Site for Photos ..................................................................................178
Programming with a Rails Model ...............................................................182
Using Active Record...........................................................................184
Requiring a gem..................................................................................185
Connecting to the database ..............................................................185
Displaying data ...................................................................................187
Modifying a Database ..................................................................................189
More Rails Programming Tricks.................................................................192
Deleting rows ......................................................................................193
Adding rows ........................................................................................194
Finding rows........................................................................................196
Using SQL ............................................................................................198
Using id numbers ...............................................................................199
Chapter 10: I’ve Been Working on the Rails Code . . . . . . . . . . . . . . . .201
Displaying an Image .....................................................................................201
Creating code ......................................................................................202
Understanding the code ....................................................................204
Passing photos from place to place .................................................207
Importing Files..............................................................................................214
Importing files the easy way .............................................................214
Importing files the geeky way ...........................................................216
Chapter 11: Image Is Everything . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .219
Enhancing Your Project’s Code ..................................................................220
Follow the book’s longest step list...................................................220
Know the flow .....................................................................................226
Understanding the Enhanced Code ...........................................................228
Creating a database table..................................................................228
Moving on to more code . . ...............................................................228
Creating a file input field ...................................................................228
Creating a Photo instance .................................................................230
Reading the image bits ......................................................................230
Composing an image tag ...................................................................231
Sending image bits to the visitor’s browser....................................232
Whew!...................................................................................................232
Chapter 12: More Model Magic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .233
Blogging Your Dreams .................................................................................233
Validating the Visitor’s Input ......................................................................235
Adding Comments........................................................................................237
Adding Keywords.........................................................................................243
Connecting dreams with keywords..................................................244
How the Rails code does what it does.............................................251
Chapter 13: Cool Things on Rails . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .257
Using Ajax......................................................................................................257
Refresh part of a page, not the entire page.....................................258
Incorporating Ajax into a Rails page................................................258
Sending E-Mail ..............................................................................................263
Don’t blame me if it doesn’t work ....................................................263
Rails mail .............................................................................................264
Creating and Consuming Web Services.....................................................269
How to avoid screen scraping ..........................................................270
Building a Web service using Ruby on Rails ...................................271
Part IV: The Part of Tens ............................................277
Chapter 14: Ten (Times Two) Great Web Sites . . . . . . . . . . . . . . . . . . .279
Ten Ruby Sites ..............................................................................................279
Documentation ...................................................................................279
Open source Ruby projects...............................................................280
Starting points for Ruby resources ..................................................280
Discussing Ruby .................................................................................280
A weekly challenge.............................................................................280
Add-ons for Ruby................................................................................281
Meet people.........................................................................................281
Write Ruby code on a desert island .................................................281
How to be multilingual.......................................................................281
Agile Development .............................................................................282
Ten Rails Sites...............................................................................................282
Straight from the source’s mouth.....................................................282
Find a Web host ..................................................................................282
Get hooked on RadRails ....................................................................283
Documentation ...................................................................................283
Discuss Ruby on Rails........................................................................283
A Rails-friendly operating system ....................................................283
Read the latest news ..........................................................................284
Steal some code..................................................................................284
Brush up on SQL.................................................................................284
The seminal Ajax document..............................................................284
Chapter 15: Ten Features That Set Ruby Apart . . . . . . . . . . . . . . . . . . .285
Hashes ...........................................................................................................285
Open Classes ................................................................................................285
Duck Typing ..................................................................................................286
Modifiers .......................................................................................................287
Blocks ............................................................................................................287
Everything Is an Object ...............................................................................288
Objects Might Have Their Own Methods..................................................289
Mixins ............................................................................................................289
Built-In Unit Testing .....................................................................................290
Built-In Reflection.........................................................................................291
Chapter 16: Ten Pivotal Ruby on Rails Concepts . . . . . . . . . . . . . . . . .293
Don’t Repeat Yourself (DRY) ......................................................................293
Convention over Configuration..................................................................294
Model/View/Controller (MVC)....................................................................294
Agile Development .......................................................................................294
Dynamic Discovery of a Database’s Characteristics ...............................295
Object-Relational Mapping (ORM).............................................................295
Using Generators..........................................................................................296
Create, Read, Update, and Delete (CRUD) ................................................296
Using Migrations ..........................................................................................296
Using Partials................................................................................................297
Chapter 17: Ten Ways to Override Rails Defaults . . . . . . . . . . . . . . . .299
Overriding the Database Name ..................................................................300
Overriding a Database Table Name ...........................................................301
Overriding a Controller Name ....................................................................303
Overriding the Name of a Table’s Primary Key........................................304
Using Singular Nouns...................................................................................305
Creating Irregular Plurals ............................................................................307
Overriding a Default Layout........................................................................308
Creating Additional Web Pages ..................................................................310
Modifying the Meanings of URLs................................................................311
Changing the Server Environment.............................................................312
Index........................................................................315
●▬▬▬▬▬❂❂❂▬▬▬▬▬●
●▬▬❂❂▬▬●
●▬❂▬●
●❂●