About This E-Book
EPUB is an open, industry-standard format for e-books. However, support for EPUB and its many features varies across reading devices and applications. Use your device or app settings to customize the presentation to your liking. Settings that you can customize often include font, font size, single or double column, landscape or portrait mode, and figures that you can click or tap to enlarge. For additional information about the settings and features on your reading device or app, visit the device manufacturers Web site.
Many titles include programming code or configuration examples. To optimize the presentation of these elements, view the e-book in single-column, landscape mode and adjust the font size to the smallest setting. In addition to presenting code and configurations in the reflowable text format, we have included images of the code that mimic the presentation found in the print book; therefore, where the reflowable format may compromise the presentation of the code listing, you will see a Click here to view code image link. Click the link to view the print-fidelity code image. To return to the previous page viewed, click the Back button on your device or app.
Ruby on Rails Tutorial
Learn Web Development with Rails
Fourth Edition
Michael Hartl
Boston Columbus Indianapolis New York San Francisco Amsterdam Cape Town
Dubai London Madrid Milan Munich Paris Montreal Toronto Delhi Mexico City
So Paulo Sydney Hong Kong Seoul Singapore Taipei Tokyo
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals.
The author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein.
For information about buying this title in bulk quantities, or for special sales opportunities (which may include electronic versions; custom cover designs; and content particular to your business, training goals, marketing focus, or branding interests), please contact our corporate sales department at or (800) 3823419.
For government sales inquiries, please contact .
For questions about sales outside the United States, please contact .
Visit us on the web: informit.com/aw
Library of Congress Control Number: 2016950287
Copyright 2017 Michael Hartl
All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permissions, request forms and the appropriate contacts within the Pearson Education Global Rights & Permissions Department, please visit www.pearsoned.com/permissions/.
The source code in Ruby on RailsTutorial, Fourth Edition, is released under the MIT License.
ISBN 13: 978-0-13-459862-8
ISBN 10: 0-13-459862-8
Text printed in the United States of America.
1 16
Editor-in-Chief
Mark L. Taub
Acquisitions Editor
Debra Williams Cauley
Managing Editor
Sandra Schroeder
Full-Service Production Manager
Julie B. Nahil
Project Editor
Rachel Paul
Copy Editor
Chris Zahn
Indexer
Richard Evans
Proofreader
Rachel Paul
Assistant Editor
Kim Boedigheimer
Cover Designer
Chuti Prasertsith
Compositor
diacriTech
Praise for Michael Hartls Books and Videos on Ruby on Rails
My former company (CD Baby) was one of the first to loudly switch to Ruby on Rails, and then even more loudly switch back to PHP. (Google me to read about the drama.) This book by Michael Hartl came so highly recommended that I had to try it, and the Ruby on RailsTutorial is what I used to switch back to Rails again.
From the Foreword by Derek Sivers (sivers.org)
Formerly: founder of CD Baby
Currently: founder of Thoughts Ltd.
Michael Hartls Rails Tutorial book is the #1 (and only, in my opinion) place to start when it comes to books about learning Rails.... Its an amazing piece of work and, unusually, walks you through building a Rails app from start to finish with testing. If you want to read just one book and feel like a Rails master by the end of it, pick the Ruby on RailsTutorial.
Peter Cooper, editor, Ruby Inside
For the self-motivated reader who responds well to the learn by doing method and is prepared to put in the effort, this comes highly recommended.
Ian Elliot, reviewer, I Programmer
Ruby on RailsTutorial is a lot of work, but if youre careful and patient, youll learn a lot.
Jason Shen, tech entrepreneur, blogger at The Art of Ass-Kicking
Michael Hartls Ruby on RailsTutorial seamlessly taught me about not only Ruby on Rails, but also the underlying Ruby language, HTML, CSS, a bit of JavaScript, and even some SQLbut most importantly it showed me how to build a web application (Twitter) in a short amount of time.
Mattan Griffel, co-founder & CEO of One Month
Although Im a Python/Django developer by trade, I cant stress enough how much this book has helped me. As an undergraduate, completely detached from industry, this book showed me how to use version control, how to write tests, and, most importantly despite the steep learning curve for setting up and getting stuff runninghow the end result of perseverance is extremely gratifying. It made me fall in love with technology all over again. This is the book I direct all my friends to who want to start learning programming/building stuff. Thank you Michael!
Prakhar Srivastav, software engineer, Xcite.com, Kuwait
It doesnt matter what you think you will be developing with in the future or what the framework du jour is; if you want to learn how to build something, there is no better place to start than with this tutorial. And for all the non-technical people out there who want to see their ideas come to life, who are considering hiring contractors, paying for a class, or founder dating in the search for a technical co-founder: stop. Take a step back. Forget about your idea for a short while and immerse yourself in this tutorial to learn what it takes to put something together. You and your software-related projects will be better for it.
Vincent C., entrepreneur and developer
It has to be the best-written book of its type Ive ever seen, and I cant recommend it enough.
Daniel Hollands, administrator of Birmingham.IO
For those wanting to learn Ruby on Rails, Hartls Ruby on RailsTutorial is (in my opinion) the best way to do it.
David Young, software developer and author at deepinthecode.com
This is a great tutorial for a lot of reasons, because aside from just teaching Rails, Hartl is also teaching good development practices.
Michael Denomy, full-stack web developer
Without a doubt, the best way I learned Ruby on Rails was by building an actual working app. I used Michael Hartls