简体   繁体   English

Rails教程(M. Hartl)第9章(9.2.1:需要登录的用户)错误:未定义方法`user_id'

[英]Rails Tutorial (M. Hartl) Chapter 9 (9.2.1 : Requiring logged user) Error: undefined method `user_id'

On chapter 9.2 of Hartl's Ruby on Rails Tutorial, I'm getting this 2 errors on the rake test after i made the test for redirecting user when they try to edit or update a profile that isn't theirs : 在Hartl的Ruby on Rails教程的9.2章中,我进行了重定向用户(当他们尝试编辑或更新不是他们的个人资料时)的测试后,在rake测试中遇到了这2个错误:

ERROR["test_should_redirect_edit_when_logged_in_as_wrong_user", UsersControllerTest, 2016-04-18 06:27:31 +0100]
 test_should_redirect_edit_when_logged_in_as_wrong_user#UsersControllerTest (1460957251.39s)
NoMethodError:         NoMethodError: undefined method `user_id' for #<User:0x00564114110958>
            test/test_helper.rb:24:in `log_in_as'
            test/controllers/users_controller_test.rb:28:in `block in <class:UsersControllerTest>'
        test/test_helper.rb:24:in `log_in_as'
        test/controllers/users_controller_test.rb:28:in `block in <class:UsersControllerTest>'

ERROR["test_should_redirect_update_when_logged_in_as_wrong_user", UsersControllerTest, 2016-04-18 06:27:31 +0100]
 test_should_redirect_update_when_logged_in_as_wrong_user#UsersControllerTest (1460957251.40s)
NoMethodError:         NoMethodError: undefined method `user_id' for #<User:0x00564114035628>
            test/test_helper.rb:24:in `log_in_as'
            test/controllers/users_controller_test.rb:36:in `block in <class:UsersControllerTest>'
        test/test_helper.rb:24:in `log_in_as'
        test/controllers/users_controller_test.rb:36:in `block in <class:UsersControllerTest>'

The errors messages points to line 24 in my test_helper and line 28,36 in my users_controller_test : 错误消息指向test_helper中的第24行和users_controller_test中的第28,36行:

test/test_helper.rb:24:in `log_in_as'
            test/controllers/users_controller_test.rb:28:in `block in <class:UsersControllerTest>'
        test/test_helper.rb:24:in `log_in_as'
        test/controllers/users_controller_test.rb:28:in `block in <class:UsersControllerTest>'


test/test_helper.rb:24:in `log_in_as'
            test/controllers/users_controller_test.rb:36:in `block in <class:UsersControllerTest>'
        test/test_helper.rb:24:in `log_in_as'
        test/controllers/users_controller_test.rb:36:in `block in <class:UsersControllerTest>'

test_helper.rb : test_helper.rb:

Line 18 would be at "remember_me" in the "log_in_as" method. 18号线将在“log_in_as”的方法是在“remember_me”。

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require "minitest/reporters"
Minitest::Reporters.use!

class ActiveSupport::TestCase
  # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical
  # order.
  fixtures :all
  # Returns true if a test user is logged in.
  def is_logged_in?
    !session[:user_id].nil?
  end
  # Log in a test user
  def log_in_as(user, options = {})
    password = options[:password] || 'password'
    remember_me = options[:remember_me] || '1'
    if integration_test?
        post login_path, session: {email: user.email,
                                                             password: password,
                                                             remember_me: remember_me}
    else
        session[:user_id] = user.user_id
    end
  end

  # Returns true inside an integration test.
    def integration_test?
      defined?(post_via_redirect)
    end
  # Add more helper methods to be used by all tests here...
end

user_controller_test.rb : user_controller_test.rb:

Line 28 would be at "log_in_as(@other_user)" in test "should redirect update when not logged in" and 36 at "log_in_as(@other_user)" in test "should redirect update when logged in as wrong user". 28号线将在“log_in_as(@other_user)” 测试“应该重定向更新未登录时”,并在测试 36在“log_in_as(@other_user)”“作为错误的用户登录时应该重定向更新”。

require 'test_helper'

class UsersControllerTest < ActionController::TestCase

  def setup
    @user       = users(:michael)
    @other_user = users(:archer)
  end

  test "should get new" do
    get :new
    assert_response :success
  end

  test "should redirect edit when not logged in" do
    get :edit, id: @user
    assert_not flash.empty?
    assert_redirected_to login_url
  end

  test "should redirect update when not logged in" do
    patch :update, id: @user, user: { name: @user.name, email: @user.email }
    assert_not flash.empty?
    assert_redirected_to login_url
  end

  test "should redirect edit when logged in as wrong user" do
    log_in_as(@other_user)
    get :edit, id: @user
    assert flash.empty?
    assert_redirected_to root_url
  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 flash.empty?
    assert_redirected_to root_url
  end
end

and this is the fixture file where :archer and :michael are stored(test/fixtures/users.yml): 这是在其中存储:archer和:michael的Fixture文件(test / fixtures / users.yml):

# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
michael:
  name: Michael Example
  email: michael@example.com
  password_digest: <%= User.digest('password') %>
archer:
  name: Sterling Archer
  email: duchess@example.gov
  password_digest: <%= User.digest('password') %>
else
    session[:user_id] = user.user_id
end

There's no such attribute as user_id , you want... 您没有想要的属性user_id

else
    session[:user_id] = user.id
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails教程(M. Hartl)第8章错误:未定义的局部变量 - Rails Tutorial (M. Hartl) Chapter 8 Error: Undefined local variable M. Hartl的Ruby on Rails教程。 第10.2.1章 - 未定义的局部变量或方法`micropost&#39; - Ruby on Rails Tutorial by M. Hartl. Chapter 10.2.1 - undefined local variable or method `micropost' Michael Hartl撰写的Rails教程第6章验证用户 - Rails Tutorial by Michael Hartl Chapter 6 Authenticate User hartl rails教程第9章电子邮件方法错误? - hartl rails tutorial chapter 9 error with email method? 密码M. Hartl编写的Ruby on Rails教程 - password Ruby on Rails Tutorial by M. Hartl Hartl rails教程第5章错误 - Hartl rails tutorial chapter 5 error Michael Hartl的Rails教程第12章-未定义的方法“用户” - Michael Hartl's Rails tutorial chapter 12 — undefined method `users' Rails教程(M. Hartl)第3章,在“ rails generate”之后缺少“ static_pages_controller_test.rb”文件夹? - Rails Tutorial (M. Hartl) Chapter 3, Missing `static_pages_controller_test.rb` folder after `rails generate`? Michael Hartl教程的第10章中的“未定义的局部变量或方法&#39;user&#39;” - “undefined local variable or method `user'” in Chapter 10 of Michael Hartl's tutorial Hartl的教程“找不到ID = 1的用户”(第7章) - Hartl's tutorial “Couldn't find User with id=1” (chapter 7)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM