Try this:
@user = User.first_or_create(:username => username, :email => email)
Yo can use warden with sinatra, and make your life easier. Check demo here.
Note: favor unless over if for negative conditions
Question
I am trying to create a simple sign up form using Sinatra and data mapper that signs up a user only if one already does not exist. The problem seems to be that no user is created using the code below
def signup(username, password, email)
@user = User.new(username: username, password: password, email: email)
@user.save if not User.all(:username => username, :email => email)
end
I have been looking but for the life of me cannot figure it out. Could someone tell me what I am doing wrong.
Solution
Try this:
@user = User.first_or_create(:username => username, :email => email)
Yo can use warden with sinatra, and make your life easier. Check demo here.
Note: favor unless over if for negative conditions