【发布时间】:2017-04-06 11:07:21
【问题描述】:
我正在开发一个使用 RoR 和 Sendgrid 发送批量电子邮件的网络应用程序。使用 undefined method `to_model' for #ActionMailer::MessageDelivery:0xa6e7980 获取 NoMethodError 您的意思是? to_yaml.
电子邮件已发送并已送达,但出现此错误。作为 Rails 和 ruby 的新手,我迷路了,即使在互联网上挖掘了近 6 个小时后,我似乎也无法弄清楚如何消除这个错误。
我的代码如下:
1. test_mailer.rb
class TestMailer < ApplicationMailer
include SendGrid
sendgrid_enable :ganalytics, :opentrack
default from: 'abc@company.com'
def send_email(e_arr, s_arr)
@e_arr = e_arr
@s_arr = s_arr
headers['X-SMTPAPI'] = { :to => @e_arr }.to_json
mail(
:to => "this.is@ignored.com",
:subject => "Hello User",
).deliver
end
end
2。路线.rb
Rails.application.routes.draw do
root 'static_pages#home'
get 'static_pages/home'
post 'static_pages/home', to: 'static_pages#func'
end
3.static_pages_controller.rb
class StaticPagesController < ApplicationController
skip_before_action :verify_authenticity_token
def home
end
def func
postvar = params[:Emails];
lines = postvar.split("\n");
arr = [];
lines.each {|line|
arr.push(line.split("@")[0]);
}
redirect_to TestMailer.send_email(lines, arr)
end
end
4。环境.rb
# Load the Rails application.
require_relative 'application'
# Initialize the Rails application.
Rails.application.initialize!
ActionMailer::Base.smtp_settings = {
:address => "smtp.sendgrid.net",
:port => 25,
:domain => "abc.com",
:authentication => :plain,
:user_name => "xyz",
:password => "pqr"
}
【问题讨论】:
-
app/controllers/static_pages_controller.rb:12:in `func' @Anthony
标签: ruby-on-rails ruby sendgrid