Chapter 9 - Personal profiles

Introduction

In the first part of this book, we gave RailsSpace users the ability to register, login, and edit their basic information. The rest of the book will be dedicated to building a social network on this foundation. We'll start off in this chapter by creating a basic profile page consisting of a user "specification" (or spec) and a personalized list of answers to Frequently Asked Questions (the user FAQ). Future chapters will add search capabilities, a simple email interface, a friendship system, and several upgrades to the user profile.

In the process of building the machinery to create, edit, and display user profiles, a lot of our previous work will come together. We'll have occasion to make several new controllers, which should help to clarify exactly what a controller is (or should be) (see the sidebar "Controlling our controllers"). We'll also finally create a second model (and a third), which will give us the opportunity to show how to use Rails to stitch data models together into one coherent whole. This chapter will also deepen our understanding of partials, especially when used for presenting collections of data.

Table of Contents

  • 9.1 A user profile stub 256
    • 9.1.1 Profile URLs 256
    • 9.1.2 Profile controller and actions 258
  • 9.2 User specs 260
    • 9.2.1 Generating the Spec model 260
    • 9.2.2 The Spec model 262
    • 9.2.3 Tying models together 264
  • 9.3 Editing the user specs 266
    • 9.3.1 Spec controller 266
    • 9.3.2 An HTML utility 268
    • 9.3.3 The spec edit view 270
    • 9.3.4 Protecting specs 272
    • 9.3.5 Testing specs 273
  • 9.4 Updating the user hub 277
    • 9.4.1 The new hub view 277
    • 9.4.2 A Spec box 280
    • 9.4.3 Named routes and the profile URL 282
    • 9.4.4 The hub main content 285
  • 9.5 Personal FAQ: Interests and personality 288
    • 9.5.1 The FAQ model 288
    • 9.5.2 The FAQ controller 292
    • 9.5.3 Editing the FAQ 293
    • 9.5.4 Adding the FAQ to the hub 294
    • 9.5.5 FAQ tests 298
  • 9.6 Public-facing profile 299

Source Code

Errata

As of the first printing, these are the known corrections:

  1. p. 268, last line. <%= text_field_for form, "Last name" %> should be
    <%= text_field_for form, "last_name" %>
  2. p. 276. Listing 9.20 is a snippet of a file, not the entire file, and Spec.new should take an argument setting its user id:
        # Test a saving a blank spec.
        def test_blank
          blank = Spec.new(:user_id => @valid_spec.user.id)
          assert blank.save, blank.errors.full_messages.join("\n")
        end
    To get this to work, you need to add the users fixture to spec_test.rb:
    class SpecTest < Test::Unit::TestCase
      fixtures :specs, :users
      .
      .
      .
    The code listed here on the web site is correct.
  3. p. 283. Listing 9.28 should should really be around the code at the bottom of the page, i.e., first we recall the old code, which should not be called listing 9.28, and then we replace it with new code that is changed and should be listing 9.28. The code listing on the web site is correct.
  4. p. 284. The user controller listing at the bottom of the page should have been numbered, and the code is included here as Listing 9.29.5.
  5. p. 289. Listing 9.34 should be titled db/migrate/006_create_faqs.rb.
  6. p. 296. We refer to Figure 9.9 on the page, but the reader should be aware that Figure 9.9 won't display until the creation of the _sidebar_box.rhtml file in Listing 9.47 on pages 297-298.
  7. p. 301. Listing 9.52 should be titled app/views/profile/show.rhtml