Ruby on rails (NoMethodError:main:Object的未定义方法`email';)。与Mandrill&;确认后发送欢迎电子邮件;邮差

Ruby on rails (NoMethodError:main:Object的未定义方法`email';)。与Mandrill&;确认后发送欢迎电子邮件;邮差,ruby-on-rails,ruby,devise,mailchimp,mandrill,Ruby On Rails,Ruby,Devise,Mailchimp,Mandrill,我目前正在覆盖Desive Confirmable方法,以便在用户确认其帐户后创建欢迎电子邮件。在当前设置下,在rails控制台中运行UserTransactionMailer.welcome_message(self.deliver_now)会导致以下错误: "NoMethodError: undefined method `email' for main:Object from /Users/AnthonyEmtman/Documents/projects/Team_Develop

我目前正在覆盖Desive Confirmable方法,以便在用户确认其帐户后创建欢迎电子邮件。在当前设置下,在rails控制台中运行UserTransactionMailer.welcome_message(self.deliver_now)会导致以下错误:

    "NoMethodError: undefined method `email' for main:Object
 from /Users/AnthonyEmtman/Documents/projects/Team_Development/kons/app/mailers/user_transaction_mailer.rb:6:in `welcome_message'" 
下面是
def确认覆盖用于触发发送欢迎消息电子邮件

models/user.rb:

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, 
         :confirmable, :lockable, :timeoutable, :zxcvbnable

  def confirm!
    send_welcome_message
    super
  end

private

  def send_welcome_message
    UserTransactionMailer.welcome_message(self).deliver_now
  end

end
class用户
以下文件是我的用户\u事务\u mailer.rb和基本\u mandrill\u mailer.rb文件。user_transaction_mailer.rb继承自base_mandrill_mailer.rb,因此我创建的所有未来邮件都可以访问mandrill_send方法,有效地减少了我每次需要写入mandrill_send方法的发送代码量

mailers/user\u transaction\u mailer.rb:

class UserTransactionMailer < BaseMandrillMailer

  def welcome_message(user, opts={})
    options = {
      :subject => "Welcome to Kontracking",
      :email => user.email,
      :global_merge_vars => [
        {
          name: "USER_NAME",
          content: user.user_name
        }
      ],
      :template_name => "Welcome Message - Kontracking"
    }

    mandrill_send options

  end

end
class CustomDeviseMailer < Devise::Mailer


  def confirmation_instructions(record, token, opts={})
    options = {
      :subject => "Confirmation Instructions",
      :email => record.email,
      :global_merge_vars => [
        {
          name: "confirmation_link",
          content: user_confirmation_url(confirmation_token: token)
          }
      ],
      :template_name => "Confirmation Instructions - Kontracking"
    }

    mandrill_send options
  end


  def reset_password_instructions(record, token, opts={})
    options = {
      :subject => "Password Reset Instructions",
      :email => record.email,
      :global_merge_vars => [
        { 
          name: "password_reset_link",
          content: reset_password_url(reset_password_token: record.reset_password_token)
        }
      ],
      :template_name => "Password Reset Instructions - Kontracking"
    }

    mandrill_send options
  end


  def unlock_instructions(record, token, opts={})
    options = {
      :subject => "Account Unlock Instructions",
      :email => record.email,
      :global_merge_vars => [
        {
          name: "account_unlock_link",
          content: user_unlock_url(unlock_token: token)
        }
      ],
      :template_name => "Account Unlock Instructions - Kontracking"
    }

    mandrill_send options
  end


  def mandrill_send(opts={})
    message = {
      :subject => "#{opts[:subject]}",
      :from_name => "Kontracking",
      :from_email => "anthony.emtman@kredibleinc.com",
      :to =>
        [{"name" => "Some User",
          "email" => "#{opts[:email]}",
          "type" => "to"}],
      :global_merge_vars => opts[:global_merge_vars]
    }
    sending = MANDRILL.messages.send_template opts[:template_name], [], message
    rescue Mandrill::Error => e
      Rails.logger.debug("#{e.class}: #{e.message}")
      raise
  end


end
class-UserTransactionMailer“欢迎来到Kontracking”,
:email=>user.email,
:全局\u合并\u变量=>[
{
名称:“用户名”,
内容:user.user\u名称
}
],
:template_name=>“欢迎消息-跟踪”
}
mandrill_发送选项
结束
结束
mailers/base_mandrill_mailer.rb:

require "mandrill"

class BaseMandrillMailer < ApplicationMailer

  def mandrill_send(opts={})
    message = {
      :subject => "#{opts[:subject]}",
      :from_name => "Kontracking",
      :from_email => "admin@kontracking.com",
      :to =>
        [{"name" => "Some User",
          "email" => "#{opts[:email]}",
          "type" => "to"}],
      :global_merge_vars => opts[:global_merge_vars]
    }
    sending = MANDRILL.messages.send_template opts[:template_name], [], message
    rescue Mandrill::Error => e
      Rails.logger.debug("#{e.class}: #{e.message}")
      raise
  end

end
需要“mandrill”
类BaseMandrillMailer“#{opts[:subject]}”,
:from_name=>“Kontracking”,
:from_email=>“admin@kontracking.com",
:to=>
[{“name”=>“某个用户”,
“email”=>“#{opts[:email]}”,
“键入”=>“到”}],
:global\u merge\u vars=>opts[:global\u merge\u vars]
}
sending=MANDRILL.messages.send_模板选择[:模板名称],],消息
rescue Mandrill::Error=>e
调试(“#{e.class}:#{e.message}”)
提升
结束
结束
运行UserTransactionMailer.welcome_message(User.first)。在rails控制台中立即交付将导致成功交付到我的电子邮件,包括正确处理要在电子邮件中显示的用户名的包含合并变量。我对哈希缺乏经验,目前无法找到解决未定义方法问题的方法(可能相当简单)。我如何让它正常工作


另外,我目前在初始值设定项中需要'mandrill',因此我应该能够从base_mandrill_mailer.rb文件中删除它,对吗?

我解决了我的问题,因为当前的实现总是保持自我未定义。修改models/user.rb confirm方法以包含消息的self和user修复了问题

models/user.rb:

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, 
         :confirmable, :lockable, :timeoutable, :zxcvbnable

  def confirm!
    send_welcome_message(self)
    super
  end

private

  def send_welcome_message(user)
    UserTransactionMailer.welcome_message(user).deliver_now
  end

end
我还为Mandrill设置了一个简单的初始值设定项(我使用的是Mandrill API——下面包含的文件)

初始值设定项/mandrill.rb:

require 'mandrill'

MANDRILL = Mandrill::API.new ENV['SMTP_PASSWORD']
config/environments/production.rb:

  # Do not dump schema after migrations.
  config.active_record.dump_schema_after_migration = false

  config.action_mailer.default_url_options = { host: ENV["SMTP_DOMAIN"] }
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.perform_deliveries = true
  config.action_mailer.smtp_settings = {
    address: ENV.fetch("SMTP_ADDRESS"),
    authentication: :plain
    domain: ENV.fetch("SMTP_DOMAIN"),
    enable_starttls_auto: true,
    password: ENV.fetch("SMTP_PASSWORD"),
    port: "587",
    user_name: ENV.fetch("SMTP_USERNAME")
  }
config/environments/development.rb:

  # Care if the mailer can't send.
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.delivery_method = :test
  host = 'localhost:3000'
  config.action_mailer.default_url_options = { host: host }
  config.action_mailer.perform_deliveries = true
config/application.yml:

SMTP_地址:SMTP.mandrillapp.com SMTP\u域:本地主机 SMTP_密码:“在此处插入Mandrill API密钥--无引号” SMTP_用户名:“在此处插入您的Mandrill用户名--无引号”

我用费加罗来管理这些。我设置了一个初始值设定项,因此如果服务器上没有设置它们,就会导致错误

初始化者/figaro.rb:

Figaro.require_keys("SMTP_ADDRESS", "SMTP_DOMAIN", 
  "SMTP_PASSWORD", "SMTP_USERNAME")
如果你有任何问题,请告诉我

Figaro.require_keys("SMTP_ADDRESS", "SMTP_DOMAIN", 
  "SMTP_PASSWORD", "SMTP_USERNAME")