Contact form in ruby, sinatra, and haml

dcb picture dcb · Jan 14, 2010 · Viewed 43.3k times · Source

I'm new to all three, and I'm trying to write a simple contact form for a website. The code I have come up with is below, but I know there are some fundamental problems with it (due to my inexperience with sinatra). Any help at getting this working would be appreciated, I can't seem to figure out/find the documentation for this sort of thing.

haml code from the contact page:

%form{:name => "email", :id => "email", :action => "/contact", :method => "post", :enctype => "text/plain"}
  %fieldset
    %ol
      %li
        %label{:for => "message[name]"} Name:
        %input{:type => "text", :name => "message[name]", :class => "text"}
      %li
        %label{:for => "message[mail]"} Mail:
        %input{:type => "text", :name => "message[mail]", :class => "text"}
      %li
        %label{:for => "message[body]"} Message:
        %textarea{:name => "message[body]"}
    %input{:type => "submit", :value => "Send", :class => "button"}

And here is my code in sinatra's app.rb:

require 'rubygems'
require 'sinatra'
require 'haml'
require 'pony'

    get '/' do
        haml :index
    end 

    get '/contact' do
        haml :contact
    end

    post '/contact' do
        name = #{params[:name]}
        mail = #{params[:mail]}
        body = #{params[:body]}     
        Pony.mail(:to => '*emailaddress*', :from => mail, :subject => 'art inquiry from' + name, :body => body) 
    end

Answer

dcb picture dcb · Jan 15, 2010

I figured it out for any of you wondering:

haml:

%form{ :action => "", :method => "post"}
  %fieldset
    %ol
      %li
        %label{:for => "name"} Name:
        %input{:type => "text", :name => "name", :class => "text"}
      %li
        %label{:for => "mail"} email:
        %input{:type => "text", :name => "mail", :class => "text"}
      %li
        %label{:for => "body"} Message:
        %textarea{:name => "body"}
    %input{:type => "submit", :value => "Send", :class => "button"}

And the app.rb:

post '/contact' do
        name = params[:name]
        mail = params[:mail]
        body = params[:body]

        Pony.mail(:to => '*emailaddress*', :from => "#{mail}", :subject => "art inquiry from #{name}", :body => "#{body}")

        haml :contact
    end