我已经开始学习 Rails,但我卡在了第 9 章的第三个练习中。
练习 看起来像这样:
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
我的问题是
last Fill_IN >> assert_not @other_user.FILL_IN.admin
?@other_user
取自 Fixture,如下所示:archer:
name: Sterling Archer
email: duchess@example.gov
password_digest: <%= User.digest('password') %>
Update action
看起来像这样: def update
@user = User.find(params[:id])
if @user.update_attributes(user_params)
flash[:success] = "Profile updated"
redirect_to @user
else
render 'edit'
end
end
我还加了
:admin
到 user_params 所以 :admin param
可以修改:def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation, :admin)
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: @other_user.password,
password_confirmation: @other_user.password_confirmation,
admin: true }
assert_not @other_user.admin?
end
但看起来@other_user 没有被修改,所以我认为这个错误在最后一个断言中。
我的回答是错误的,我不能让这个测试失败,这是因为在最后的断言中
"assert_not @other_user.FILL_IN.admin?"
我不知道在 FILL_IN 部分放什么。我试图切断 FILL_IN 但这不起作用。
最佳答案
在对基础记录进行更改后,您必须重新加载实例变量。这将加载新的更改。
assert_not @other_user.reload.admin?
关于ruby-on-rails - Railstutorial.org Michael Hartl 第 9 章练习 3 "should not allow the admin attribute to be edited via the web",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26270102/