gpt4 book ai didi

ruby-on-rails - 在 Sinatra 中使用 ActionMailer,使用邮件方法接收 nil 对象

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

我有一个非常轻量级的博客,我在其中创建了一个联系表。我决定使用 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@address.com",
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@address.com",
from: "email",
subject: "subject") do |format|
format.html
end
end
end

更新:事实证明与 Action 邮件程序和我需要的其他一些 gem 存在冲突。我认为我最好的选择是尝试像小马这样的不同 gem 。

最佳答案

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

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

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