所以我正在 Ruby/Sinatra 中构建一个登录/注册页面,并且我正在尝试添加一些逻辑,以便如果有人尝试使用正在使用的电子邮件进行注册,它会告诉他们,并且不允许他们注册
require 'rubygems'
require 'sinatra'
require 'mysql'
get "/" do
erb :form
end
post "/" do
begin
con = Mysql.new('localhost', 'tut', 'tut', 'recruited_users')
auth = con.query('SELECT school FROM users WHERE email = "#{params[:email]}" AND password = "#{params[:password]}"')
auth.fetch_row
ensure
con.close if con
end
end
get '/signup' do
erb :signup
end
post '/signup' do
begin
con = Mysql.new('localhost', 'tut', 'tut', 'recruited_users')
check_for_user = con.query("SELECT email FROM users WHERE email = '#{params[:email]}'")
if check_for_user == ''
"Sorry, but there is already an account for this user. The ID is '#{params[:check_for_user]}', please try again"
else
auth = con.query("INSERT INTO users (email, password, school) VALUES('#{params[:email]}', '#{params[:password]}', '#{params[:school]}')")
"Succesfully created user #{params[:email]}"
end
ensure
con.close if con
end
end
问题是该变量check_for_user
没有收到任何值,至少不是我可以使用的值。我需要能够设置 if 语句,以便他们只能在数据库中不存在电子邮件时创建新用户。