Form handler using sinatra, config.ru

Hi,

Thanks for creating Middleman! It’s been excellent for helping me with a (mostly) static site I’m building. Now my client wants to add a contact form. I’ve written a Sinatra app to handle this, but I’m apparently not hooking it up properly.

Here’s my directory/file structure under the source directory:
index.haml
contact.haml
// other haml files
app.rb
config.ru
stylesheets/
javascripts/
// etc…

The app.rb has a route for ‘/contact’ to handle POST data, but submitting on the server just causes a 404 Not Found error.

Is config.ru supposed to be in another directory, or should these routes instead be added to the config.rb for middleman itself?

Thanks in advance for any tips whatsoever on this!
Chip

You should remove the config.ru, move app.rb to a new folder named lib.

In your config.rb:

require 'lib/app'
map("/my_form") { run App }

Where App is the class name of your sinatra class. Now your Sinatra app will live under /my_form

Thanks, that worked well locally.

Since I had to deploy this to an Apache/Passenger stack on my server, there was a slightly different setup there. I’ll describe what I did in case it helps anyone else…

Basically, the directory I deploy to is different than the app that handles the form POST data:

/var/www/example.com     # website
/var/www/sinatra-mail-form  # app for handling the form data

Locally, I have this main directory structure:

.
├── build
├── lib
│   └── sinatra-mail-form
│       └── tmp
└── source

My config.rb has this:
require ‘lib/sinatra-mail-form/mail_form’
map("/app") { run MailForm }

Here’s the sinatra-mail-form repo I created as a template on future projects as well:

I’m sure there’s probably a cleaner way of doing this. Since it took me a little tweaking to get it right, I wanted to share my solution in case anyway runs into trouble or has better suggestions.

Thanks again for your help as well as your efforts on middleman!
Chip