I have been doing research on interacting with a database and storing information on a database. Currently, I am working on a project for my programming class and I am trying to create a sign up page. At this moment in my signup.html
file, the form takes two inputs: the username and the password. Then once submitted, it goes to a post method root in my app.rb
file. Now, within that app.rb
file, in the /sources
root (which is my post method root for the form in my signup.html
file) I have it so the entered credentials are saved in my database. Now I want to make it so the database crosschecks to make sure no one has the same username as the one just entered. In other words, one can not signup if the username is already taken. If the username is taken, I want a message to appear saying that the user needs to input a new desired username. If the username is not taken, I want it to redirect to home.html
file I have created. I have done research and found that the way to do this is by using the SELECT column_name FROM table_name
. But I am at a loss as to where to put this code. Does it go under my /sources
root in my app.rb
file?
Any help is greatly appreciated. Here is my code:
In my signup.html
file:
<p>Please fill out the information below to sign up.</p>
<form action = "/sources" method="post">
Username: <input type="text" name ="username" placeholder="Username"></br>
Password: <input type="password" name ="password" placeholder="Password"></br>
<input type="submit" value="Submit">
</form>
In my app.rb
file:
require 'sinatra'
require 'sequel'
require 'rubygems'
require 'simple-rss'
require 'open-uri'
DB = Sequel.connect('sqlite://test.db')
get '/chart' do
DB.create_table(:db) do
primary_key :id
String :username
String :password
end
#I realize this is not a professional way of creating the database
# but I'm not looking for how to change this at the moment
end
post '/sources' do
@username = params[:username]
@password = params[:password]
@items = DB[:db]
@items.insert(:username => @username, :password => @password)
end
get '/signup' do
redirect 'signup.html'
end
If you were doing this professionally, you'd split the app into layers. Inside the routes you'd be in the business or logic layer, and then you'd hand off data requests to the database layer. The database layer would likely be handled by and ORM (like Sequel, Datamapper, or ActiveRecord etc) and there you would query the database, handle creation of the new user etc, and then report back to the logic layer which could then take a decision as to which route and info to direct the user to.
Take a look at the Sequel documentation on models and set up a User
model that handles these things and then gives back either a User
instance or (probably) nil
, something like that. You'll want methods like self.exists?
in the model.
You really should move that create table statement out of the route, and if you're not going to use migrations (hint: you should, it's just as quick) you may want to add a DB.table_exists?
method too.
A unique constraint on the username column is probably a good idea too.