As it turns out there appear to be dependency conflicts with tux and actionmailer, involving active model. The best solution in this case was to use an alternative - pony gem - for emailing.
Using ActionMailer with Sinatra, receiving nil object with mail method
-
18-06-2023 - |
Вопрос
I have a pretty lightweight blog, where I created a contact form. I decided to go with ActionMailer, but for whatever reason when I call the Mailer.contact method it is returning nil, so that I cant call the deliver method on it. The error is -undefined method `deliver' for nil:NilClass. This is in my app.rb
#app.rb
require "action_mailer"
require_relative 'mailer/init'
this is the file where I am configuring action mailer, its initializing fine, tested.
#mailer/init.rb
configure do
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'heroku.com',
:enable_starttls_auto => true
}
ActionMailer::Base.view_paths = File.expand_path('views')
end
class Mailer < ActionMailer::Base
def contact
mail(
to: "email@address.com",
from: "email",
subject: "subject") do |format|
format.html
end
end
end
Using tux when i call Mailer.contact it returns nil. I also noticed that ActionMailer::Base.view_paths is also returning nil, but I have this in a views folder -
#views/mailer/contact.html.erb
this is the email
I have no idea why I am not being returned a mail object when I call the Mailer.contact method, any help is much appreciated. Below is the full error stacktrace
NoMethodError - undefined method `deliver' for nil:NilClass:
/Users/Matts/projects/blog_site/routes/init.rb:169:in `block in <top (required)>'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/sinatra-1.4.4/lib/sinatra/base.rb:1593:in `call'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/sinatra-1.4.4/lib/sinatra/base.rb:1593:in `block in compile!'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/sinatra-1.4.4/lib/sinatra/base.rb:957:in `[]'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/sinatra-1.4.4/lib/sinatra/base.rb:957:in `block (3 levels) in route!'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/sinatra-1.4.4/lib/sinatra/base.rb:976:in `route_eval'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/sinatra-1.4.4/lib/sinatra/base.rb:957:in `block (2 levels) in route!'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/sinatra-1.4.4/lib/sinatra/base.rb:997:in `block in process_route'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/sinatra-1.4.4/lib/sinatra/base.rb:995:in `catch'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/sinatra-1.4.4/lib/sinatra/base.rb:995:in `process_route'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/sinatra-1.4.4/lib/sinatra/base.rb:955:in `block in route!'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/sinatra-1.4.4/lib/sinatra/base.rb:954:in `each'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/sinatra-1.4.4/lib/sinatra/base.rb:954:in `route!'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/sinatra-1.4.4/lib/sinatra/base.rb:1067:in `block in dispatch!'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/sinatra-1.4.4/lib/sinatra/base.rb:1049:in `block in invoke'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/sinatra-1.4.4/lib/sinatra/base.rb:1049:in `catch'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/sinatra-1.4.4/lib/sinatra/base.rb:1049:in `invoke'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/sinatra-1.4.4/lib/sinatra/base.rb:1064:in `dispatch!'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/sinatra-1.4.4/lib/sinatra/base.rb:889:in `block in call!'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/sinatra-1.4.4/lib/sinatra/base.rb:1049:in `block in invoke'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/sinatra-1.4.4/lib/sinatra/base.rb:1049:in `catch'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/sinatra-1.4.4/lib/sinatra/base.rb:1049:in `invoke'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/sinatra-1.4.4/lib/sinatra/base.rb:889:in `call!'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/sinatra-1.4.4/lib/sinatra/base.rb:877:in `call'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/rack-protection-1.5.2/lib/rack/protection/xss_header.rb:18:in `call'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/rack-protection-1.5.2/lib/rack/protection/base.rb:50:in `call'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/rack-protection-1.5.2/lib/rack/protection/base.rb:50:in `call'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/rack-protection-1.5.2/lib/rack/protection/path_traversal.rb:16:in `call'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/rack-protection-1.5.2/lib/rack/protection/json_csrf.rb:18:in `call'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/rack-protection-1.5.2/lib/rack/protection/base.rb:50:in `call'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/rack-protection-1.5.2/lib/rack/protection/base.rb:50:in `call'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/rack-protection-1.5.2/lib/rack/protection/frame_options.rb:31:in `call'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/rack-1.5.2/lib/rack/session/abstract/id.rb:225:in `context'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/rack-1.5.2/lib/rack/session/abstract/id.rb:220:in `call'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/rack-1.5.2/lib/rack/logger.rb:15:in `call'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/sinatra-1.4.4/lib/sinatra/base.rb:210:in `call'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/rack-1.5.2/lib/rack/head.rb:11:in `call'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/rack-1.5.2/lib/rack/methodoverride.rb:21:in `call'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/sinatra-1.4.4/lib/sinatra/show_exceptions.rb:21:in `call'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/sinatra-1.4.4/lib/sinatra/base.rb:180:in `call'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/sinatra-1.4.4/lib/sinatra/base.rb:2004:in `call'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/sinatra-1.4.4/lib/sinatra/base.rb:1469:in `block in call'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/sinatra-1.4.4/lib/sinatra/base.rb:1778:in `synchronize'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/sinatra-1.4.4/lib/sinatra/base.rb:1469:in `call'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/sass-3.2.14/lib/sass/plugin/rack.rb:54:in `call'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/rack-1.5.2/lib/rack/lint.rb:49:in `_call'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/rack-1.5.2/lib/rack/lint.rb:37:in `call'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/rack-1.5.2/lib/rack/showexceptions.rb:24:in `call'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/rack-1.5.2/lib/rack/commonlogger.rb:33:in `call'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/sinatra-1.4.4/lib/sinatra/base.rb:217:in `call'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/shotgun-0.9/lib/shotgun/loader.rb:86:in `proceed_as_child'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/shotgun-0.9/lib/shotgun/loader.rb:31:in `call!'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/shotgun-0.9/lib/shotgun/loader.rb:18:in `call'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/shotgun-0.9/lib/shotgun/favicon.rb:12:in `call'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/shotgun-0.9/lib/shotgun/static.rb:14:in `call'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/rack-1.5.2/lib/rack/builder.rb:138:in `call'
/usr/local/rvm/gems/ruby-2.0.0-p247@bridge/gems/rack-1.5.2/lib/rack/handler/webrick.rb:60:in `service'
/usr/local/rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
/usr/local/rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
/usr/local/rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'
So I went ahead and started commenting stuff out to see where the problem could be, this is my new app.rb, and it is still returning a nil object when i call Mailer.contact
# require 'dotenv'
# Dotenv.load
require "sinatra"
require "action_mailer"
# require "sinatra/activerecord"
# require "bcrypt"
# require "carrierwave"
# require "carrierwave/orm/activerecord"
# ActiveRecord::Base.establish_connection(ENV['DATABASE_URL'] || 'postgres://localhost/db_name')
# enable :sessions
# require_relative 'config/initializers/carrierwave'
# require_relative 'uploaders/image_uploader'
# require_relative 'models/models'
# require_relative 'helpers/helpers'
# require_relative 'routes/routes'
configure do
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'heroku.com',
:enable_starttls_auto => true
}
ActionMailer::Base.view_paths = File.expand_path('views')
end
class Mailer < ActionMailer::Base
def contact
mail(
to: "email@address.com",
from: "email",
subject: "subject") do |format|
format.html
end
end
end
UPDATE: so it turns out there is a conflict with action mailer and some of the other gems I was requiring. I think my best bet here is to try a different gem like pony.
Решение