- •Introduction
- •Rails Is Agile
- •Finding Your Way Around
- •Acknowledgments
- •Getting Started
- •Models, Views, and Controllers
- •Installing Rails
- •Installing on Windows
- •Installing on Mac OS X
- •Installing on Unix/Linux
- •Rails and Databases
- •Keeping Up-to-Date
- •Rails and ISPs
- •Creating a New Application
- •Hello, Rails!
- •Linking Pages Together
- •What We Just Did
- •Building an Application
- •The Depot Application
- •Incremental Development
- •What Depot Does
- •Task A: Product Maintenance
- •Iteration A1: Get Something Running
- •Iteration A2: Add a Missing Column
- •Iteration A4: Prettier Listings
- •Task B: Catalog Display
- •Iteration B1: Create the Catalog Listing
- •Iteration B2: Add Page Decorations
- •Task C: Cart Creation
- •Sessions
- •More Tables, More Models
- •Iteration C1: Creating a Cart
- •Iteration C3: Finishing the Cart
- •Task D: Checkout!
- •Iteration D2: Show Cart Contents on Checkout
- •Task E: Shipping
- •Iteration E1: Basic Shipping
- •Task F: Administrivia
- •Iteration F1: Adding Users
- •Iteration F2: Logging In
- •Iteration F3: Limiting Access
- •Finishing Up
- •More Icing on the Cake
- •Task T: Testing
- •Tests Baked Right In
- •Testing Models
- •Testing Controllers
- •Using Mock Objects
- •Test-Driven Development
- •Running Tests with Rake
- •Performance Testing
- •The Rails Framework
- •Rails in Depth
- •Directory Structure
- •Naming Conventions
- •Active Support
- •Logging in Rails
- •Debugging Hints
- •Active Record Basics
- •Tables and Classes
- •Primary Keys and IDs
- •Connecting to the Database
- •Relationships between Tables
- •Transactions
- •More Active Record
- •Acts As
- •Aggregation
- •Single Table Inheritance
- •Validation
- •Callbacks
- •Advanced Attributes
- •Miscellany
- •Action Controller and Rails
- •Context and Dependencies
- •The Basics
- •Routing Requests
- •Action Methods
- •Caching, Part One
- •The Problem with GET Requests
- •Action View
- •Templates
- •Builder templates
- •RHTML Templates
- •Helpers
- •Formatting Helpers
- •Linking to Other Pages and Resources
- •Pagination
- •Form Helpers
- •Layouts and Components
- •Adding New Templating Systems
- •Introducing AJAX
- •The Rails Way
- •Advanced Techniques
- •Action Mailer
- •Sending E-mail
- •Receiving E-mail
- •Testing E-mail
- •Web Services on Rails
- •Dispatching Modes
- •Using Alternate Dispatching
- •Method Invocation Interception
- •Testing Web Services
- •Protocol Clients
- •Securing Your Rails Application
- •SQL Injection
- •Cross-Site Scripting (CSS/XSS)
- •Avoid Session Fixation Attacks
- •Creating Records Directly from Form Parameters
- •Knowing That It Works
- •Deployment and Scaling
- •Picking a Production Platform
- •A Trinity of Environments
- •Iterating in the Wild
- •Maintenance
- •Finding and Dealing with Bottlenecks
- •Case Studies: Rails Running Daily
- •Appendices
- •Introduction to Ruby
- •Ruby Names
- •Regular Expressions
- •Source Code
- •Cross-Reference of Code Samples
- •Resources
- •Index
LINKING PAGES TOGETHER |
37 |
creates a new Time object holding the current time and tucks it away in the @time instance variable.
4.Rails looks for a template to display the result. It searchs the directory app/views for a subdirectory with the same name as the controller (say), and in that subdirectory for a file named after the action (hello.rhtml).
5.Rails processes this template through ERb, executing any embedded Ruby and substituting in values set up by the controller.
6.The result is sent back to the browser, and Rails finishes processing this request.
This isn’t the whole story—Rails gives you lots of opportunities to override this basic workflow (and we’ll be taking advantage of these shortly). As it stands, our story illustrates convention over configuration, one of the fundamental parts of the philosophy of Rails. By providing convenient defaults and by applying certain conventions, Rails applications are typically written using little or no external configuration—things just knit themselves together in a natural way.
4.3 Linking Pages Together
It’s a rare web application that has just one page. Let’s see how we can add another stunning example of web design to our Hello, World! application.
Normally, each style of page in your application will correspond to a separate view. In our case, we’ll also use a new action method to handle the page (although that isn’t always the case, as we’ll see later in the book). We’ll use the same controller for both actions. Again, this needn’t be the case, but we have no compelling reason to use a new controller right now.
We already know how to add a new view and action to a Rails application. To add the action, we define a new method in the controller. Let’s call this action goodbye. Our controller now looks like the following.
File 214 |
class |
SayController < ApplicationController |
|
def |
hello |
|
@time = Time.now |
|
|
end |
|
|
def |
goodbye |
|
end |
|
|
end |
|
Prepared exclusively for Rida Al Barazi
Report erratum
LINKING PAGES TOGETHER |
38 |
Figure 4.6: A Basic Goodbye Screen
|
Next we have to create a new template in the directory app/views/say. This |
|
time it’s called goodbye.rhtml, because by default templates are named after |
|
the associated actions. |
File 215 |
<html> |
|
<head> |
|
<title>See You Later!</title> |
|
</head> |
|
<body> |
|
<h1>Goodbye!</h1> |
|
<p> |
|
It was nice having you here. |
|
</p> |
|
</body> |
|
</html> |
|
Fire up our trusty browser again, but this time point to our new view |
|
using the URL http://localhost:3000/say/goodbye. You should see something |
|
like Figure 4.6 . |
|
Now we need to link the two screens together. We’ll put a link on the |
|
hello screen that takes us to the goodbye screen, and vice versa. In a real |
|
application we might want to make these proper buttons, but for now we’ll |
|
just use hyperlinks. |
|
We already know that Rails uses a convention to parse the URL into a |
|
target controller and an action within that controller. So a simple approach |
|
would be to adopt this URL convention for our links. The file hello.rhtml |
|
would contain the following. |
Prepared exclusively for Rida Al Barazi
Report erratum
|
LINKING PAGES TOGETHER |
39 |
|
<html> |
|
|
.... |
|
|
<p> |
|
|
Say <a href="/say/goodbye">GoodBye</a>! |
|
|
</p> |
|
|
</html> |
|
|
and the file goodbye.rhtml would point the other way. |
|
|
<html> |
|
|
.... |
|
|
<p> |
|
|
Say <a href="/say/hello">Hello</a>! |
|
|
</p> |
|
|
</html> |
|
|
This approach would certainly work, but it’s a bit fragile. If we were to |
|
|
move our application to a different place on the web server, the URLs |
|
|
would no longer be valid. It also encodes assumptions about the Rails |
|
|
URL format into our code; it’s possible a future version of Rails might |
|
|
change this. |
|
|
Fortunately, these aren’t risks we have to take. Rails comes with a bunch |
|
|
of helper methods that can be used in view templates. Here, we’ll use the |
|
|
helper method link_to( ), which creates a hyperlink to an action.5 Using |
|
|
link_to( ), hello.rhtml becomes |
|
File 217 |
<html> |
|
<head>
<title>Hello, Rails!</title>
</head> <body>
<h1>Hello from Rails!</h1>
<p>
It is now <%= @time %>.
</p> <p>
Time to say
<%= link_to "GoodBye!", :action => "goodbye" %>
</p> </body>
</html>
There’s a link_to( ) call within an ERb <%=...%> sequence. This creates a link to a URL that will invoke the goodbye( ) action. The first parameter in the call to link_to( ) is the text to be displayed in the hyperlink, and the next parameter tells Rails to generate the link to the goodbye action. As we don’t specify a controller, the current one will be used.
Let’s stop for a minute to consider that last parameter to link_to( ). We wrote
link_to "GoodBye!", :action => "goodbye"
5The link_to( ) method can do a lot more than this, but let’s take it gently for now....
Prepared exclusively for Rida Al Barazi
Report erratum
LINKING PAGES TOGETHER |
40 |
Figure 4.7: Hello Page Linked to the Goodbye Page
|
The :action part is a Ruby symbol. You can think of the colon as meaning |
|
the thing named..., so :action means the thing named action. The => "good- |
|
bye" associates the string goodbye with the name action. In effect, this |
|
gives us keyword parameters for methods. Rails makes extensive use of |
|
this facility—whenever a method takes a number of parameters and some |
|
of those parameters are optional, you can use this keyword parameter |
|
facility to give those parameters values. |
|
OK. Back to the application. If we point our browser at our hello page, it |
|
will now contain the link to the goodbye page, as shown in Figure 4.7 . |
|
We can make the corresponding change in goodbye.rhtml, linking it back to |
|
the initial hello page. |
File 216 |
<html> |
|
<head> |
|
<title>See You Later!</title> |
|
</head> |
|
<body> |
|
<h1>Goodbye!</h1> |
|
<p> |
|
It was nice having you here. |
|
</p> |
|
<p> |
|
Say <%= link_to "Hello", :action=>"hello" %> again. |
|
</p> |
|
</body> |
|
</html> |
Prepared exclusively for Rida Al Barazi
Report erratum