Search code examples
rubysinatrasequel

Ruby with Sequel and Sinatra: form parameter passing


I am new to Ruby and Sinatra. I am attempting to pass parameters from an HTML form and insert them into a PostgreSQL database (on Heroku) with Sequel.

The connection to the database works because I have succeeded with this code block

DB = Sequel.connect(connection_ credentials)

insert_ds = DB["INSERT INTO users (email_address, username, password) VALUES ('[email protected]', 'my_username', 'my_password')"]
insert_ds.insert

This works fine, but I cannot insert data from an HTML form. The data is not being passed.

So, for example, this does not work

@email_address = params[:email_address]
@username = params[:username]
@password = params[:password]

insert_ds = DB["INSERT INTO users (email_address, username, password) VALUES (@email_address, @username', @password)"]
insert_ds.insert

The error message is

Sequel::DatabaseError at / PG::Error: ERROR: column "email_address" does not exist LINE 1: ...sers (email_address, username, password) VALUES (@email_addr... ^

which leads me to presume that the parameter was not passed

The full code is:

require 'sinatra'
require "rubygems"
require "sequel"
require 'sinatra/sequel'
require 'pg'

DB = Sequel.connect('postgres://my username:my [email protected]:5432/d70h0792oqobc')

get '/' do
  #@users = users.all :order => :id.desc
  #@title = 'All Users'
  erb :index
end

post '/' do
    @email_address = params[:email_address]
    @username = params[:username]
    @password = params[:password]
    insert_ds = DB["INSERT INTO users (email_address, username, password) VALUES (@email_address, @username, @password)"]
    insert_ds.insert
    redirect '/'
end


__END__

@@ layout
<!DOCTYPE html>
<html>
<head></head>
<body>
<%= yield %>
</body>
</html>

@@index
<section id="add">
  <form action="/" method="post">
  <label class="label"> <span>Email Address: </span> </label> <input type="text" id="email_address" name="email_address" />
  <label class="label"> <span>Username: </span> </label> <input type="text" id="username" name="username" />
  <label class="label"> <span>Password: </span> </label> <input type="password" id="password" name="password" />
    <input type="submit" value="Register me!">
  </form>
</section>

Very grateful for all help!

Thank you.


Solution

  • You are trying to interpolate the global variables @email_address, @username, and @password but haven't used the interpolation operator #

    Your SQL string apears as

    INSERT INTO users (email_address, username, password) VALUES (@email_address, @username', @password)
    

    when (apart from the stray single quote) you mean to have the values of those variables appear within the command. You should write instead

        insert_ds = DB["INSERT INTO users (email_address, username, password)
                                   VALUES (#@email_address, #@username, #@password)"]
    

    It is easy to diagnose this by adding

    puts insert_ds
    

    directly after the assignment.