ruby on rails - undefined method `to_model' for #<ActionMailer::MessageDelivery:0xa6e7980> -
i developing web app send bulk email using ror , sendgrid. getting nomethoderror undefined method `to_model' #actionmailer::messagedelivery:0xa6e7980 did mean? to_yaml.
the emails sent , delivered error shows up. being new rails , ruby i'm lost , can't seem figure out remove error after digging internet 6 hours. code follows:
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 root 'static_pages#home' '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 rails application. require_relative 'application' # initialize 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" }
you can't redirect_to
mailer send_mail function.
instead of
redirect_to testmailer.send_email(lines, arr)
just do
testmailer.send_email(lines, arr).deliver_now
in separate line redirect_to path want go after sending mail, or render view want, or if func
method call existing action, don't anything, , return action.
Comments
Post a Comment