好的,我正在开发一个ruby项目,我想创建一个包含可以通过数组访问的用户名和密码信息的数据库。这是我的代码。
$ncounter = 1
$databaseArray = [["",""],["",""]]
class User
attr_accessor :username
attr_accessor :password
def initialize
puts "Welcome to the site!"
end
end
def createUser(username, password)
counter = $ncounter
counter = User.new
counter.username = username
$databaseArray[ncounter - 1][0] = username
counter.password = password
$databaseArray[ncounter - 1][1] = password
ncounter = ncounter + 1
end
createUser("Charlie", "1234")
每当我运行它时,我都会在我的控制台中找到它
Welcome to the site!
user.rb:17:in `createUser': undefined local variable or method `ncounter' for
main:Object (NameError)
from user.rb:24:in `<main>'
答案 0 :(得分:0)
简短回答:您在ncounter
方法的某些方面将其称为$ncounter
而非createUser
。
长答案:您应该查看一些现有的宝石进行身份验证。如果这是一个Rails应用程序,我建议您检查devise gem。
答案 1 :(得分:-1)
检查下面的代码,应该是非反击者:
def createUser(username, password)
counter = $ncounter
counter = User.new
counter.username = username
$databaseArray[counter - 1][0] = username
counter.password = password
$databaseArray[counter - 1][1] = password
ncounter = ncounter + 1
end