gpt4 book ai didi

ruby - 同一页面上的 2 个表单,使用 Sinatra 中的 Pony 发送,相同的电子邮件地址

转载 作者:太空宇宙 更新时间:2023-11-03 17:55:29 25 4
gpt4 key购买 nike

我正在使用 Pony.mail 在 Sinatra 中发送邮件,我现在有两种形式,一种只发送用于订阅时事通讯的电子邮件地址,第二种形式是联系方式,两者都经过相同的操作.

我想要实现的是,如果订阅字段已完成,则仅发送这些参数,或者如果联系表已完成并发送,则发送这些参数

这是我到目前为止的想法,但是未定义的方法为 nil

 post '/' do
require 'pony'
Pony.mail(
:from => params[:name] || params[:subscribe],
:to => 'myemailaddress',
:subject => params[:name] + " has contacted you via the Website" || params[:subscribe] + " has subscribed to the newsletter",
:body => params[:email] + params[:comment],
:via => :smtp,
:via_options => {
:address => 'smtp.gmail.com',
:port => '587',
:enable_starttls_auto => true,
:user_name => 'myemailaddress',
:password => 'mypassword',
:authentication => :plain,
:domain => "localhost.localdomain"
})
redirect '/success'
end

这甚至可能吗?还是每个表单都必须单独处理?

谢谢

最佳答案

我会经历几个阶段来重构这段代码。

1。提取正在变化的东西(并使它们更像 Rubyish)

post '/' do
require 'pony'

from = params[:name] || params[:subscribe]
subject = "#{params[:name]} has contacted you via the Website" ||
"#{params[:subscribe]} has subscribed to the newsletter"
body = "#{params[:email]}#{params[:comment]}"

Pony.mail(
:from => from,
:to => 'myemailaddress',
:subject => subject,
:body => body,
:via => :smtp,
:via_options => {
:address => 'smtp.gmail.com',
:port => '587',
:enable_starttls_auto => true,
:user_name => 'myemailaddress',
:password => 'mypassword',
:authentication => :plain,
:domain => "localhost.localdomain"
})
redirect '/success'
end

2。明确你的意图

在这种情况下,代码有两个分支。

post '/' do
require 'pony'

if params[:name] # contact form
from = params[:name]
subject = "#{params[:name]} has contacted you via the Website"
else # subscription form
from = params[:subscribe]
subject = "#{params[:subscribe]} has subscribed to the newsletter"
end

body = "#{params[:email]}#{params[:comment]}"

Pony.mail(
:from => from,
:to => 'myemailaddress',
:subject => subject,
:body => body,
:via => :smtp,
:via_options => {
:address => 'smtp.gmail.com',
:port => '587',
:enable_starttls_auto => true,
:user_name => 'myemailaddress',
:password => 'mypassword',
:authentication => :plain,
:domain => "localhost.localdomain"
})
redirect '/success'
end

(我不太喜欢在条件分支中设置局部变量,但为了清楚起见,我们将忽略它。我可能会在条件分支之前创建一个散列,其中的键已经完成,然后将其填充到分支但 YMMV。)

3。从发生变化的事物中提取未发生变化的事物。

Sinatra 有一个 configure只是为了这种事情而阻止。

require 'pony'

configure :development do
set :email_options, {
:via => :smtp,
:via_options => {
:address => 'smtp.gmail.com',
:port => '587',
:enable_starttls_auto => true,
:user_name => 'myemailaddress',
:password => 'mypassword',
:authentication => :plain,
:domain => "localhost.localdomain"
}
end

Pony.options = settings.email_options

请注意,我添加了 :development,因为您可能希望针对生产对其进行不同的设置。

现在你的路线更干净,更容易调试:

post '/' do

if params[:name] # contact form
from = params[:name]
subject = "#{params[:name]} has contacted you via the Website"
else # subscription form
from = params[:subscribe]
subject = "#{params[:subscribe]} has subscribed to the newsletter"
end

body = "#{params[:email]}#{params[:comment]}"

Pony.mail
:from => from,
:to => 'myemailaddress',
:subject => subject,
:body => body,

redirect '/success'
end

我的最后一个技巧是将尽可能多的 Pony 选项放入 ENV 变量中,这不仅可以使密码之类的东西不受源代码控制,还可以让您更轻松地更改设置。也许将它们放在 Rakefile 中并为不同的上下文等加载不同的环境。


要使用环境变量,我执行以下操作:

# Rakefile

# in this method set up some env vars
def basic_environment
# I load them in from a YAML file that is *not* in source control
# but you could just specify them here
# e.g. ENV["EMAIL_A"] = "me@example.com"
end

namespace :app do

desc "Set up the environment locally"
task :environment do
warn "Entering :app:environment"
basic_environment()
end

desc "Run the app locally"
task :run_local => "app:environment" do
exec "bin/rackup config.ru -p 4630"
end
end

# from the command line, I'd run
`bin/rake app:run_local`

# in the Sinatra app file
configure :production do
# these are actual settings I use for a Heroku app using Sendgrid
set "email_options", {
:from => ENV["EMAIL_FROM"],
:via => :smtp,
:via_options => {
:address => 'smtp.sendgrid.net',
:port => '587',
:domain => 'heroku.com',
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:authentication => :plain,
:enable_starttls_auto => true
},
}
end

# then a block with slightly different settings for development
configure :development do
# local settings…
set "email_options", {
:via => :smtp,
:via_options => {
:address => 'smtp.gmail.com',
:port => '587',
:enable_starttls_auto => true,
:user_name => ENV["EMAIL_A"],
:password => ENV["EMAIL_P"],
:authentication => :plain,
:domain => "localhost.localdomain"
}
}
end

我通常将大部分这些设置保存在本地的 YAML 文件中用于开发,但直接将它们添加到生产服务器。有很多方法可以解决这个问题,YMMV。

关于ruby - 同一页面上的 2 个表单,使用 Sinatra 中的 Pony 发送,相同的电子邮件地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14750364/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com