9.6 练习
最后更新于:2022-04-01 22:30:00
# 9.6 练习
电子书中有练习的答案,如果想阅读参考答案,请[购买电子书](http://railstutorial-china.org/#purchase)。
避免练习和正文冲突的方法参见[3.6 节](chapter3.html#mostly-static-pages-exercises)中的说明。
1. 编写一个测试,确保友好转向只会在首次登录后转向指定的地址,以后再登录都会转向默认地址(即资料页面)。提示:把这个测试添加到[代码清单 9.26](#listing-friendly-forwarding-test) 中,检查 `session[:forwarding_url]` 中是否保存了正确的值。
2. 编写一个集成测试,测试布局中的所有链接,以及登录后和登录前应该看到哪些链接。提示:把这个测试添加到[代码清单 5.25](chapter5.html#listing-layout-links-test) 中,使用 `log_in_as` 辅助方法。
3. 参照[代码清单 9.59](#listing-forbidden-admin-test),直接向 `update` 动作发送 `PATCH` 请求,确认无法修改 `admin` 属性。为了确保测试写得正确,首先应该把 `admin` 添加到允许修改的参数列表 `user_params` 中,所以在此之前测试组件无法通过。
4. 使用[代码清单 9.60](#listing-new-edit-partial) 中的局部视图重构 `new.html.erb` 和 `edit.html.erb` 视图中的表单,重构后的代码如[代码清单 9.61](#listing-new-user-with-partial) 和[代码清单 9.62](#listing-edit-user-with-partial) 所示。注意这里使用 `provide` 方法([3.4.3 节](chapter3.html#layouts-and-embedded-ruby)用过)避免布局中有重复。[[12](#fn-12)]
##### 代码清单 9.59:测试禁止修改 `admin` 属性
test/controllers/users_controller_test.rb
```
require 'test_helper'
class UsersControllerTest < ActionController::TestCase
def setup
@user = users(:michael)
@other_user = users(:archer)
end
.
.
.
test "should redirect update when logged in as wrong user" do
log_in_as(@other_user)
patch :update, id: @user, user: { name: @user.name, email: @user.email }
assert_redirected_to root_url
end
test "should not allow the admin attribute to be edited via the web" do log_in_as(@other_user) assert_not @other_user.admin? patch :update, id: @other_user, user: { password: FILL_IN, password_confirmation: FILL_IN, admin: FILL_IN } assert_not @other_user.FILL_IN.admin? end .
.
.
end
```
##### 代码清单 9.60:`new` 和 `edit` 视图中使用的表单局部视图
app/views/users/_form.html.erb
```
<%= form_for(@user) do |f| %>
<%= render 'shared/error_messages', object: @user %>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control' %>
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
<%= f.submit yield(:button_text), class: "btn btn-primary" %>
<% end %>
```
##### 代码清单 9.61:使用局部视图的注册页面视图
app/views/users/new.html.erb
```
<% provide(:title, 'Sign up') %>
<% provide(:button_text, 'Create my account') %>
```
##### 代码清单 9.62:使用局部视图的编辑页面视图
app/views/users/edit.html.erb
```
<% provide(:title, 'Edit user') %>
<% provide(:button_text, 'Save changes') %>
```
';
Sign up
<%= render 'form' %>
Update your profile
<%= render 'form' %>
<%= gravatar_for @user %>
Change