Head First Rails: A Learner's Companion to Ruby on Rails


Ready to transport your web applications into the Web 2.0 era? Head First Rails takes your programming — and productivity — to the max. You'll learn everything from the fundamentals of Rails scaffolding to building customized interactive web apps using Rails' rich set of tools and the MVC framework. Please note this book covers Rails 2.

By the time you're finished, you'll have learned more than just another web framework. You'll master database interactions, integration with ...

See more details below
BN.com price
(Save 28%)$49.99 List Price

Pick Up In Store

Reserve and pick up in 60 minutes at your local store

Other sellers (Paperback)
  • All (18) from $1.99   
  • New (9) from $16.01   
  • Used (9) from $1.99   
Head First Rails: A learner's companion to Ruby on Rails

Available on NOOK devices and apps  
  • NOOK Devices
  • NOOK HD/HD+ Tablet
  • NOOK
  • NOOK Color
  • NOOK Tablet
  • Tablet/Phone
  • NOOK for Windows 8 Tablet
  • NOOK for iOS
  • NOOK for Android
  • NOOK Kids for iPad
  • PC/Mac
  • NOOK for Windows 8
  • NOOK for PC
  • NOOK for Mac
  • NOOK Study
  • NOOK for Web

Want a NOOK? Explore Now

NOOK Book (eBook)
BN.com price
(Save 42%)$39.99 List Price


Ready to transport your web applications into the Web 2.0 era? Head First Rails takes your programming — and productivity — to the max. You'll learn everything from the fundamentals of Rails scaffolding to building customized interactive web apps using Rails' rich set of tools and the MVC framework. Please note this book covers Rails 2.

By the time you're finished, you'll have learned more than just another web framework. You'll master database interactions, integration with Ajax and XML, rich content, and even dynamic graphing of your data — all in a fraction of the time it takes to build the same apps with Java, PHP, ASP.NET, or Perl. You'll even get comfortable and familiar with Ruby, the language that underpins Rails. But you'll do it in the context of web programming, and not through boring exercises such as "Hello, World!"

Your time is way too valuable to waste struggling with new concepts. Using the latest research in cognitive science and learning theory to craft a multi-sensory learning experience, Head First Rails uses a visually rich format designed to take advantage of the way your brain really works.

Read More Show Less

Product Details

  • ISBN-13: 9780596515775
  • Publisher: O'Reilly Media, Incorporated
  • Publication date: 12/1/2008
  • Series: Head First Series
  • Edition number: 1
  • Pages: 464
  • Sales rank: 698,839
  • Product dimensions: 8.10 (w) x 9.18 (h) x 1.08 (d)

Meet the Author

David Griffiths began programming at age 12, after being inspired by a documentary on the work of Seymour Papert. At age 15 he wrote an implementation of Papert's computer language LOGO. After studying Pure Mathematics at University, he began writing code for computers and magazine articles for humans and he is currently an agile coach with Exoftware in the UK, helping people to create simpler, more valuable software. He spends his free time traveling and time with his lovely wife, Dawn.

Read More Show Less

Table of Contents

Advance Praise for Head First Rails;
Praise for Head First Ajax;
Praise for other Head First books;
Author of Head First Rails;
How to use this Book: Intro;
Who is this book for?;
We know what you’re thinking;
We know what your brain is thinking;
Metacognition: thinking about thinking;
Here’s what WE did:;
Here’s what YOU can do to bend your brain into submission;
Read Me;
The technical review team;
Safari® Books Online;
Chapter 1: Getting Started: Really Rapid Rails;
1.1 Friday, 9 AM;
1.2 The application needs to do lots of things;
1.3 So what things do we need for the app?;
1.4 Rails is for database-centric apps like the ticket sales system;
1.5 You create a new application with the rails command;
1.6 Now you need to add your own code to the default app;
1.7 Scaffolding is GENERATED code;
1.8 There are no tables in the database yet!;
1.9 Create the table by running a migration;
1.10 Sweet! You saved your buddy’s job!;
1.11 To modify an app, you need to dig into the app’s architecture;
1.12 The 3 parts of your app: model, view, and controller;
1.13 The 3 types of code are kept in SEPARATE folders;
1.14 The files in the VIEW need to be edited;
1.15 Edit the HTML in the view;
1.16 Sunday, 8 AM;
1.17 The application needs to store more information now;
1.18 A migration is just a Ruby script;
1.19 Rails can generate migrations;
1.20 Give your migration a “smart” name, and Rails writes your code for you;
1.21 You need to run your migration with rake;
1.22 But changing the database isn’t enough;
1.23 The concert is a sell-out!;
1.24 Tools for your Rails Toolbox;
Chapter 2: Beyond Scaffolding: Rails apps, made to order;
2.1 MeBay, Inc. needs your help;
2.2 Scaffolding does WAY too much;
2.3 Let’s start by generating the MeBay model...;
2.4 ...and then we’ll actually create the table using rake;
2.5 But what about the controller?;
2.6 The view is created with a page template;
2.7 The page template contains HTML;
2.8 A route tells Rails where your web page is;
2.9 Behind the scenes with routes;
2.10 The view doesn’t have the data to display app;
2.11 So what should the page show?;
2.12 The controller sends the ad to the view;
2.13 Rails turned the record into an object;
2.14 The data’s in memory, and the web page can see it;
2.15 There’s a problem — people can’t find the pages they want;
2.16 Routes run in priority order;
2.17 To get data into the view, you will also need code in the controller;
2.18 An index page will need data from ALL of the records;
2.19 Ad.find(:all) reads the whole table at once;
2.20 The data is returned as an object called an array;
2.21 An array is a numbered sequence of objects;
2.22 Read all of the ads with a for loop;
2.23 We need HTML for each element in the array;
2.24 Rails converts page templates into Ruby code;
2.25 Loops can be added to page templates using scriptlets;
2.26 On each pass of the loop, the page generates one link;
2.27 So what does the generated HTML look like?;
2.28 You just got an email from the folks at MeBay...;
2.29 But there are two page templates... should we change the code of each one?;
2.30 But what about the new static content MeBay sent over?;
2.31 Tools for your Rails Toolbox;
Chapter 3: Inserting, Updating, and Deleting: Everything changes;
3.1 People want to post new ads online;
3.2 You already know how to build an app that publishes data from the database;
3.3 Saving data works just the OPPOSITE of reading data;
3.4 You need a form to submit data and an action method to save it;
3.5 Are forms and objects related?;
3.6 Rails can create forms that are associated with model objects;
3.7 The @ad form object has not been created;
3.8 The form object needs to be created before the form is displayed;
3.9 The forms ad object will be created in the new action of the controller;
3.10 Each page template now has a matching controller method;
3.11 The form doesn’t send an object back, it sends DATA back;
3.12 Rails needs to convert the data into an object before it can be saved;
3.13 The controller create method, step-by-step;
3.14 The controller needs to save the record;
3.15 Don’t create a new page, use an existing one;
3.16 But how can a controller action display ANOTHER action’s page?;
3.17 Redirects let the controller specify which view is displayed;
3.18 But what if an ad needs to be amended after it’s been posted?;
3.19 Updating an ad is just like creating one... only different;
3.20 Instead of creating an ad, you need to find one; instead of saving it, you need to update the ad;
3.21 Restricting access to a function;
3.22 ...but now old ads need to be deleted;
3.23 Doing it yourself gave you the power to do more than scaffolding;
3.24 Tools for your Rails Toolbox;
Chapter 4: Database Finders: Truth or consequences?;
4.1 Keep fit with the Rubyville Health Club;
4.2 The application actually looks pretty close...;
4.3 We’re going to fix the scaffolding;
4.4 Design the search function;
4.5 Let’s start by building the form;
4.6 Add the search to the interface;
4.7 How do we find client records?;
4.8 We only need those records where client-name = the search string;
4.9 There’s a finder for every attribute;
4.10 So what’s next?;
4.11 We need to match either the client name OR the trainer name;
4.12 We need to be able to modify the conditions used in the SQL query;
4.13 Use :conditions to supply SQL;
4.14 Then there’s a knock at the door...;
4.15 Tools for your Rails Toolbox;
Chapter 5: Validating your Data: Preventing mistakes;
5.1 Watch out—there’s bad data in the room;
5.2 Validation code goes in the MODEL;
5.3 Rails uses validators for simple validation;
5.4 So how do validators work?;
5.5 Let’s check if something is a number;
5.6 Users have been leaving out data on their workout forms;
5.7 How do we check for mandatory fields?;
5.8 Validators are simple and work well;
5.9 Something strange has happened at MeBay;
5.10 The validators work, but they don’t display errors;
5.11 If you build your own pages, you need to write your own error message code;
5.12 The controller needs to know if there was an error;
5.13 We still need to display error messages!;
5.14 The MeBay system is looking pretty sweet;
5.15 Tools for your Rails Toolbox;
Chapter 6: Making Connections: Bringing it all together;
6.1 Coconut Airways need a booking system;
6.2 We need to see flights and seat bookings together;
6.3 Let’s look at what the seat scaffolding gives us;
6.4 We need the booking form and seat list on the flight page;
6.5 How can we split a page’s content up into separate files?;
6.6 ERb will ASSEMBLE our pages;
6.7 So how do we create the booking form partial?;
6.8 Now we need to include the partial in the template;
6.9 We need to give the partial a seat!;
6.10 You can pass local variables to a partial;
6.11 We also need a partial for the seat list;
6.12 People are ending up on the wrong flights;
6.13 A relationship connects models together;
6.14 But how do we define the relationship?;
6.15 But some people have too much baggage;
6.16 We need to write our OWN validation;
6.17 We need the REVERSE relationship;
6.18 The system’s taken off at Coconut Airways;
6.19 Tools for your Rails Toolbox;
Chapter 7: Ajax: Avoiding the traffic;
7.1 There’s a new offer at Coconut Airways;
7.2 Which parts of a page change most?;
7.3 Doesn’t the browser always update the entire page?;
7.4 So what ELSE can make a request?;
7.5 First we need to include the Ajax libraries...;
7.6 ...then we need to add an Ajax “Refresh” link;
7.7 The browser needs to ask for an update;
7.8 But SHOULD we make the browser ask over and over again?;
7.9 You listen to a timer like you listen to a button;
7.10 Someone’s having trouble with their bachelor party;
7.11 The form needs to make an Ajax request;
7.12 The form needs to be under the CONTROL of JavaScript;
7.13 We need to replace the create method;
7.14 So what effect does this code have?;
7.15 There’s a problem with the flight bookings;
7.16 We only know how to update one part of the page at a time;
7.17 The controller needs to return JavaScript instead of HTML;
7.18 So what does Rails generate?;
7.19 If you don’t say where to put the response, it will be executed;
7.20 Tools for your Rails Toolbox;
Chapter 8: XML and Multiple Representations: It all looks different now...;
8.1 Climbing all over the world;
8.2 The users hate the interface!;
8.3 The data needs to be on a map;
8.4 We need to create a new action;
8.5 The new action seems to work...;
8.6 The new page needs a map... that’s the point!;
8.7 So what code do we need?;
8.8 The code will only work for localhost;
8.9 Now we need the map data;
8.10 What do we need to generate?;
8.11 We’ll generate XML from the model;
8.12 A model object can generate XML;
8.13 What will the controller code look like;
8.14 Meanwhile, at 20,000 feet...;
8.15 We need to generate XML and HTML;
8.16 XML and HTML are just representations;
8.17 How should we decide which format to use?;
8.18 How does the map page work?;
8.19 The code is ready to go live;
8.20 RSS feeds are just XML;
8.21 We’ll create an action called news;
8.22 We have to change the structure of the XML;
8.23 So we’ll use a new kind of template: an XML builder;
8.24 Now let’s add the feed to the pages;
8.25 On top of the world!;
8.26 Tools for your Rails Toolbox;
Chapter 9: REST and Ajax: Taking things further;
9.1 Too many incidents!;
9.2 The map could show more details;
9.3 We can extend the map using Ajax;
9.4 But how do we convert the index page?;
9.5 What will the “show” action need to generate?;
9.6 The new map functionality is a success!;
9.7 We need to create requests using Ajax, too;
9.8 The map partial lets us specify a “new” action;
9.9 How do we PROVE an incident was saved?;
9.10 The form needs to update the contents of the pop-up’s
9.11 Avalanche!;
9.12 How things works now...;
9.13 We could have an “Edit” link in the pop-up;
9.14 We’ll start by modifying the “edit” action;
9.15 And we’ll also need a new link on the show page;
9.16 So how do we use the link_to helper?;
9.17 Ajax links to the rescue;
9.18 We’re using the wrong route!;
9.19 The HTTP method affects the route that’s chosen;
9.20 So what’s an HTTP method?;
9.21 Head First Climbers needs you!;
9.22 Tools for your Rails Toolbox;
Chapter 10: Real-World Applications: Rails in the real world;
10.1 Look! It’s a big Ruby “Try this” page;
10.2 Web apps need testing too;
10.3 So what kinds of tests are available?;
10.4 Going live;
10.5 So how do you change the database?;
10.6 What’s REST?;
10.7 The web application that went astray;
10.8 Living on the Edge;
10.9 Getting more information;
10.10 A little light reading...;
10.11 Head First books on related topics;
10.12 Tools for your Rails Toolbox;
10.13 Leaving town...;
10.14 It’s been great having you here in Railsville!;

Read More Show Less

Customer Reviews

Be the first to write a review
( 0 )
Rating Distribution

5 Star


4 Star


3 Star


2 Star


1 Star


Your Rating:

Your Name: Create a Pen Name or

Barnes & Noble.com Review Rules

Our reader reviews allow you to share your comments on titles you liked, or didn't, with others. By submitting an online review, you are representing to Barnes & Noble.com that all information contained in your review is original and accurate in all respects, and that the submission of such content by you and the posting of such content by Barnes & Noble.com does not and will not violate the rights of any third party. Please follow the rules below to help ensure that your review can be posted.

Reviews by Our Customers Under the Age of 13

We highly value and respect everyone's opinion concerning the titles we offer. However, we cannot allow persons under the age of 13 to have accounts at BN.com or to post customer reviews. Please see our Terms of Use for more details.

What to exclude from your review:

Please do not write about reviews, commentary, or information posted on the product page. If you see any errors in the information on the product page, please send us an email.

Reviews should not contain any of the following:

  • - HTML tags, profanity, obscenities, vulgarities, or comments that defame anyone
  • - Time-sensitive information such as tour dates, signings, lectures, etc.
  • - Single-word reviews. Other people will read your review to discover why you liked or didn't like the title. Be descriptive.
  • - Comments focusing on the author or that may ruin the ending for others
  • - Phone numbers, addresses, URLs
  • - Pricing and availability information or alternative ordering information
  • - Advertisements or commercial solicitation


  • - By submitting a review, you grant to Barnes & Noble.com and its sublicensees the royalty-free, perpetual, irrevocable right and license to use the review in accordance with the Barnes & Noble.com Terms of Use.
  • - Barnes & Noble.com reserves the right not to post any review -- particularly those that do not follow the terms and conditions of these Rules. Barnes & Noble.com also reserves the right to remove any review at any time without notice.
  • - See Terms of Use for other conditions and disclaimers.
Search for Products You'd Like to Recommend

Recommend other products that relate to your review. Just search for them below and share!

Create a Pen Name

Your Pen Name is your unique identity on BN.com. It will appear on the reviews you write and other website activities. Your Pen Name cannot be edited, changed or deleted once submitted.

Your Pen Name can be any combination of alphanumeric characters (plus - and _), and must be at least two characters long.

Continue Anonymously
Sort by: Showing all of 4 Customer Reviews
  • Posted February 13, 2010

    Practical and refreshing approach to the basics of Rails

    While I am developing an actual application with Rails, my approach to learning has been very "pull" as needed. The one book I did use cover-to-cover to get started was "Rails Up and Running" which was very short (awesome) and showed many cool features. Awesome! However, I wanted to get some better grasp of the core parts of this framework, do some cool stuff (RSS and Mashups) and follow good patterns like HTML is content and CSS is for layout. It's in this book.

    This book is actually fun to work along with, it is not a dry tome on the theory of REST and how Rails does it. It is not a complete guide to the most awesome, super, Rails hacks. It is a book of good, solid basics... as in any task, having the basics is a must. The better you have them the better your code is.

    This is not a reference at all, it's far too "Head's First" for that. However, the various exercises, bullet points and the resulting application code can make it something to which you can refer because the information is in a context of use.

    There are some hand waving spots - and a few little bugs I had to work around in routes and such, but you can always peek at the code from the book site if you get stuck.

    So even though I have already done some multi-model forms, queries, RJS, and form animations - I did get something from this book. If you are a super-Rails genius you probably won't get anything from this book, but if you are starting or in the middle and want to get some fundamentals then consider this book.

    1 out of 1 people found this review helpful.

    Was this review helpful? Yes  No   Report this review
  • Anonymous

    Posted April 7, 2010

    No text was provided for this review.

  • Anonymous

    Posted August 22, 2010

    No text was provided for this review.

  • Anonymous

    Posted October 23, 2010

    No text was provided for this review.

Sort by: Showing all of 4 Customer Reviews

If you find inappropriate content, please report it to Barnes & Noble
Why is this product inappropriate?
Comments (optional)