Contact form in ruby, sinatra, and haml

左心房为你撑大大i 提交于 2019-12-03 02:54:18

问题


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

回答1:


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



回答2:


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.




回答3:


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 42
end
a =
b =
foo(a,b)

will set a and b to 42.




回答4:


#{} is interpolation that is used inside "". Just using it outside for a variable assignment won't work.

It would be more likely to be used like this:

number_of_people = 15 

Puts "There are #{number_of_people} scheduled tonight" 



回答5:


I've created an example of this in two parts that is available on github. The signup form app is here: signup-form-heroku and an example of the static website that interacts with this is here: static-website-to-s3-example. The form app is built using Sinatra and is ready to deploy straight onto Heroku. The static site is ready to deploy straight to S3 and use amazon cloudfront.



来源:https://stackoverflow.com/questions/2068148/contact-form-in-ruby-sinatra-and-haml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!