Search code examples
rubysqlitesinatrasequel

Sequel Sinatra Post Database params


I am trying to build just a very simple form for registering attendees for my wedding. This is somewhat a draft because I don't know if I'll be using Sinatra or Rails. I am going to be setting up scheduled emails to notify guests and myself of the wedding specifics.

So anyway here is the code I can't get anything to persist to the database using the insert syntax. Some code has been omitted. This is just pertaining to the POST issue.

    require "sinatra"
    require "sequel"
    require "sqlite3"


    DB = Sequel.connect('sqlite://pauley_wedding.db')

    DB.create_table! :attendees do 
     primary_key :id 
     string :fname
     string :lname
     string :phone
     string :email
    end

    @attendees = DB[:attendees]

here is the routes/views:

    get '/' do 
     erb :index, :layout => :layout
    end

    get '/about' do 
     erb :about, :layout => :layout
    end


    get '/rsvp' do 
      erb :rsvp, :layout => :layout
    end

    post '/rsvp' do
      @attendees.insert(:fname => params[:fname], :lname =>                                       params[:lname], :phone => 
    params[:phone], :email => params[:email]) 
redirect '/confirm_rsvp'
   end

    get '/confirm_rsvp' do 
        erb :confirmation, :layout => :layout
    end

    get '/admin' do 
        erb :admin, :layout => :layout
       @attendees
    end

    @@rsvp 
    <style>
    h1 {
        color: tan;
    }

    .lead {
        color: tan;
    }
    </style>
    <h1><p class="lead">Sign up!</p></h1>
    <form action="/rsvp" method="POST">
    <p class="lead">First Name:</p><br /><input type="text" name="fname"><br /><br />
    <p class="lead">Last Name:</p><br /><input type="text" name="lname"><br />   <br />
    <p class="lead">Phone Number:</p><br /><input type="text" name="phone"><br      /><br />
    <p class="lead">Email Address:</p><br /><input type="text" name="email">
    <hr />
    <button class="btn btn-primary" input type="submit">Sign Up!</button>

    </form>

Solution

  • It doesn't appear as though @attendees in the first code block is the same as @attendees in the second code block. In the first code block, @attendees is set in what appears to be top-level code. In the second code block, @attendees is used inside a route, which is going to be an instance of the Sinatra class you are using. You could try to use ATTENDEES (a constant) instead of an instance variable. You can also add a database logger require 'logger'; DB.loggers << Logger.new($stdout) to see what SQL Sequel is sending the database.