【问题标题】:undefined method `to_model' for #<ActionMailer::MessageDelivery:0xa6e7980>#<ActionMailer::MessageDelivery:0xa6e7980> 的未定义方法“to_model”
【发布时间】: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"
}

screenshot of the error

【问题讨论】:

  • app/controllers/static_pages_controller.rb:12:in `func' @Anthony

标签: ruby-on-rails ruby sendgrid


【解决方案1】:

你不能redirect_to一个邮件程序的send_mail函数。

代替

redirect_to TestMailer.send_email(lines, arr)

做事

TestMailer.send_email(lines, arr).deliver_now

在一个单独的行中,重定向到您在发送邮件后要转到的路径,或渲染您想要的视图,或者如果func 只是来自现有操作的方法调用,不要做任何事情,它会回到行动。

【讨论】:

  • 我试过了。错误消失了。但是现在没有发送/发送电子邮件。出了什么问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-14
  • 2021-10-28
相关资源
最近更新 更多