我正在開發一個web應用程序來使用RoR和Sendgrid發送批量電子郵件。獲取NoMethodError與未定義的方法`to_model'爲#ActionMailer :: MessageDelivery:0xa6e7980您的意思是? to_yaml。未定義的方法`to_model'爲#<ActionMailer :: MessageDelivery:0xa6e7980>
電子郵件已發送和交付,但顯示此錯誤。作爲rails和ruby的新手,我迷路了,似乎無法弄清楚在挖了近6個小時的互聯網之後如何去除這個錯誤。 我的代碼如下:
1. test_mailer.rb
class TestMailer < ApplicationMailer
include SendGrid
sendgrid_enable :ganalytics, :opentrack
default from: '[email protected]'
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 => "[email protected]",
:subject => "Hello User",
).deliver
end
end
2. routes.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. environment.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 –