Rails教程:NoMethodError:未定义的方法`each'代表nil:NilClass

时间:2015-10-28 16:12:07

标签: ruby-on-rails railstutorial.org

我对铁轨上的红宝石非常新,很可能会遗漏一些东西。

我一直关注Michael Hartl的教程。我昨晚停下来的时候一切都很好,但是今天早上我在墙上试着找出这个错误。

我在第8章:测试列出了8.25。

我以为我可能刚刚打错了,所以我回到了今天早上开始的地方,直接从教程中复制了代码。不过,当我运行测试时,我得到了

100 users = 100 queries, 1000 users = 1000 queries.

之前从来没有出现问题,所以这就是为什么我在墙上撞到我的头。

在那些错误之后,我正在

1) Error:
StaticPagesControllerTest#test_should_get_about:
ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError


Error:
StaticPagesControllerTest#test_should_get_about:
NoMethodError: undefined method `each' for nil:NilClass

静态页面控制器测试

10) Error:
UserTest#test_name_should_not_be_too_long:
ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError

测试助手

require 'test_helper'

class StaticPagesControllerTest < ActionController::TestCase

def setup
  @base_title = "Ruby on Rails Tutorial Sample App"
end

test "should get home" do
  get :home
  assert_response :success
  assert_select "title", "Ruby on Rails Tutorial Sample App"
end

test "should get help" do
  get :help
  assert_response :success
  assert_select "title", "Help | #{@base_title}"
end

test "should get about" do
  get :about
  assert_response :success
  assert_select "title", "About | #{@base_title}"
end

test "should get contact" do
  get :contact
  assert_response :success
  assert_select "title", "Contact | Ruby on Rails Tutorial Sample App"
end

end

这是灯具

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

class ActiveSupport::TestCase
  # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
  fixtures :all

  # Add more helper methods to be used by all tests here...
  # Returns true if a test user is logged in.
  def is_logged_in?
    !session[:user_id].nil?
  end
end

会话控制器

michael:
name: Michael Example
email: michael@example.com
password_digest: <%= User.digest('password') %>

会话助手

class SessionsController < ApplicationController

def new
end

def create
  user = User.find_by(email: params[:session][:email].downcase)
  if user && user.authenticate(params[:session][:password])
    # Log the user in and redirect to the user's show page.
    log_in user
    redirect_to user
  else
    # Create an error message.
    flash.now[:danger] = 'invalid email/password combination' # not
    quite right        
    render 'new'
  end
end

def destroy
end

end

用户模型

module SessionsHelper

  # Logs in the given user.
  def log_in(user)
    session[:user_id] = user.id
  end

  # Returns the current logged in user (if any)
  def current_user
    @current_user ||= User.find_by(id: session[:user_id])
  end

  # Returns true if the user is logged in, false otherwise
  def logged_in?
    !current_user.nil?
  end
end

用户控制器

class User < ActiveRecord::Base
    before_save { self.email = email.downcase }
    validates :name, presence: true, length: { maximum: 50 }
    VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
    validates :email, presence: true, length: { maximum: 255 },
                      format: { with: VALID_EMAIL_REGEX },
                      uniqueness: { case_sensitive: false }

    has_secure_password
    validates :password, presence: true, length: { minimum: 6 }

    # Returns the hash digest of the given string.
      def User.digest(string)
        cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
                                                      BCrypt::Engine.cost
        BCrypt::Password.create(string, cost: cost)
      end
end

任何帮助都将 GREATLY 赞赏! 如果我需要发布任何其他内容,请告诉我。

1 个答案:

答案 0 :(得分:0)

使用缩进(夹具):

michael:
  name: Michael Example
  email: michael@example.com
  password_digest: <%= User.digest('password') %>
相关问题