Рубин на рельсах активная запись - PullRequest
0 голосов
/ 16 октября 2019

Для этого метода у меня проблема с циклом 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

1 Ответ

0 голосов
/ 17 октября 2019

Ваш код войдет в цикл только в том случае, если уже есть клиент с этими учетными данными. Можете ли вы подтвердить, что клиент с таким именем уже существует, и там нет ошибок проверки, как предлагает @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
...