Learning Rails 5
by J. Mark Locklear and Eric J. Gruber
Copyright 2016 Mark Locklear, Simon St.Laurent, Edd Dumbill, Eric Gruber. All rights reserved.
Printed in the United States of America.
Published by OReilly Media, Inc. , 1005 Gravenstein Highway North, Sebastopol, CA 95472.
OReilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://oreilly.com/safari). For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com .
- Editor: Allyson MacDonald
- Production Editor: Nicole Shelby
- Copyeditor: Rachel Monaghan
- Proofreader: Charles Roumeliotis
- Indexer: Judy McConville
- Interior Designer: David Futato
- Cover Designer: Randy Comer
- Illustrator: Rebecca Demarest
- August 2016: First Edition
Revision History for the First Edition
- 2016-08-09: First Release
- 2017-02-24: Second Release
See http://oreilly.com/catalog/errata.csp?isbn=9781491926192 for release details.
The OReilly logo is a registered trademark of OReilly Media, Inc. Learning Rails 5, the cover image, and related trade dress are trademarks of OReilly Media, Inc.
While the publisher and the authors have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the authors disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights.
978-1-491-92619-2
[LSI]
Foreword
I came to Rails in 2010. At the time I was doing contract work as a QA engineer in Raleigh, NC. Previous to this I was an out of work software tester. After following my wife to Asheville, NC for a university teaching position I decided I wanted to move from software testing to development work. Asheville is a small market and finding work was tough, so Raleigh was one of my only options. At the time Rails was at version 2.x. Though I was only testing, not developing Rails apps, the learning curve was steep. The process of putting all the pieces of a Rails App in place (GIT, Bundler, Gems, Gemfiles, RVM) was daunting to say the least. Drinking from a fire hose was the term we used. I was living and working in Raleigh during the week, and going home to be with my wife on the weekend. This living arrangement gave me lots of time to explore this new framework. I wrote test scripts during the day, and spent my evenings working through Rails tutorials. I owe a debt of gratitude to many of the engineers I worked with on the Entitlement Services team at Red Hat. Specifically, Brenton Leanhardt for his patience and mentoring in learning Ruby and Linux. At the time Rails was only about 5 years old and there were still questions of whether or not Rails was enterprise ready. Those questions have since fallen by the wayside, and Rails is now considered a full-fledged enterprise development platform. Rails is no longer a second-class citizen to Java or PHP, and is arguably at the top of the web framework pecking order.
I began teaching Rails in the Fall of 2014 at Asheville-Buncombe Technical College. By far the biggest hurdle I find my students are faced with in getting started with Rails is setting up the Rails development environment. To begin with Rails is not very Windows friendly. Unless you own a Mac or are a Linux guru you are already a second-class citizen in the Rails world (see ). But, be encouraged. Microsoft is taking steps to make the Rails experience better. That being said, if you want the full Rails development experience you must still own a Mac or use some flavor of Linux. After this, your challenges are not over. Generally, you want to install some kind of version control for Ruby (RVM or RBENV), along with any number of system level packages for Rails. I say all this not to scare you, but to prepare you. Getting your development environment set up can be daunting, but I promise you, it will be worth it. Think of it as a right of passage for Rails development. Once your environment is properly set up you seldom have to revisit it in any sophisticated way, or if you do, you can usually correct whatever problem you are having pretty quickly.
Finally, I want to encourage you to learn Rails, and assure you that you will benefit from this book even if you do not go on to become a Rails developer. I can say this with confidence because Rails has become the gold standard for Model-View-Controller (MVC) architecture in web development. In addition to the general MVC structure, Rails has also created a workflow around migrations, dev ops tasks, REST, GIT, and community support. For all these reasons, even if you do not go on to do great things in Rails, you will encounter some if not all of these challenges in whatever web development platform you choose to work in. By exposing yourself to Rails and its solutions to these challenges you will learn best practices that can be applied to other frameworks and workflows.
J. Mark Locklear
Preface
Let me tell you why youre here. Youre here because you know something. What you know you cant explain, but you feel it. Youve felt it your entire life: that theres something wrong in the world. You dont know what it is, but its there. Like a splinter in your mind, driving you mad. It is this feeling that brought you to me.
Morpheus, The Matrix
What is wrong in the world? Web development. It is way harder than it has to be.
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 the power of Rails 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 the many interlinked components of Rails.
Note
This updated version of Learning Rails