ruby-on-rails - Railstutorial.org Michael Hartl 第 9 章练习 3 "should not allow the admin attribute to be edited via the web"

标签 ruby-on-rails railstutorial.org

我已经开始学习 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/

相关文章:

html - 如何在 Rails 中单击按钮打开新选项卡?

ruby-on-rails-3 - ruby on Rails signup_path 不可用 - 遵循 ruby​​ on Rails 教程

ruby-on-rails - Ruby on Rails 测试失败或成功取决于测试套件的调用方式

railstutorial.org - RailsTutorial: NoMethodError 'permanent' Rake::Test::CookieJar

ruby-on-rails-4 - 未定义方法 `[]' 为 nil :NilClass railstutorial. org 8.20 >

ruby-on-rails - 您对 Web 应用程序文件位置的约定是什么?

javascript仅适用于页面重新加载(安装了rails、turbolinks)

ruby-on-rails - Rails 4 嵌套表单 has_many 通过关联和数据库

ruby-on-rails - 启动Rails服务器时出错:未定义的方法 'configure'

ruby-on-rails - 如何从 Rails 中的 'first' 或 'last' 记录中获取属性值?