Contact form in ruby, sinatra, and haml
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
In case anyone can use this, here is what you might need to use your gmail account to send mail.
post '/contact' do require 'pony'Pony.mail( :name => params[:name], :mail => params[:mail], :body => params[:body], :to => 'a_lumbee@gmail.com', :subject => params[:name] + " has contacted you", :body => params[:message], :port => '587', :via => :smtp, :via_options => { :address => 'smtp.gmail.com', :port => '587', :enable_starttls_auto => true, :user_name => 'lumbee', :password => 'p@55w0rd', :authentication => :plain, :domain => 'localhost.localdomain' })redirect '/success' end
Note the redirect at the end, so you will need a success.haml to indicate to the user that their email was sent successfully.
Uhmm, i tried in irb the following:
foo = #{23}
Of course it wont work! the '#' is for comments in Ruby UNLESS it occurs in a string! Its even commented out in the syntax highlighting.What you wanted was:
name = "#{params[:name]}"
as you did in your solution (which is not necessary, as it already is a string).
Btw, the reason why the code does not throw an error is the following:
a =b =42
will set a and b to 42. You can even do some strange things (as you accidentally did) and set the variables to the return value of a function which takes these variables as parameters:
def foo(a,b) puts "#{a.nil?} #{b.nil?}" #outputs 'true true' return 42enda =b =foo(a,b)
will set a and b to 42.