【发布时间】:2014-10-09 04:00:45
【问题描述】:
我已经开始学习 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
我还在 user_params 中添加了:admin,以便可以修改: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.FILL_IN.admin?
标签: ruby-on-rails railstutorial.org