为什么我会得到“未定义的方法`编码’”?
我怎样才能解决这个问题?
我怎样才能解决这个问题?
错误信息
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
邮寄/ 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
解决方法
我发现你正在为bcc获得正确的数组,可能你有一些错误的电子邮件地址.
当一个电子邮件地址在@之前包含一个点时,例如test.@ test.com,并作为一个数组的一部分提交,你得到一个未定义的方法`encoding’for Array.该错误仅出现在Ruby 1.9中,并且仅在地址作为数组提交时才出现.
试试这个
def call_email(users_emails,mesesage_body) @users_emails = users_emails @mesesage_subject = mesesage_subject @mesesage_body = mesesage_body mail( :bcc => @users_emails.join(','),:body => @mesesage_body) do |format| format.html end end