我正在开发一个使用RoR和Sendgrid发送批量电子邮件的Web应用程序。使用未定义的方法`to_model'为
发送和发送电子邮件但出现此错误。作为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。的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"
}
答案 0 :(得分:0)
您不能redirect_to
邮件程序send_mail功能。
而不是
redirect_to TestMailer.send_email(lines, arr)
只做
TestMailer.send_email(lines, arr).deliver_now
在另一行中redirect_to发送邮件后要转到的路径,或呈现所需的视图,或者如果func
只是来自现有操作的方法调用,则不要执行任何操作,并且它会回到行动。