Learning Rails 3
Simon St. Laurent
Edd Dumbill
Eric J Gruber
Beijing Cambridge Farnham Kln Sebastopol Tokyo
Special Upgrade Offer
If you purchased this ebook directly from oreilly.com, you have the following benefits:
DRM-free ebooksuse your ebooks across devices without restrictions or limitations
Multiple formatsuse on your laptop, tablet, or phone
Lifetime access, with free updates
Dropbox syncingyour files, anywhere
If you purchased this ebook from another retailer, you can upgrade your ebook to take advantage of all these benefits for just $4.99. to access your ebook upgrade.
Please note that upgrade offers are not available from sample content.
A Note Regarding Supplemental Files
Supplemental files and examples for this book can be found at http://examples.oreilly.com/0636920021322/. Please use a standard desktop web browser to access these files, as they may not be accessible from all ereader devices.
All code files or examples referenced in the book will be available online. For physical books that ship with an accompanying disc, whenever possible, weve posted all CD/DVD content. Note that while we provide as much of the media content as we are able via free download, we are sometimes limited by licensing restrictions. Please direct any questions or concerns to .
Preface
Everyone cool seems to agree: Ruby on Rails is an amazing way to build web applications. Ruby is a powerful and flexible programming language, and Rails takes advantage of that flexibility to build a web application framework that takes care of a tremendous amount of work for the developer. Everything sounds great!
Except, well all the Ruby on Rails books talk about this Model-View-Controller thing, and they start deep inside the application, close to the database, most of the time. From an experienced Rails developers perspective, this makes sensethe frameworks power lies largely in making it easy for developers to create a data model quickly, layer controller logic on top of that, and then, once all the hard work is done, put a thin layer of interface view on the very top. Its good programming style, and it makes for more robust applications. Advanced Ajax functionality seems to come almost for free!
From the point of view of someone learning Ruby on Rails, however, that race to show off Rails power can be extremely painful. Theres a lot of seemingly magical behavior in Rails that works wonderfullyuntil one of the incantations isnt quite right and figuring out what happened means unraveling all that work Rails did. Rails certainly makes it easier to work with databases and objects without spending forever thinking about them, but there are a lot of things to figure out before that ease becomes obvious.
If youd rather learn Ruby on Rails more slowly, starting from pieces that are more familiar to the average web developer and then moving slowly into controllers and models, youre in the right place. You can start from the HTML you already likely know, and then move more deeply into Rails many interlinked components.
Note
This updated version of Learning Rails covers version 3.2. There are substantial changes from earlier versions. Rails itself keeps changing, even in ways that affect beginners.
Who This Book Is For
Youve probably been working with the Web for long enough to know that writing web applications always seems more complicated than it should be. There are lots of parts to manage, along with lots of people to manage, and hopefully lots of visitors to please. Ruby on Rails has intrigued you as one possible solution to that situation.
You may be a designer whos moving toward application development or a developer who combines some design skills with some programming skills. You may be a programmer whos familiar with HTML but who lacks the sense of grace needed to create beautiful designthats a fair description of one of the authors of this book, anyway. Wherever youre from, whatever you do, you know the Web well and would like to learn how Rails can make your life easier.
The only mandatory technical prerequisite for reading this book is direct familiarity with HTML and a general sense of how programming works. Youll be inserting Ruby code into that HTML as a first step toward writing Ruby code directly, so understanding HTML is a key foundation. (If you dont know Ruby at all, you probably want to look over or at least keep it handy for reference.)
Cascading Style Sheets (CSS) will help you make that HTML look a lot nicer, but its not necessary for this book. Similarly, a sense of how JavaScript works may help. Experience with other templating languages (like PHP, ASP, and ASP.NET) can also help, but it isnt required.
You also need to be willing to work from the command line sometimes. The commands arent terribly complicated, but they arent (yet) completely hidden behind a graphical interface.
Who This Book Is Not For
We dont really want to cut anyone out of the possibility of reading this book, but there are some groups of people who arent likely to enjoy it. Model-View-Controller purists will probably grind their teeth through the first few chapters, and people who insist that data structures are at the heart of a good application are going to have to wait an even longer time to see their hopes realized. If you consider HTML just a nuisance that programmers have to put up with, odds are good that this book isnt for you. Most of the other Ruby on Rails books, though, are written for people who want to start from the model!
Also, people who are convinced that Ruby and Rails are the one true way may have some problems with this book, which spends a fair amount of time warning readers about potential problems and confusions they need to avoid. Yes, once youve worked with Ruby and Rails for a while, their elegance is obvious. However, reaching that level of comfort and familiarity is often a difficult road. This book attempts to ease as many of those challenges as possible by describing them clearly.
What Youll Learn
Building a Ruby on Rails application requires mastering a complicated set of skills. You may find thatdepending on how youre working with it, and who youre working withyou only need part of this tour. Thats fine. Just go as far as you think youll need.
At the beginning, youll need to install Ruby on Rails. Well explore different ways of doing this, with an emphasis on easier approaches to getting Ruby and Rails operational.
Next, well create a very simple Ruby on Rails application, with only a basic view and then a controller that does a very few things. From this foundation, well explore ways to create a more sophisticated layout using a variety of tools, learning more about Ruby along the way.
Once weve learned how to present information, well take a closer look at controllers and what they can do. Forms processing is critical to most web applications, so well build a few forms and process their results, moving from the simple to the complex.
Forms can do interesting things without storing data, but after a while its a lot more fun to have data that lasts for more than just a few moments. The next step is setting up a database to store information and figuring out how the magic of Rails ActiveRecord makes it easy to create code that maps directly to database structureswithout having to think too hard about database structures or SQL.