我有一个非常轻量级的博客,我在其中创建了一个联系表单。我决定使用 ActionMailer,但是无论出于何种原因,当我调用 Mailer.contact 方法时,它都返回 nil,因此我无法在其上调用 Deliver 方法。错误是 -undefined method `deliver' for nil:NilClass。这是在我的 app.rb

#app.rb
require "action_mailer"
require_relative 'mailer/init'

这是我配置 Action 邮件程序的文件,它的初始化很好,经过测试。
#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 protected]",
      from: "email",
      subject: "subject") do |format|
        format.html
    end
  end
end

当我调用 Mailer.contact 时使用 tux 它返回 nil。我还注意到 ActionMailer::Base.view_paths 也返回 nil,但我在一个 View 文件夹中有这个 -
#views/mailer/contact.html.erb
this is the email

我不知道为什么我在调用 Mailer.contact 方法时没有收到邮件对象,非常感谢任何帮助。以下是完整的错误堆栈跟踪
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'

所以我继续并开始评论内容以查看问题可能出在哪里,这是我的新 app.rb,当我调用 Mailer.contact 时它仍然返回一个 nil 对象
# 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 protected]",
      from: "email",
      subject: "subject") do |format|
        format.html
    end
  end
end

更新:所以事实证明与 action mailer 和我需要的其他一些 gem 存在冲突。我认为我在这里最好的选择是尝试一种不同的 gem ,比如小马。

最佳答案

事实证明,tux 和 actionmailer 似乎存在依赖冲突,涉及事件模型。在这种情况下,最好的解决方案是使用替代品 - pony gem - 发送电子邮件。

关于ruby-on-rails - 在 Sinatra 中使用 ActionMailer,使用邮件方法接收 nil 对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22539883/

10-14 17:34