You are previewing Head First Rails.
O'Reilly logo
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. 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. Special Upgrade Offer
  3. A Note Regarding Supplemental Files
  4. Advance Praise for <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="emphasis"><em>Head First Rails</em></span>
  5. Praise for <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="emphasis"><em>Head First</em></span> Ajax Ajax
  6. Praise for other <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="emphasis"><em>Head First</em></span> books books
  7. Author of Head First Rails
  8. 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 <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="emphasis"><em>brain</em></span> is thinking 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
  9. 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 <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="underline">rails</span> command 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 <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="underline">migration</span>
    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 <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="underline">rake</span>
      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
  10. 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 <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="underline">WAY</span> too much 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 <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="underline">page template</span>
    7. The page template contains HTML
    8. A <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="underline">route</span> tells Rails where your web page is tells Rails where your web page is
    9. Behind the scenes with routes
    10. The view doesn’t have the data to display <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="roman">app</span>
    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 <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="underline">array</span>
    21. An array is a numbered sequence of objects
    22. Read <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="underline">all</span> of the ads with a for loop 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 <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="underline">scriptlets</span>
    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
  11. 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 <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="underline">publishes</span> data data <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="underline">from</span> the database the database
    3. Saving data works just the <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="underline">OPPOSITE</span> of reading data of reading data
    4. You need a <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="underline">form</span> to submit data and an action to submit data and an action <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="underline">method</span> to save it 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 <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="underline">before</span> the form is displayed the form is displayed
      1. Can you think what the problem is with that?
    9. The forms ad object will be created in the <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="underline">new</span> action of the controller 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 <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="underline">ANOTHER</span> action&#8217;s page? action’s page?
    17. Redirects let the controller specify which view is displayed
    18. But what if an ad needs to be amended <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="underline">after</span> it&#8217;s been posted? it’s been posted?
    19. Updating an ad is just like creating one... only different
    20. Instead of <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="underline">creating</span> an ad, you need to an ad, you need to <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="underline">find</span> one; instead of one; instead of <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="underline">saving</span> it, you need to it, you need to <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="underline">update</span> the ad the ad
    21. Restricting access to a function
    22. ...but now old ads need to be deleted
    23. Doing it <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="underline">yourself</span> gave you the power to do gave you the power to do <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="underline">more</span> than scaffolding than scaffolding
    24. Tools for your Rails Toolbox
  12. 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 <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="underline">every</span> attribute attribute
    10. So what’s next?
    11. We need to match <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="underline">either</span> the client name the client name <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="underline">OR</span> the trainer name 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
  13. 5. Validating your Data: Preventing mistakes
    1. Watch out—there’s bad data in the room
    2. Validation code goes in the <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="underline">MODEL</span>
    3. Rails uses <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="underline">validators</span> for simple validation 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 <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="underline">write</span> <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="underline">your</span> <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="underline">own</span> error message code 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
  14. 6. Making Connections: Bringing it all together
    1. Coconut Airways need a booking system
    2. We need to see flights and seat bookings <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="underline">together</span>
    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 <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="underline">ASSEMBLE</span> our pages 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 <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="underline">OWN</span> validation validation
    17. We need the <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="underline">REVERSE</span> relationship 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
  15. 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 <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="underline">always</span> update the update the <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="underline">entire</span> page? page?
    4. So what <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="underline">ELSE</span> can make a request? 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 <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="underline">CONTROL</span> of JavaScript 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
  16. 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 <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="underline">map</span>
    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 <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="underline">localhost</span>
    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 <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="underline">and</span> HTML HTML
    16. XML and HTML are just <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="underline">representations</span>
    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 <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="underline">POWER</span>
    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
  17. 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 <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="underline">Ajax</span>
    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
  18. 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!
  19. Index
  20. About the Author
  21. Special Upgrade Offer
  22. Copyright