30

我是这三个方面的新手,我正在尝试为网站编写一个简单的联系表格。我提出的代码如下,但我知道它存在一些基本问题(由于我对 sinatra 缺乏经验)。获得这项工作的任何帮助将不胜感激,我似乎无法弄清楚/找到这类事情的文档。

来自联系页面的haml代码:

%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"}

这是我在 sinatra 的 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
4

5 回答 5

46

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
于 2010-01-15T02:42:54.347 回答
6

如果任何人都可以使用它,那么您可能需要使用 gmail 帐户发送邮件。

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

请注意最后的重定向,因此您需要一个 success.haml 来向用户表明他们的电子邮件已成功发送。

于 2011-11-30T00:45:49.673 回答
5

嗯,我在 irb 中尝试了以下内容:

foo = #{23}

当然行不通!'#' 用于 Ruby 中的注释,除非它出现在字符串中!它甚至在语法高亮中被注释掉了。你想要的是:

name = "#{params[:name]}"

正如您在解决方案中所做的那样(这不是必需的,因为它已经是一个字符串)。

顺便说一句,代码没有抛出错误的原因如下:

a =
b =
42

将 a 和 b 设置为 42。您甚至可以做一些奇怪的事情(就像您不小心做的那样)并将变量设置为将这些变量作为参数的函数的返回值:

def foo(a,b)
    puts "#{a.nil?} #{b.nil?}" #outputs 'true true'
    return 42
end
a =
b =
foo(a,b)

将 a 和 b 设置为 42。

于 2011-07-12T23:12:41.543 回答
2

#{} 是在“”中使用的插值。仅在外部使用它进行变量赋值是行不通的。

它更有可能像这样使用:

number_of_people = 15 

Puts "There are #{number_of_people} scheduled tonight" 
于 2016-05-12T00:41:28.190 回答
0

我在 github 上分两部分创建了一个示例。注册表单应用程序在这里:signup-form-heroku,与之交互的静态网站示例在这里:static-website-to-s3-example。表单应用程序是使用 Sinatra 构建的,可以直接部署到 Heroku。静态站点已准备好直接部署到 S3 并使用 amazon cloudfront。

于 2015-12-01T08:00:15.597 回答