简体   繁体   English

Rails教程:NoMethodError:nil:NilClass的未定义方法“ each”

[英]Rails Tutorial: NoMethodError: undefined method `each' for nil:NilClass

I'm incredibly new to ruby on rails, and very likely missing something easy. 我是红宝石的新手,很容易错过一些容易的事情。

I've been following through Michael Hartl's tutorial book. 我一直在关注Michael Hartl的教程。 Everything was fine last night when I stopped, but this morning I am hitting my head on a wall trying to figure out this error. 昨晚我停下来时一切都很好,但今天早上我正撞在墙上试图找出此错误。

I'm on Chapter 8: the test is listing 8.25. 我在第8章:测试列出了8.25。

I thought I may have just made a typo, so I went back to where I started this morning and just copied the code straight from the tutorial. 我以为我可能刚刚打过错,所以我回到了今天早上开始的地方,只是直接从教程中复制了代码。 Still, when I run the test I get 不过,当我运行测试时,我得到

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

That was never a problem before, so that's why I'm hitting my head on the wall. 以前从来都不是问题,所以这就是为什么我要撞墙了。

After those errors, I'm getting 这些错误之后,我得到

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

Static pages controller test 静态页面控制器测试

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

Test Helper 测试助手

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

Here's the fixture 这是治

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

Sessions Controller 会话控制器

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

Sessions Helper 会话助手

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

User Model 用户模型

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

User Controller 用户控制器

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

  def new
    @user = User.new
  end

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

  private

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

Any help would be GREATLY appreciated! 任何帮助将不胜感激! Let me know if there's anything else I need to post. 让我知道是否还有其他需要发布的信息。

Use it indented (fixture): 使用它缩进(夹具):

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

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

相关问题 Rails 4:NoMethodError:nil的未定义方法`each&#39;:NilClass - Rails 4: NoMethodError: undefined method `each' for nil:NilClass Rails 3教程10.4.2:NoMethodError未定义方法`admin?&#39; 为零:NilClass - Rails 3 Tutorial 10.4.2 : NoMethodError undefined method `admin?' for nil:NilClass NoMethodError:用户创建轨道上nil:NilClass的未定义方法“ each” - NoMethodError: undefined method `each' for nil:NilClass on user creation rails Rails 3-NoMethodError:每个迭代中nil:NilClass的未定义方法 - Rails 3 - NoMethodError: undefined method for nil:NilClass in each Iteration Rails控制台错误:nil的未定义方法`each&#39;:NilClass(NoMethodError) - Rails console error: undefined method `each' for nil:NilClass (NoMethodError) Rails-NoMethodError:Model.find_by上nil:NilClass的未定义方法“ each” - Rails - NoMethodError: undefined method `each' for nil:NilClass on Model.find_by NoMethodError(nil:NilClass 的未定义方法 `each&#39;): - NoMethodError (undefined method `each' for nil:NilClass): NoMethodError:nil:NilClass的未定义方法“ each” - NoMethodError : undefined method `each' for nil:NilClass NoMethodError:nil的未定义方法&#39;type&#39;:Rails中的NilClass - NoMethodError: Undefined method 'type' for nil:NilClass in Rails rails NoMethodError:nil:NilClass的未定义方法“” - rails NoMethodError: undefined method “ ” for nil:NilClass
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM