Head First Rails

Book description

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.

Table of contents

  1. Dedication
  2. A Note Regarding Supplemental Files
  3. Advance Praise for Head First Rails
  4. Praise for Head First Ajax
  5. Praise for other Head First books
  6. Author of Head First Rails
  7. How to use this Book: Intro
    1. Who is this book for?
      1. Who should probably back away from this book?
    2. We know what you’re thinking
    3. We know what your brain is thinking
    4. Metacognition: thinking about thinking
    5. Here’s what WE did:
    6. Here’s what YOU can do to bend your brain into submission
    7. Read Me
    8. The technical review team
    9. Acknowledgments
    10. Safari® Books Online
  8. 1. Getting Started: Really Rapid Rails
    1. Friday, 9 AM
    2. The application needs to do lots of things
    3. So what things do we need for the app?
      1. So how does Rails help us?
    4. Rails is for database-centric apps like the ticket sales system
    5. You create a new application with the rails command
      1. So what does this do?
    6. Now you need to add your own code to the default app
      1. Rails apps always follow conventions
    7. Scaffolding is GENERATED code
      1. So what does the scaffold command do?
    8. There are no tables in the database yet!
    9. Create the table by running a migration
    10. Sweet! You saved your buddy’s job!
      1. So how can we change the labels?
    11. To modify an app, you need to dig into the app’s architecture
    12. The 3 parts of your app: model, view, and controller
    13. The 3 types of code are kept in SEPARATE folders
    14. The files in the VIEW need to be edited
    15. Edit the HTML in the view
    16. Sunday, 8 AM
    17. The application needs to store more information now
    18. A migration is just a Ruby script
    19. Rails can generate migrations
    20. Give your migration a “smart” name, and Rails writes your code for you
    21. You need to run your migration with rake
      1. Timestamps tell rake which migrations to run, and in which order
    22. But changing the database isn’t enough
    23. The concert is a sell-out!
    24. Tools for your Rails Toolbox
  9. 2. Beyond Scaffolding: Rails apps, made to order
    1. MeBay, Inc. needs your help
      1. MeBay will store their ads in a database
    2. Scaffolding does WAY too much
    3. Let’s start by generating the MeBay model...
    4. ...and then we’ll actually create the table using rake
    5. But what about the controller?
      1. We’ve created the model and controller, now let’s move onto the view...
    6. The view is created with a page template
    7. The page template contains HTML
    8. A route tells Rails where your web page is
    9. Behind the scenes with routes
    10. The view doesn’t have the data to display app
    11. So what should the page show?
      1. We need to display the data in the right place
    12. The controller sends the ad to the view
    13. Rails turned the record into an object
    14. The data’s in memory, and the web page can see it
    15. There’s a problem — people can’t find the pages they want
    16. Routes run in priority order
    17. To get data into the view, you will also need code in the controller
    18. An index page will need data from ALL of the records
      1. So how do you read more than one record?
    19. Ad.find(:all) reads the whole table at once
    20. The data is returned as an object called an array
    21. An array is a numbered sequence of objects
    22. Read all of the ads with a for loop
    23. We need HTML for each element in the array
    24. Rails converts page templates into Ruby code
    25. Loops can be added to page templates using scriptlets
    26. On each pass of the loop, the page generates one link
    27. So what does the generated HTML look like?
    28. You just got an email from the folks at MeBay...
    29. But there are two page templates... should we change the code of each one?
      1. A layout defines a STANDARD look for a whole set of page templates
      2. You need to REMOVE the boilerplate from your page templates
    30. But what about the new static content MeBay sent over?
    31. Tools for your Rails Toolbox
  10. 3. Inserting, Updating, and Deleting: Everything changes
    1. People want to post new ads online
    2. You already know how to build an app that publishes data from the database
    3. Saving data works just the OPPOSITE of reading data
    4. You need a form to submit data and an action method to save it
    5. Are forms and objects related?
    6. Rails can create forms that are associated with model objects
    7. The @ad form object has not been created
    8. The form object needs to be created before the form is displayed
      1. Can you think what the problem is with that?
    9. The forms ad object will be created in the new action of the controller
    10. Each page template now has a matching controller method
    11. The form doesn’t send an object back, it sends DATA back
      1. But how is the data FORMATTED?
    12. Rails needs to convert the data into an object before it can be saved
      1. The model can create objects from raw form data
    13. The controller create method, step-by-step
    14. The controller needs to save the record
      1. Rails was complaining because it had no way of generating a RESPONSE to your request
    15. Don’t create a new page, use an existing one
    16. But how can a controller action display ANOTHER action’s page?
    17. Redirects let the controller specify which view is displayed
    18. But what if an ad needs to be amended after it’s been posted?
    19. Updating an ad is just like creating one... only different
    20. Instead of creating an ad, you need to find one; instead of saving it, you need to update the ad
    21. Restricting access to a function
    22. ...but now old ads need to be deleted
    23. Doing it yourself gave you the power to do more than scaffolding
    24. Tools for your Rails Toolbox
  11. 4. Database Finders: Truth or consequences?
    1. Keep fit with the Rubyville Health Club
      1. The scaffolding isn’t right—but do we write our own code or fix the scaffolding?
    2. The application actually looks pretty close...
    3. We’re going to fix the scaffolding
    4. Design the search function
      1. So here’s what you need to build
    5. Let’s start by building the form
      1. The search will need a new kind of form
    6. Add the search to the interface
      1. So are the form parameters structured differently?
    7. How do we find client records?
    8. We only need those records where client-name = the search string
    9. There’s a finder for every attribute
    10. So what’s next?
    11. We need to match either the client name OR the trainer name
      1. Can you see a problem here?
    12. We need to be able to modify the conditions used in the SQL query
    13. Use :conditions to supply SQL
    14. Then there’s a knock at the door...
    15. Tools for your Rails Toolbox
  12. 5. Validating your Data: Preventing mistakes
    1. Watch out—there’s bad data in the room
    2. Validation code goes in the MODEL
    3. Rails uses validators for simple validation
    4. So how do validators work?
    5. Let’s check if something is a number
    6. Users have been leaving out data on their workout forms
      1. So can validators help?
    7. How do we check for mandatory fields?
    8. Validators are simple and work well
    9. Something strange has happened at MeBay
    10. The validators work, but they don’t display errors
    11. If you build your own pages, you need to write your own error message code
    12. The controller needs to know if there was an error
    13. We still need to display error messages!
    14. The MeBay system is looking pretty sweet
    15. Tools for your Rails Toolbox
  13. 6. Making Connections: Bringing it all together
    1. Coconut Airways need a booking system
    2. We need to see flights and seat bookings together
    3. Let’s look at what the seat scaffolding gives us
    4. We need the booking form and seat list on the flight page
    5. How can we split a page’s content up into separate files?
    6. ERb will ASSEMBLE our pages
    7. So how do we create the booking form partial?
    8. Now we need to include the partial in the template
    9. We need to give the partial a seat!
    10. You can pass local variables to a partial
    11. We also need a partial for the seat list
      1. But the seat-list partial needs an array of seats
    12. People are ending up on the wrong flights
    13. A relationship connects models together
    14. But how do we define the relationship?
    15. But some people have too much baggage
    16. We need to write our OWN validation
    17. We need the REVERSE relationship
      1. So what does the Seat model look like now?
    18. The system’s taken off at Coconut Airways
    19. Tools for your Rails Toolbox
  14. 7. Ajax: Avoiding the traffic
    1. There’s a new offer at Coconut Airways
    2. Which parts of a page change most?
      1. We need a way to update just the seat list
    3. Doesn’t the browser always update the entire page?
    4. So what ELSE can make a request?
    5. First we need to include the Ajax libraries...
    6. ...then we need to add an Ajax “Refresh” link
    7. The browser needs to ask for an update
    8. But SHOULD we make the browser ask over and over again?
    9. You listen to a timer like you listen to a button
    10. Someone’s having trouble with their bachelor party
    11. The form needs to make an Ajax request
    12. The form needs to be under the CONTROL of JavaScript
    13. We need to replace the create method
    14. So what effect does this code have?
    15. There’s a problem with the flight bookings
    16. We only know how to update one part of the page at a time
      1. So what’s different this time?
    17. The controller needs to return JavaScript instead of HTML
    18. So what does Rails generate?
    19. If you don’t say where to put the response, it will be executed
      1. The completed code can will now do several things
    20. Tools for your Rails Toolbox
  15. 8. XML and Multiple Representations: It all looks different now...
    1. Climbing all over the world
      1. So why isn’t it popular?
    2. The users hate the interface!
    3. The data needs to be on a map
    4. We need to create a new action
    5. The new action seems to work...
    6. The new page needs a map... that’s the point!
    7. So what code do we need?
    8. The code will only work for localhost
    9. Now we need the map data
    10. What do we need to generate?
    11. We’ll generate XML from the model
    12. A model object can generate XML
    13. What will the controller code look like
    14. Meanwhile, at 20,000 feet...
      1. Before your latest changes
      2. After your latest changes
    15. We need to generate XML and HTML
    16. XML and HTML are just representations
    17. How should we decide which format to use?
    18. How does the map page work?
    19. The code is ready to go live
    20. RSS feeds are just XML
    21. We’ll create an action called news
    22. We have to change the structure of the XML
      1. We need more XML POWER
    23. So we’ll use a new kind of template: an XML builder
    24. Now let’s add the feed to the pages
    25. On top of the world!
    26. Tools for your Rails Toolbox
  16. 9. REST and Ajax: Taking things further
    1. Too many incidents!
    2. The map could show more details
    3. We can extend the map using Ajax
    4. But how do we convert the index page?
    5. What will the “show” action need to generate?
    6. The new map functionality is a success!
    7. We need to create requests using Ajax, too
      1. So what needs to change?
    8. The map partial lets us specify a “new” action
    9. How do we PROVE an incident was saved?
    10. The form needs to update the contents of the pop-up’s <div>
    11. Avalanche!
    12. How things works now...
    13. We could have an “Edit” link in the pop-up
    14. We’ll start by modifying the “edit” action
      1. So why have two partials?
    15. And we’ll also need a new link on the show page
    16. So how do we use the link_to helper?
    17. Ajax links to the rescue
    18. We’re using the wrong route!
    19. The HTTP method affects the route that’s chosen
    20. So what’s an HTTP method?
    21. Head First Climbers needs you!
    22. Tools for your Rails Toolbox
  17. 10. Real-World Applications: Rails in the real world
    1. Look! It’s a big Ruby “Try this” page
    2. Web apps need testing too
    3. So what kinds of tests are available?
      1. Unit tests
      2. Functional tests
      3. Integration tests
    4. Going live
      1. But how do you switch between environments?
    5. So how do you change the database?
    6. What’s REST?
      1. So what are the main principles of REST?
    7. The web application that went astray
      1. Why is not being RESTful a problem?
      2. HTTP verbs are the only verbs you need
    8. Living on the Edge
    9. Getting more information
      1. Built-in documentation
    10. A little light reading...
      1. The Ruby Way
      2. Agile Web Development with Rails
      3. Rails Cookbook
    11. Head First books on related topics
      1. Head First Ajax
      2. Head First JavaScript
      3. Head First Software Development
    12. Tools for your Rails Toolbox
    13. Leaving town...
    14. It’s been great having you here in Railsville!
  18. Index
  19. About the Author
  20. Copyright

Product information

  • Title: Head First Rails
  • Author(s): David Griffiths
  • Release date: December 2008
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9780596515775