【问题标题】:Railstutorial.org Michael Hartl chapter 9 exercise 3 "should not allow the admin attribute to be edited via the web"Railstutorial.org Michael Hartl 第 9 章练习 3“不应允许通过网络编辑 admin 属性”
【发布时间】: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


【解决方案1】:

对基础记录进行更改后,您必须重新加载实例变量。这将加载新的更改。

assert_not @other_user.reload.admin?

【讨论】:

    【解决方案2】:

    现在,有了第 4 版教程,使用 Rails 5.0.0,我的答案是:

    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 user_path(@other_user), params: {
                                      user: { password:              "",
                                              password_confirmation: "",
                                              admin: true } }
      assert_not @other_user.reload.admin?
    end
    

    密码和密码确认字段可以留空,恕我直言;如另一个示例所示,代码清单 10.11(在 Rails 5 版本中)。

    【讨论】:

      【解决方案3】:

      关于您的代码的另一件事——我也在学习 Hartl 教程,我认为如果您在 PATCH 请求中的密码设置为 @other_user.password(以及密码确认),那么即使user_params 中允许使用:user,您的测试也会显示为绿色,此时测试实际上应该是红色的。

      发生这种情况是因为您的users.yml 夹具文件中的:archer 没有:password 属性;他只有password_digest: &lt;%= User.digest('password') %&gt;,一旦密码经过哈希处理,您就不能像他一样取消哈希处理。

      只需将条目更改为

      { password: 'password', password_confirmation: 'password' ... }
      

      并且测试应该测试正确的东西。

      【讨论】:

        【解决方案4】:

        我遇到了和你一样的问题,根据上面的帮助,这应该是正确的答案:

          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 user_path(@other_user), params: { user: { password:                  'password',
                                                            password_confirmation:     'password',
                                                            admin:                      true } }
            assert_not @other_user.reload.admin?
          end
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-10-12
          • 1970-01-01
          • 1970-01-01
          • 2013-10-22
          • 1970-01-01
          相关资源
          最近更新 更多