Chapter 8 - Updating user information
Introduction
We're nearly ready to start building the social networking aspects of RailsSpace, but there are a couple of loose ends to tie off before we leave the User model. In Chapter 4, we developed a registration page to create users for RailsSpace, each with a screen name, email address, and password. It's time to give our users the ability to edit (some of) these attributes.
We'll start by fleshing out the user hub (introduced in Section 4.3.4) with the basic user information. Then we'll set up a form to edit the email address and password, and write the edit action to handle the input from the form. Finally, we'll write tests for the new user information update functionality.
It's important to note that we will not allow users to change their screen names. The reason is that we would like to incorporate screen names into URLs. For example, it would be nice for the address of each RailsSpace profile to be something like
http://RailsSpace.com/profile/<screen_name>
(We'll achieve this goal in Chapter 9.) We also plan to use the screen name in the URLs for email messages (Chapter 13) and the friendship request system (Chapter 14)). Allowing users to change their screen names would break these URLs, so we've decided not to allow it.
We'll see that in the process of making the edit form and writing the new tests, we will generate a lot of duplicated code. We've already seen some of the ways Rails makes it possible to eliminate duplication; in this chapter, we'll introduce two new methods: Rails partials and the Rails test helper.
Table of Contents
- 8.1 A non-stub hub 226
- 8.2 Updating the email address 226
- 8.3 Updating password 229
- 8.3.1 Handling password submissions 233
- 8.4 Testing user edits 237
- 8.4.1 Test helpers 237
- 8.4.2 Testing the edit page 242
- 8.4.3 An advanced test 243
- 8.5 Partials 245
- 8.5.1 Two simple partials 246
- 8.5.2 A more advanced partial 247
- 8.5.3 A wrinkle, then done 249
- 8.5.4 Updating login and register 250
Source Code
- Listing 8.1 app/controllers/user_controller.rb
- Listing 8.2 app/views/user/index.rhtml
- Listing 8.3 app/views/user/edit.rhtml
- Listing 8.4 app/controllers/user_controller.rb
- Listing 8.5 app/controllers/user_controller.rb
- Listing 8.6 app/views/user/edit.rhtml
- Listing 8.7 app/models/user.rb
- Listing 8.8 app/models/user.rb
- Listing 8.9 app/controllers/user_controller.rb
- Listing 8.10 app/views/user/edit.rhtml
- Listing 8.11 app/controllers/user_controller.rb
- Listing 8.12 app/models/user.rb
- Listing 8.13 app/models/user.rb
- Listing 8.14 test/test_helper.rb
- Listing 8.15 test/test_helper.rb
- Listing 8.16 test/functional/user_controller_test.rb
- Listing 8.17 test/functional/user_controller_test.rb
- Listing 8.18 test/functional/user_controller_test.rb
- Listing 8.19 test/functional/user_controller_test.rb
- Listing 8.20 test/unit/user_test.rb
- Listing 8.21 test/test_helper.rb
- Listing 8.22 test/unit/user_test.rb
- Listing 8.23 app/views/user/_email_field_row.rhtml
- Listing 8.24 app/views/user/_screen_name_field_row.rhtml
- Listing 8.25 app/views/user/login.rhtml
- Listing 8.26 app/views/user/edit.rhtml
- Listing 8.27 app/views/user/_password_field_row.rhtml
- Listing 8.28 app/views/user/edit.rhtml
- Listing 8.29 app/views/user/_password_field_row.rhtml
- Listing 8.30 app/views/user/login.rhtml
- Listing 8.31 app/views/user/register.rhtml
- Listing 8.32 test/functional/user_controller_test.rb
Errata
As of the first printing, these are the known corrections:
- p. 244. Listing 8.21
def assert_length(boundary, object, attribute, length options = {})is missing a comma. It should bedef assert_length(boundary, object, attribute, length, options = {}) - p. 244, footnote. Cross reference should be "7.1.3" instead of "7.13".