gpt4 book ai didi

ruby-on-rails - hartl rails 教程第 9 章使用电子邮件方法出错?

转载 作者:太空宇宙 更新时间:2023-11-03 17:48:32 25 4
gpt4 key购买 nike

我正在研究 Michael Hartl 的 Ruby on Rails 教程,我在第 9 章遇到了错误。

错误:

  1) Error:
UsersIndexTest#test_index_as_admin_including_pagination_and_delete_links:
NoMethodError: undefined method `email' for nil:NilClass
test/test_helper.rb:18:in `log_in_as'
test/integration/users_index_test.rb:11:in `block in <class:UsersIndexTest>'

36 runs, 79 assertions, 0 failures, 1 errors, 0 skips

文件:测试/集成/users_index_test.rb

require 'test_helper'

class UsersIndexTest < ActionDispatch::IntegrationTest

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

test "index as admin including pagination and delete links" do
log_in_as(@admin)
get users_path
assert_template 'users/index'
assert_select 'div.pagination'
first_page_of_users = User.paginate(page: 1)
first_page_of_users.each do |user|
assert_select 'a[href=?]', user_path(user), text: user.name
unless user == @admin
assert_select 'a[href=?]', user_path(user), text: 'delete',
method: :delete
end
end
assert_difference 'User.count', -1 do
delete user_path(@non_admin)
end
end

test "index as non-admin" do
log_in_as(@non_admin)
get users_path
assert_select 'a', text: 'delete', count: 0
end
end

文件:test/test_helper.rb

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'

class ActiveSupport::TestCase
fixtures :all

# Returns true if a test user is logged in.
def is_logged_in?
!session[:user_id].nil?
end

# Logs 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.id
end
end

private

# Returns true inside an integration test.
def integration_test?
defined?(post_via_redirect)
end
end

我不知道如何解决这个错误...非常感谢任何帮助!

编辑:

文件:app/controllers/user_controller.rb

class UsersController < ApplicationController
before_action :logged_in_user, only: [:index, :edit, :update, :destroy]
before_action :correct_user, only: [:edit, :update]
before_action :admin_user, only: :destroy

def index
@users = User.paginate(page: params[:page])
end

def show
@user = User.find(params[:id])
end

def new
@user = User.new
end

def create
@user = User.new(user_params)
if @user.save
log_in @user
flash[:success] = "Welcome to the Sample App!"
redirect_to @user
else
render 'new'
end
end

def edit
@user = User.find(params[:id])
end

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

def destroy
User.find(params[:id]).destroy
flash[:success] = "User deleted"
redirect_to users_url
end

private

def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation)
end

# Before filters

# Confirms a logged-in user.
def logged_in_user
unless logged_in?
store_location
flash[:danger] = "Please log in."
redirect_to login_url
end
end

# Confirms the correct user.
def correct_user
@user = User.find(params[:id])
redirect_to(root_url) unless current_user?(@user)
end

# Confirms an admin user.
def admin_user
redirect_to(root_url) unless current_user.admin?
end
end

文件:db/schema.rb

ActiveRecord::Schema.define(version: 20150418002759) do

create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
t.string "remember_digest"
t.boolean "admin", default: false
end

add_index "users", ["email"], name: "index_users_on_email", unique: true

end

最佳答案

在您的测试设置中,您需要将@user 更改为@admin:

  def setup
@admin = users(:michael)
@non_admin = users(:archer)
end

这是在其余测试中引用的实例变量名称。由于你没有设置正确,@adminnil,导致错误

关于ruby-on-rails - hartl rails 教程第 9 章使用电子邮件方法出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29711845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com