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
- Dedication
- A Note Regarding Supplemental Files
- 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
-
1. Getting Started: Really Rapid Rails
- Friday, 9 AM
- The application needs to do lots of things
- So what things do we need for the app?
- Rails is for database-centric apps like the ticket sales system
- You create a new application with the rails command
- Now you need to add your own code to the default app
- Scaffolding is GENERATED code
- There are no tables in the database yet!
- Create the table by running a migration
- Sweet! You saved your buddy’s job!
- To modify an app, you need to dig into the app’s architecture
- The 3 parts of your app: model, view, and controller
- The 3 types of code are kept in SEPARATE folders
- The files in the VIEW need to be edited
- Edit the HTML in the view
- Sunday, 8 AM
- The application needs to store more information now
- A migration is just a Ruby script
- Rails can generate migrations
- Give your migration a “smart” name, and Rails writes your code for you
- You need to run your migration with rake
- But changing the database isn’t enough
- The concert is a sell-out!
- Tools for your Rails Toolbox
-
2. Beyond Scaffolding: Rails apps, made to order
- MeBay, Inc. needs your help
- Scaffolding does WAY too much
- Let’s start by generating the MeBay model...
- ...and then we’ll actually create the table using rake
- But what about the controller?
- The view is created with a page template
- The page template contains HTML
- A route tells Rails where your web page is
- Behind the scenes with routes
- The view doesn’t have the data to display app
- So what should the page show?
- The controller sends the ad to the view
- Rails turned the record into an object
- The data’s in memory, and the web page can see it
- There’s a problem — people can’t find the pages they want
- Routes run in priority order
- To get data into the view, you will also need code in the controller
- An index page will need data from ALL of the records
- Ad.find(:all) reads the whole table at once
- The data is returned as an object called an array
- An array is a numbered sequence of objects
- Read all of the ads with a for loop
- We need HTML for each element in the array
- Rails converts page templates into Ruby code
- Loops can be added to page templates using scriptlets
- On each pass of the loop, the page generates one link
- So what does the generated HTML look like?
- You just got an email from the folks at MeBay...
- But there are two page templates... should we change the code of each one?
- But what about the new static content MeBay sent over?
- Tools for your Rails Toolbox
-
3. Inserting, Updating, and Deleting: Everything changes
- People want to post new ads online
- You already know how to build an app that publishes data from the database
- Saving data works just the OPPOSITE of reading data
- You need a form to submit data and an action method to save it
- Are forms and objects related?
- Rails can create forms that are associated with model objects
- The @ad form object has not been created
- The form object needs to be created before the form is displayed
- The forms ad object will be created in the new action of the controller
- Each page template now has a matching controller method
- The form doesn’t send an object back, it sends DATA back
- Rails needs to convert the data into an object before it can be saved
- The controller create method, step-by-step
- The controller needs to save the record
- Don’t create a new page, use an existing one
- But how can a controller action display ANOTHER action’s page?
- Redirects let the controller specify which view is displayed
- But what if an ad needs to be amended after it’s been posted?
- Updating an ad is just like creating one... only different
- Instead of creating an ad, you need to find one; instead of saving it, you need to update the ad
- Restricting access to a function
- ...but now old ads need to be deleted
- Doing it yourself gave you the power to do more than scaffolding
- Tools for your Rails Toolbox
-
4. Database Finders: Truth or consequences?
- Keep fit with the Rubyville Health Club
- The application actually looks pretty close...
- We’re going to fix the scaffolding
- Design the search function
- Let’s start by building the form
- Add the search to the interface
- How do we find client records?
- We only need those records where client-name = the search string
- There’s a finder for every attribute
- So what’s next?
- We need to match either the client name OR the trainer name
- We need to be able to modify the conditions used in the SQL query
- Use :conditions to supply SQL
- Then there’s a knock at the door...
- Tools for your Rails Toolbox
-
5. Validating your Data: Preventing mistakes
- Watch out—there’s bad data in the room
- Validation code goes in the MODEL
- Rails uses validators for simple validation
- So how do validators work?
- Let’s check if something is a number
- Users have been leaving out data on their workout forms
- How do we check for mandatory fields?
- Validators are simple and work well
- Something strange has happened at MeBay
- The validators work, but they don’t display errors
- If you build your own pages, you need to write your own error message code
- The controller needs to know if there was an error
- We still need to display error messages!
- The MeBay system is looking pretty sweet
- Tools for your Rails Toolbox
-
6. Making Connections: Bringing it all together
- Coconut Airways need a booking system
- We need to see flights and seat bookings together
- Let’s look at what the seat scaffolding gives us
- We need the booking form and seat list on the flight page
- How can we split a page’s content up into separate files?
- ERb will ASSEMBLE our pages
- So how do we create the booking form partial?
- Now we need to include the partial in the template
- We need to give the partial a seat!
- You can pass local variables to a partial
- We also need a partial for the seat list
- People are ending up on the wrong flights
- A relationship connects models together
- But how do we define the relationship?
- But some people have too much baggage
- We need to write our OWN validation
- We need the REVERSE relationship
- The system’s taken off at Coconut Airways
- Tools for your Rails Toolbox
-
7. Ajax: Avoiding the traffic
- There’s a new offer at Coconut Airways
- Which parts of a page change most?
- Doesn’t the browser always update the entire page?
- So what ELSE can make a request?
- First we need to include the Ajax libraries...
- ...then we need to add an Ajax “Refresh” link
- The browser needs to ask for an update
- But SHOULD we make the browser ask over and over again?
- You listen to a timer like you listen to a button
- Someone’s having trouble with their bachelor party
- The form needs to make an Ajax request
- The form needs to be under the CONTROL of JavaScript
- We need to replace the create method
- So what effect does this code have?
- There’s a problem with the flight bookings
- We only know how to update one part of the page at a time
- The controller needs to return JavaScript instead of HTML
- So what does Rails generate?
- If you don’t say where to put the response, it will be executed
- Tools for your Rails Toolbox
-
8. XML and Multiple Representations: It all looks different now...
- Climbing all over the world
- The users hate the interface!
- The data needs to be on a map
- We need to create a new action
- The new action seems to work...
- The new page needs a map... that’s the point!
- So what code do we need?
- The code will only work for localhost
- Now we need the map data
- What do we need to generate?
- We’ll generate XML from the model
- A model object can generate XML
- What will the controller code look like
- Meanwhile, at 20,000 feet...
- We need to generate XML and HTML
- XML and HTML are just representations
- How should we decide which format to use?
- How does the map page work?
- The code is ready to go live
- RSS feeds are just XML
- We’ll create an action called news
- We have to change the structure of the XML
- So we’ll use a new kind of template: an XML builder
- Now let’s add the feed to the pages
- On top of the world!
- Tools for your Rails Toolbox
-
9. REST and Ajax: Taking things further
- Too many incidents!
- The map could show more details
- We can extend the map using Ajax
- But how do we convert the index page?
- What will the “show” action need to generate?
- The new map functionality is a success!
- We need to create requests using Ajax, too
- The map partial lets us specify a “new” action
- How do we PROVE an incident was saved?
- The form needs to update the contents of the pop-up’s <div>
- Avalanche!
- How things works now...
- We could have an “Edit” link in the pop-up
- We’ll start by modifying the “edit” action
- And we’ll also need a new link on the show page
- So how do we use the link_to helper?
- Ajax links to the rescue
- We’re using the wrong route!
- The HTTP method affects the route that’s chosen
- So what’s an HTTP method?
- Head First Climbers needs you!
- Tools for your Rails Toolbox
-
10. Real-World Applications: Rails in the real world
- Look! It’s a big Ruby “Try this” page
- Web apps need testing too
- So what kinds of tests are available?
- Going live
- So how do you change the database?
- What’s REST?
- The web application that went astray
- Living on the Edge
- Getting more information
- A little light reading...
- Head First books on related topics
- Tools for your Rails Toolbox
- Leaving town...
- It’s been great having you here in Railsville!
- Index
- About the Author
- Copyright
Product information
- Title: Head First Rails
- Author(s):
- Release date: December 2008
- Publisher(s): O'Reilly Media, Inc.
- ISBN: 9780596515775
You might also like
book
RESTful Rails Development
This book serves as a practical guide to developing RESTful applications, designing RESTful architectures, and deploying …
book
Learn Rails 6: Accelerated Web Development with Ruby on Rails
Effectively learn and apply software development and engineering techniques to web application development using Rails 6 …
book
Head First Ruby
What will you learn from this book? What’s all the buzz about this Ruby language? Is …
book
Learning Rails
While most books written about Rails cater to programmers looking for information on data structures, Learning …