2017-04-06 36 views
0

我正在開發一個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" 
} 

screenshot of the error

+0

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

回答

0

你不能redirect_to郵件發送郵件功能。

而不是

redirect_to TestMailer.send_email(lines, arr) 

只是做

TestMailer.send_email(lines, arr).deliver_now 

在redirect_to的你想去發送郵件後,或使你想要的視圖,或路徑單獨行,如果func只是來自現有操作的方法調用,不做任何事情,並且它將返回到操作。

+0

我試過了。錯誤消失了。但電子郵件現在不發送/傳送。出了什麼問題? –

+0

你需要'deliver_now' ...看到我編輯的答案。 – SteveTurczyn

相關問題