Ruby on rails Rails db:seed";雷克流产了!ArgumentError:参数数目错误(2代表0..1)";
我正在做一个噩梦,试图将用户数据植入我的rails项目。我正在使用Nitrous.io,通过在控制台中键入以下内容,我可以设置一个新用户Ruby on rails Rails db:seed";雷克流产了!ArgumentError:参数数目错误(2代表0..1)";,ruby-on-rails,ruby,Ruby On Rails,Ruby,我正在做一个噩梦,试图将用户数据植入我的rails项目。我正在使用Nitrous.io,通过在控制台中键入以下内容,我可以设置一个新用户 rails console 然后在控制台中执行以下命令 2.1.1 :001 > u = User.new
rails console
然后在控制台中执行以下命令
2.1.1 :001 > u = User.new
=> #<User id: nil, username: nil, email: nil, password_hash: nil, password_salt: nil, admin: nil, points: nil, leagues: nil>
2.1.1 :002 > u.username = 'testadmin'
=> "testadmin"
2.1.1 :003 > u.email = 'testAdmin@test.com'
=> "testAdmin@test.com"
2.1.1 :004 > u.password = 'test'
=> "test"
2.1.1 :005 > u.password_confirmation = 'test'
=> "test"
2.1.1 :006 > u.admin = true
=> true
2.1.1 :007 > u.save
(86.8ms) BEGIN
User Exists (87.5ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = 'testAdmin@test.com' LIMIT 1
User Exists (86.6ms) SELECT 1 AS one FROM "users" WHERE "users"."username" = 'testadmin' LIMIT 1
SQL (91.7ms) INSERT INTO "users" ("admin", "email", "password_hash", "password_salt", "username") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["admin", "t"], ["email", "testAdmin @test.com"], ["password_hash", "$2a$10$tVMzM58wjt.lDG6dlsoLQe4obwYzLf3UfUc2ui6MD2eTGAgwTTaQK"], ["password_salt", "$2a$10$tVMzM58wjt.lDG6dlsoLQe"], ["username", "testadmin"]]
(89.3ms) COMMIT
=> true
2.1.1 :008 > Exit
User.create ({username: 'testadmin2', email: 'testAdmin2@test.com', password: 'test', password_confirmation: 'test', admin: true})
然后在控制台中运行rake db:seed,但出现以下错误:
rake aborted!
ArgumentError: wrong number of arguments (2 for 0..1)
/home/action/workspace/PAW/app/models/user.rb:39:in `initialize'
/home/action/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/inheritance.rb:30:in `new'
/home/action/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/inheritance.rb:30:in `new'
/home/action/.rvm/gems/ruby-2.1.1/gems/protected_attributes-1.0.7/lib/active_record/mass_assignment_security/persistence.rb:45:in `create'
/home/action/workspace/PAW/db/seeds.rb:8:in `<top (required)>'
/home/action/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:241:in `load'
/home/action/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:241:in `block in load'
/home/action/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:232:in `load_dependency'
/home/action/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:241:in `load'
/home/action/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/engine.rb:543:in `load_seed'
/home/action/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/tasks/database_tasks.rb:184:in `load_seed'
/home/action/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/railties/databases.rake:173:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)
rake中止!
ArgumentError:参数数目错误(2代表0..1)
/home/action/workspace/PAW/app/models/user.rb:39:in'initialize'
/home/action/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active\u record/heritation.rb:30:in“new”
/home/action/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active\u record/heritation.rb:30:in“new”
/home/action/.rvm/gems/ruby-2.1.1/gems/protected\u attributes-1.0.7/lib/active\u record/mass\u assignment\u security/persistence.rb:45:在“创建”中
/home/action/workspace/PAW/db/seeds.rb:8:in`'
/home/action/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active\u support/dependencies.rb:241:in'load'
/home/action/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active\u support/dependencies.rb:241:in'block in load'
/home/action/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active\u support/dependencies.rb:232:in'load\u dependencity'
/home/action/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active\u support/dependencies.rb:241:in'load'
/home/action/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/engine.rb:543:in'load_seed'
/home/action/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active\u record/tasks/database\u tasks.rb:184:in'load\u seed'
/home/action/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active\u record/railties/databases.rake:173:in `块(2层)in'
任务:TOP=>db:seed
(通过使用--trace运行任务查看完整跟踪)
我是ruby和rails的新手,所以我可能在做一些愚蠢的事情,但在过去的两个小时里我一直在做这些事情,我一点也不明白。我想像这样植入数据的原因是因为我想设置一些可以访问某些页面的管理员(所有这些都可以正常工作)。为此,我需要设置admin=true。如果我通过网站设置了新用户,我没有将用户设置为管理员的选项。我可以通过上面提到的控制台来做,但需要很长时间。播种会快得多,我也想知道我做错了什么。我将在下面给您一些其他代码。感谢您的关注(并希望提供帮助)
user.rb
class用户:create
验证是否存在:电子邮件,:on=>:创建
验证是否存在:username,:on=>:create
验证电子邮件的唯一性
验证用户名的唯一性
通过电子邮件(电子邮件、密码)进行自我验证
用户=通过电子邮件查找(电子邮件)
如果user&&user.password\u hash==BCrypt::Engine.hash\u secret(密码,user.password\u salt)
用户
其他的
无
结束
结束
def self.authenticate_by_username(用户名、密码)
用户=通过用户名(用户名)查找
如果user&&user.password\u hash==BCrypt::Engine.hash\u secret(密码,user.password\u salt)
用户
其他的
无
结束
结束
def加密密码
如果密码存在?
self.password\u salt=BCrypt::Engine.generate\u salt
self.password\u hash=BCrypt::Engine.hash\u secret(密码、密码)
结束
结束
def初始化(属性={})
super#必须允许活动记录初始化!
attributes.each do |名称、值|
发送(“#{name}=”,值)
结束
结束
结束
身份验证\u controller.rb
class AuthenticationController“登录”
结束
结束
def已注销
会话[:用户\u id]=无
flash[:notice]=“您已注销。”
结束
定义n
class User < ActiveRecord::Base
attr_accessible :email, :username, :password, :password_confirmation
attr_accessor :password
before_save :encrypt_password
validates_confirmation_of :password
validates_presence_of :password, :on => :create
validates_presence_of :email, :on => :create
validates_presence_of :username, :on => :create
validates_uniqueness_of :email
validates_uniqueness_of :username
def self.authenticate_by_email(email, password)
user = find_by_email(email)
if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
user
else
nil
end
end
def self.authenticate_by_username(username, password)
user = find_by_username(username)
if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
user
else
nil
end
end
def encrypt_password
if password.present?
self.password_salt = BCrypt::Engine.generate_salt
self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
end
end
def initialize(attributes = {})
super # must allow the active record to initialize!
attributes.each do |name, value|
send("#{name}=", value)
end
end
end
class AuthenticationController < ApplicationController
def sign_in
@user = User.new
end
def login
username_or_email = params[:user][:username]
password = params[:user][:password]
if username_or_email.rindex('@')
email=username_or_email
user = User.authenticate_by_email(email, password)
else
username=username_or_email
user = User.authenticate_by_username(username, password)
end
if user
session[:user_id] = user.id
flash[:notice] = 'Welcome'
redirect_to :root
else
flash.now[:error] = 'Unknown user. Please check your username and password.'
# Assign user to instance variable for the `sign_in` view!
@user = User.new(params[:user])
render :action => "sign_in"
end
end
def signed_out
session[:user_id] = nil
flash[:notice] = "You have been signed out."
end
def new_user
@user = User.new
end
def register
@user = User.new(params[:user])
if @user.valid?
@user.save
session[:user_id] = @user.id
flash[:notice] = 'Welcome.'
redirect_to :root
else
render :action => "new_user"
end
end
end
def initialize(attributes = {})
super # must allow the active record to initialize!
attributes.each do |name, value|
send("#{name}=", value)
end
end