- 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
- Rails Exposed
- 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
- The application needs to store
more information now
- A migration is just a Ruby script
- Rails can generate migrations
- You need to run your migration with rake
- But changing the database isn't enough
- Scaffolding does WAY too much
- Let’s start by generating the
- ... 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
- 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?
- But what about the new static
content MeBay sent over?
- People want to post new ads online
- 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
- 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,
- 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
- 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
- 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?
- There’s a finder for every attribute
- We need to match either the client name
OR the trainer name
- Finders write database queries
- We need to be able to modify the
conditions used in the SQL query
- Use :conditions to supply SQL
- 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
- The controller needs to know if there
was an error
- We still need to display error messages!
- The MeBay system is looking pretty sweet
- Coconut Airways need a booking system
- We need to see flights and seat
- 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
- 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
- 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
- Ajax Exposed
- Someone’s having trouble with their
- The form needs to make an Ajax request
- The form needs to be under the
- 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
- So what does Rails generate?
- If you don’t say where to put the response,
it will be executed
- 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!
- 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
- How do we PROVE an incident was saved?
- The form needs to update the contents of
the pop-up’s <div>
- How things works now
- We could have an “Edit” link in the pop-up
- We’ll start by modifying the “edit”
- And we’ll also need a new link on the
- 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!
- Look! It’s a big Ruby “Try this” page
- Web apps need testing too
- So what kinds of tests are available?
- So how do you change the database?
- The web application that went astray
- Living on the Edge
- Getting more information
- A little light reading
- Head First books on related topics