Thursday, 15 August 2013

ruby - 2 forms on same page, Sending using Pony in Sinatra, same email address -



ruby - 2 forms on same page, Sending using Pony in Sinatra, same email address -

i using pony.mail send mail service within sinatra, have 2 forms, 1 sends email address subscription newsletter , sec form contact form, both going through same action.

what trying accomplish if subscription field completed send params or if contact form completed , sent send params

heres come far, getting undefined method nil

post '/' require 'pony' pony.mail( :from => params[:name] || params[:subscribe], :to => 'myemailaddress', :subject => params[:name] + " has contacted via website" || params[:subscribe] + " has subscribed 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

is possible or each form have dealt individually?

thanks

there several stages i'd go through refactor code.

1. extract things changing (and create them more rubyish) post '/' require 'pony' = params[:name] || params[:subscribe] subject = "#{params[:name]} has contacted via website" || "#{params[:subscribe]} has subscribed 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. create clear intentions

in case, there 2 branches through code.

post '/' require 'pony' if params[:name] # contact form = params[:name] subject = "#{params[:name]} has contacted via website" else # subscription form = params[:subscribe] subject = "#{params[:subscribe]} has subscribed 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

(i'm not big fan of setting local vars within conditional branches, we'll ignore clarity. i'd create hash before conditional keys done, , populate in branches ymmv.)

3. extract doesn't alter does.

sinatra has configure block kind of thing.

require 'pony' configure :development 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

notice i've added :development may want set differently production.

now route lot cleaner , easier debug:

post '/' if params[:name] # contact form = params[:name] subject = "#{params[:name]} has contacted via website" else # subscription form = params[:subscribe] subject = "#{params[:subscribe]} has subscribed newsletter" end body = "#{params[:email]}#{params[:comment]}" pony.mail :from => from, :to => 'myemailaddress', :subject => subject, :body => body, redirect '/success' end

my lastly tip, set many of pony options env vars, not maintain things passwords out of source command allow alter settings lot easier. perhaps set them in rakefile , load different environments different contexts etc.

to utilize environment variables, following:

# rakefile # in method set env vars def basic_environment # load them in yaml file *not* in source command # specify them here # e.g. env["email_a"] = "me@example.com" end namespace :app desc "set environment locally" task :environment warn "entering :app:environment" basic_environment() end desc "run app locally" task :run_local => "app:environment" exec "bin/rackup config.ru -p 4630" end end # command line, i'd run `bin/rake app:run_local` # in sinatra app file configure :production # these actual settings utilize 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 # block different settings development configure :development # 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

i maintain of these setting in yaml file locally development, add together these production server directly. there lots of ways handle this, ymmv.

ruby forms sinatra pony

No comments:

Post a Comment