Home Page Icon
Home Page
Table of Contents for
Dedication
Close
Dedication
by David Griffiths
Head First Rails
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
Who is this book for?
Who should probably back away from this book?
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
Acknowledgments
Safari® Books Online
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?
So how does Rails help us?
Rails is for database-centric apps like the ticket sales system
You create a new application with the rails command
So what does this do?
Now you need to add your own code to the default app
Rails apps always follow conventions
Scaffolding is GENERATED code
So what does the scaffold command do?
There are no tables in the database yet!
Create the table by running a migration
Sweet! You saved your buddy’s job!
So how can we change the labels?
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
Timestamps tell rake which migrations to run, and in which order
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
MeBay will store their ads in a database
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?
We’ve created the model and controller, now let’s move onto the view...
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?
We need to display the data in the right place
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
So how do you read more than one record?
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?
A layout defines a STANDARD look for a whole set of page templates
You need to REMOVE the boilerplate from your page templates
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
Can you think what the problem is with that?
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
But how is the data FORMATTED?
Rails needs to convert the data into an object before it can be saved
The model can create objects from raw form data
The controller create method, step-by-step
The controller needs to save the record
Rails was complaining because it had no way of generating a RESPONSE to your request
Don’t create a new page, use an existing one
But how can a controller action display ANOTHER action’s page? 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? 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 scaffolding isn’t right—but do we write our own code or fix the scaffolding?
The application actually looks pretty close...
We’re going to fix the scaffolding
Design the search function
So here’s what you need to build
Let’s start by building the form
The search will need a new kind of form
Add the search to the interface
So are the form parameters structured differently?
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
Can you see a problem here?
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
So can validators help?
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
But the seat-list partial needs an array of seats
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
So what does the Seat model look like now?
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?
We need a way to update just the seat list
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
So what’s different this 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
The completed code can will now do several things
Tools for your Rails Toolbox
8. XML and Multiple Representations: It all looks different now...
Climbing all over the world
So why isn’t it popular?
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...
Before your latest changes
After your latest changes
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
We need more XML POWER
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
So what needs to change?
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
So why have two partials?
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?
Unit tests
Functional tests
Integration tests
Going live
But how do you switch between environments?
So how do you change the database?
What’s REST?
So what are the main principles of REST?
The web application that went astray
Why is not being RESTful a problem?
HTTP verbs are the only verbs you need
Living on the Edge
Getting more information
Built-in documentation
A little light reading...
The Ruby Way
Agile Web Development with Rails
Rails Cookbook
Head First books on related topics
Head First Ajax
Head First JavaScript
Head First Software Development
Tools for your Rails Toolbox
Leaving town...
It’s been great having you here in Railsville!
Index
About the Author
Copyright
Search in book...
Toggle Font Controls
Playlists
Add To
Create new playlist
Name your new playlist
Playlist description (optional)
Cancel
Create playlist
Sign In
Email address
Password
Forgot Password?
Create account
Login
or
Continue with Facebook
Continue with Google
Sign Up
Full Name
Email address
Confirm Email Address
Password
Login
Create account
or
Continue with Facebook
Continue with Google
Prev
Previous Chapter
Head First Rails
Next
Next Chapter
A Note Regarding Supplemental Files
For Dawn, and in memory of my Mother, Joan Beryl Griffiths.
Add Highlight
No Comment
..................Content has been hidden....................
You can't read the all page of ebook, please click
here
login for view all page.
Day Mode
Cloud Mode
Night Mode
Reset