Why am I getting the undefined "encoding" method?

Why am I getting the undefined `encoding 'method? How can i fix this?

Error message

NoMethodError (undefined method `encoding' for #<Array:0x000000218f61e8>): app/controllers/messages_controller.rb:255:in `deliver' 

messages_controller.rb

 # coding: UTF-8 class MessagesController < ApplicationController deliver users = User.confirmed.order("created_at ASC") @users_emails = [] users.each do |user| @users_emails += [user.email] end subject = params[:messages][:subject] body = params[:messages][:body] CallMailer.call_email(@users_emails, subject, body).deliver <= This is line 255 end end 

mailers / call _mailer.rb

 # coding: UTF-8 class CallMailer < ActionMailer::Base default :from => " dont-reply@example.com " def call_email(users_emails, mesesage_subject, mesesage_body) @users_emails = users_emails @mesesage_subject = mesesage_subject @mesesage_body = mesesage_body mail( :bcc => @users_emails, :subject => @mesesage_subject, :body => @mesesage_body) do |format| format.html end end end 
+4
source share
3 answers

I found that you are getting the correct array for the bcc, maybe you have the wrong email address.

When the email address contains a dot before @, for example test. @ test.com, and presented as part of the array, you get the undefined `encoding 'method for Array. The error appears only in Ruby 1.9, and only if the address is presented as an array.

try it

 def call_email(users_emails, mesesage_subject, mesesage_body) @users_emails = users_emails @mesesage_subject = mesesage_subject @mesesage_body = mesesage_body mail( :bcc => @users_emails.join(','), :subject => @mesesage_subject, :body => @mesesage_body) do |format| format.html end end 
+9
source

In your CallMailer you are the same as: bcc use the string not array

so what can you do

 @user_emails.each do |email| mail( :bcc => email, :subject => @mesesage_subject, :body => @mesesage_body) do |format| format.html end end 

thanks

+3
source

In fact, the bond will look as follows

  mail(:to => " rails@example.com " , :subject => "Example Rails" :bcc => [" bcc@rails.com ", "Rails Group < railsgroup@example.com >"] , :cc => " other@example.com " ) 

so you can do one thing by doing bcc bcc = []
@user_emails.each do |email|
bcc << email + ","
end

  mail(:to => " rails@example.com ",:subject => "Example Rails", :bcc => bcc, :cc => " other@example.com " ) 

thanks

+2
source

Source: https://habr.com/ru/post/1493406/


All Articles