0
votes

I am developing a web app to send bulk email using RoR and Sendgrid. Getting NoMethodError with undefined method `to_model' for #ActionMailer::MessageDelivery:0xa6e7980 Did you mean? to_yaml.

The emails are sent and delivered but this error shows up. Being new to rails and ruby I'm lost and can't seem to figure out what to do to remove this error even after digging the internet for nearly 6 hours. My code is as follows:
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

1
app/controllers/static_pages_controller.rb:12:in `func' @Anthony - Kushal Kale

1 Answers

0
votes

You can't redirect_to a mailer send_mail function.

Instead of

redirect_to TestMailer.send_email(lines, arr)

Just do

TestMailer.send_email(lines, arr).deliver_now

In a separate line redirect_to the path you want to go to after sending mail, or render the view you want, or if func is just a method call from an existing action, don't do anything, and it will return to the action.