对于这种方法,我在while循环中遇到问题。如果我输入的用户名已经存在,则应该再次提示我输入另一个用户名,但这是因为它正在跳过该部分并直接跳转以提示用户输入密码。
def create_account
# You can assign the 'get' method results to a var if you want
puts "Enter your full name"
get_full_name = gets.chomp
puts 'Enter your email address'
get_email = gets.chomp
puts 'Enter your desired username.'
get_username = gets.chomp
while Customer.exists?(username: get_username) do
puts "This username is already taken. Please enter a different one"
get_username = gets.chomp
break if !Customer.exists?(username: get_username)
end
puts "Please enter password"
get_password = gets.chomp
customer = Customer.create(
first_last_name: get_full_name,
email_address: get_email,
username: get_username,
password: get_password)
end
仅当已经存在具有这些凭据的客户时,您的代码才会进入循环。您可以确认已经存在具有该名称的客户,并且没有像@SujaAdiga那样的验证错误吗?
您可以使用以下方法进行测试:
def create_account
# You can assign the 'get' method results to a var if you want
puts "Enter your full name"
get_full_name = gets.chomp
puts 'Enter your email address'
get_email = gets.chomp
puts 'Enter your desired username.'
get_username = gets.chomp
## It will only enter the lop if such a customer already exists.
## Let's add one:
customer = Customer.create(
first_last_name: get_full_name,
email_address: get_email,
username: get_username,
password: "password")
if customer.errors.full_messages.empty?
puts "******successfully created customer*******"
puts customer
else
puts "*****ok we could not create the customer*****"
end
while Customer.exists?(username: get_username) do
puts "This username is already taken. Please enter a different one"
get_username = gets.chomp
break if !Customer.exists?(username: get_username)
end
puts "Please enter password"
get_password = gets.chomp
customer = Customer.create(
first_last_name: get_full_name,
email_address: get_email,
username: get_username,
password: get_password)
end
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句