5

为什么我会得到“未定义的方法‘编码’”?我怎样才能解决这个问题?

错误信息

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

消息控制器.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

3 回答 3

10

我发现你得到了正确的密件抄送数组,可能你有一些错误的电子邮件地址。

当电子邮件地址在@ 之前包含一个点时,例如 test.@test.com,并且作为数组的一部分提交,您将获得数组的未定义方法“编码​​”。该错误仅出现在 Ruby 1.9 中,并且仅当地址作为数组提交时才会出现。

尝试这个

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
于 2013-07-25T09:57:32.870 回答
3

在你的CallMailer你因为 :bcc 只使用字符串而不是数组

你能行吗

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

谢谢

于 2013-07-25T09:39:56.870 回答
2

实际上密件抄送看起来像这样

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

所以你可以做一件事制作密件抄送
bcc = []
@user_emails.each do |email|
bcc << email + ","
end

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

谢谢

于 2013-07-25T09:51:39.767 回答