Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 第10章RubyonRails期望nil不是nil失败_Ruby On Rails_Ruby_Authentication_Testing_Null - Fatal编程技术网

Ruby on rails 第10章RubyonRails期望nil不是nil失败

Ruby on rails 第10章RubyonRails期望nil不是nil失败,ruby-on-rails,ruby,authentication,testing,null,Ruby On Rails,Ruby,Authentication,Testing,Null,我在考试中遇到了一些问题,他们总是不及格,我想我有个主意,但是因为你们更有经验,我请求帮助 这就是错误: DEPRECATION WARNING: You attempted to assign a value which is not explicitly `true` or `false` to a boolean column. Currently this value casts to `false`. This will change to match Ruby's semantics

我在考试中遇到了一些问题,他们总是不及格,我想我有个主意,但是因为你们更有经验,我请求帮助

这就是错误:

DEPRECATION WARNING: You attempted to assign a value which is not explicitly `true` or `false` to a boolean column. Currently this value casts to `false`. This will change to match Ruby's semantics, and will cast to `true` in Rails 5. If you would like to maintain the current behavior, you should explicitly handle the values you would like cast to `false`. (called from create at /home/ubuntu/workspace/sample_app/app/controllers/sessions_controller.rb:9)
DEPRECATION WARNING: You attempted to assign a value which is not explicitly `true` or `false` to a boolean column. Currently this value casts to `false`. This will change to match Ruby's semantics, and will cast to `true` in Rails 5. If you would like to maintain the current behavior, you should explicitly handle the values you would like cast to `false`. (called from create at /home/ubuntu/workspace/sample_app/app/controllers/sessions_controller.rb:9)
 FAIL["test_login_with_valid_information_followed_by_logout", UsersLoginTest, 1.5503864830825478]
 test_login_with_valid_information_followed_by_logout#UsersLoginTest (1.55s)
        Failed assertion, no message given.
        test/integration/users_login_test.rb:22:in `block in <class:UsersLoginTest>'
这是必要的
会话控制器.rb

require 'test_helper'

class UsersLoginTest < ActionDispatch::IntegrationTest

    def setup
        @user = users(:michael)
    end

    test "login with invalid information" do
    get login_path
    assert_template 'sessions/new'
    post login_path, session: { email: "", password: "" }
    assert_template 'sessions/new'
    assert_not flash.empty?
    get root_path
    assert flash.empty?
    end 

  test "login with valid information followed by logout" do
    get login_path
    post login_path, session: { email: @user.email, password: 'password' }
    assert is_logged_in?
    assert_redirected_to @user
    follow_redirect!
    assert_template 'users/show'
    assert_select "a[href=?]", login_path, count: 0
    assert_select "a[href=?]", logout_path
    assert_select "a[href=?]", user_path(@user)
    delete logout_path
    assert_not is_logged_in?
    assert_redirected_to root_url
    #simula al usuario saliendo de la sesion en otra ventana
    delete logout_path
    follow_redirect!
    assert_select "a[href=?]", login_path
    assert_select "a[href=?]", logout_path,      count: 0
    assert_select "a[href=?]", user_path(@user), count: 0
    end  

    test "login with remembering" do
            log_in_as(@user, remember_me: '1')
            assert_not_nil cookies['remember_token']
    end
    test "login without remembering" do
            log_in_as(@user, remember_me: '0')
            assert_nil cookies['remember_token']
    end
end
class SessionsController < ApplicationController
  def new
  end

  def create
    user = User.find_by(email: params[:session][:email].downcase)
    if user && user.authenticate(params[:session][:password])
      # loguea al usuario y redirecciona a la pagina del usuario
      if user.activated?
      log_in user
      params[:session][:remember_me] == '1'? remember(user) : forget(user)
      redirect_back_or user
      else
      message = "Cuenta no activada"
      message += "Por favor y no te lo repito mas anda a tu mail para activar tu cuenta"
      flash[:warning] = message
      redirect_to root_url
      end
    else
      # Crea un mensaje de error
      flash.now[:danger] = "email/contraseña incorrectos"
     render 'new'
    end  
  end

  def destroy
    log_out if logged_in?
    redirect_to root_url
  end
end

我做错了什么?提前谢谢

我刚刚在User.yml中找到了答案,我写了
activated:true,
但这是正确的文件,现在一切都正常了:D

michael:
  name: Michael Example
  email: michael@example.com
  password_digest: <%= User.digest('password') %>
  admin: true
  activated: true
  activated_at: <%= Time.zone.now %>

archer:
  name: Sterling Archer
  email: duchess@example.gov
  password_digest: <%= User.digest('password') %>
  activated: true
  activated_at: <%= Time.zone.now %>

lana:
  name: Lana Kane
  email: hands@example.gov
  password_digest: <%= User.digest('password') %>
  activated: true
  activated_at: <%= Time.zone.now %>

mallory:
  name: Mallory Archer
  email: boss@example.gov
  password_digest: <%= User.digest('password') %>
  activated: true
  activated_at: <%= Time.zone.now %>

<% 30.times do |n| %>
user_<%= n %>:
  name:  <%= "User #{n}" %>
  email: <%= "user-#{n}@example.com" %>
  password_digest: <%= User.digest('password') %>
  activated: true
  activated_at: <%= Time.zone.now %>
<% end %>
迈克尔: 姓名:Michael Example 电邮:michael@example.com 密码摘要: 管理员:是的 激活:正确 在以下位置激活\u: 弓箭手: 姓名:斯特林弓箭手 电邮:duchess@example.gov 密码摘要: 激活:正确 在以下位置激活\u: 拉娜: 姓名:拉娜·凯恩 电邮:hands@example.gov 密码摘要: 激活:正确 在以下位置激活\u: 马洛里: 姓名:马洛里·阿切尔 电邮:boss@example.gov 密码摘要: 激活:正确 在以下位置激活\u: 用户: 姓名: 电邮: 密码摘要: 激活:正确 在以下位置激活\u:
让我们看看您的
登录方法及其逻辑。用户表的模式可能也会有所帮助,哪本书的第10章?RubyonRails教程第10章@sagarpandya82。我现在就上传它@Milesstanfield您是否对照github上的源代码检查了代码?
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

  # Add more helper methods to be used by all tests here...
  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
michael:
  name: Michael Example
  email: michael@example.com
  password_digest: <%= User.digest('password') %>
  admin: true
  activated: true
  activated_at: <%= Time.zone.now %>

archer:
  name: Sterling Archer
  email: duchess@example.gov
  password_digest: <%= User.digest('password') %>
  activated: true
  activated_at: <%= Time.zone.now %>

lana:
  name: Lana Kane
  email: hands@example.gov
  password_digest: <%= User.digest('password') %>
  activated: true
  activated_at: <%= Time.zone.now %>

mallory:
  name: Mallory Archer
  email: boss@example.gov
  password_digest: <%= User.digest('password') %>
  activated: true
  activated_at: <%= Time.zone.now %>

<% 30.times do |n| %>
user_<%= n %>:
  name:  <%= "User #{n}" %>
  email: <%= "user-#{n}@example.com" %>
  password_digest: <%= User.digest('password') %>
  activated: true
  activated_at: <%= Time.zone.now %>
<% end %>