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.

Was it helpful?

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

OTHER TIPS

I making a gem Vxod for "Social and password authorization solution" for any Rake framework, any ORM.

Currently Vxod tested on Sinatra and Mongoid.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top