Ruby on rails designe在User.first之后返回nil
在添加了一个新的迁移之后,我正在测试一些关联,该迁移将用户关联到许多帖子Ruby on rails designe在User.first之后返回nil,ruby-on-rails,devise,Ruby On Rails,Devise,在添加了一个新的迁移之后,我正在测试一些关联,该迁移将用户关联到许多帖子 class DeviseCreateUsers < ActiveRecord::Migration def change create_table(:users) do |t| ## Customization t.string :name ## Database authenticatable t.string :email,
class DeviseCreateUsers < ActiveRecord::Migration
def change
create_table(:users) do |t|
## Customization
t.string :name
## Database authenticatable
t.string :email, null: false, default: ""
t.string :encrypted_password, null: false, default: ""
## Recoverable
t.string :reset_password_token
t.datetime :reset_password_sent_at
## Rememberable
t.datetime :remember_created_at
## Trackable
t.integer :sign_in_count, default: 0, null: false
t.datetime :current_sign_in_at
t.datetime :last_sign_in_at
t.string :current_sign_in_ip
t.string :last_sign_in_ip
## Confirmable
t.string :confirmation_token
t.datetime :confirmed_at
t.datetime :confirmation_sent_at
t.string :unconfirmed_email # Only if using reconfirmable
## Lockable
# t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
# t.string :unlock_token # Only if unlock strategy is :email or :both
# t.datetime :locked_at
t.timestamps
end
add_index :users, :email, unique: true
add_index :users, :reset_password_token, unique: true
# add_index :users, :confirmation_token, unique: true
# add_index :users, :unlock_token, unique: true
end
end
但是我得到了nil
2.1.5 :001 > u = User.first
User Load (0.1ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1
=> nil
这是件坏事吗?为什么没有显示任何内容?您需要在控制台中执行类似操作以添加第一个用户:
user = User.new(:email => 'tester@test.com', :password => 'testingpassword',
:password_confirmation => 'testingpassword')
user.save
Desive不会为您创建默认用户,因此,在您创建默认用户之前,user.first将返回为nil您需要在控制台中执行类似操作以添加第一个用户:
user = User.new(:email => 'tester@test.com', :password => 'testingpassword',
:password_confirmation => 'testingpassword')
user.save
Deviate不会为您创建默认用户,因此,在您创建默认用户之前,user.first将返回为nil您的数据库目前为空。因此你得零分。您可以这样做: 1) 使用前端的设计注册功能将新用户添加到表中 2) 在db/seeds.rb文件中添加种子数据,如下所示:
User.find_or_create_by(:email => 'tester@test.com', :password => 'password', :password_confirmation => 'password')
从命令提示符下运行rake db:seed。种子数据是要添加到数据库中以使应用程序按需运行的初始数据
3) 使用Rails控制台:在命令提示符下运行Rails c
,并执行以下操作:
User.create(:email => 'tester@test.com', :password => 'password', :password_confirmation => 'password')
您的数据库目前为空。因此你得零分。您可以这样做: 1) 使用前端的设计注册功能将新用户添加到表中 2) 在db/seeds.rb文件中添加种子数据,如下所示:
User.find_or_create_by(:email => 'tester@test.com', :password => 'password', :password_confirmation => 'password')
从命令提示符下运行rake db:seed。种子数据是要添加到数据库中以使应用程序按需运行的初始数据
3) 使用Rails控制台:在命令提示符下运行Rails c
,并执行以下操作:
User.create(:email => 'tester@test.com', :password => 'password', :password_confirmation => 'password')
在测试之前,您需要确保数据库中包含数据。在我看来,您的数据库是空的…在测试之前,您需要确保您的数据库中包含数据。在我看来你的数据库是空的。。。