ActionMailer在视图中发送身体不可用

时间:2010-05-12 01:51:29

标签: ruby-on-rails ruby actionmailer null

所以我有一个ActionMailer邮件

class ReportMailer < ActionMailer::Base
  def notify_doctor_of_updated_document(document)
    recipients  document.user.email_id
    from        "(removed for privacy)"
    subject     "Document #{document.document_number} has been updated and saved as #{document.status}"
    sent_on     Time.now
    body        :document => document
  end
end

并且视图是

Document
<%= @document.class %>

但在运行时

>> d = Document.last
=> #<Document id: "fff52d70-7ba2-11de-9b70-001ec9e252ed", document_number: "ABCD1234", procedures_count: 0, user_id: "630", created_at: "2009-07-28 18:18:07", updated_at: "2009-08-30 20:59:41", active: false, facility_id: 94157, status: "incomplete", staff_id: nil, transcriptionist_id: nil, job_length: nil, work_type: nil, transcription_date: nil, non_trans_edit_date: nil, pervasync_flag: true, old_id: nil>
>> ReportMailer.deliver_notify_doctor_of_updated_document(d)
=> #<TMail::Mail port=#<TMail::StringPort:id=0x8185326c> bodyport=#<TMail::StringPort:id=0x8184d6b4>>
从控制台

打印在日志中

Sent mail to (removed for privacy)

Date: Tue, 11 May 2010 20:45:14 -0500
From: (removed for privacy)
To: (removed for privacy)
Subject: Document ABCD1234 has been updated and saved as incomplete
Mime-Version: 1.0
Content-Type: multipart/alternative; boundary=mimepart_4bea082ab4ae8_aa4800b81ac13f5


--mimepart_4bea082ab4ae8_aa4800b81ac13f5
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: Quoted-printable
Content-Disposition: inline

Document
NilClass=

--mimepart_4bea082ab4ae8_aa4800b81ac13f5--

1 个答案:

答案 0 :(得分:1)

请告诉你控制员的动作。我最近申请中的一个例子就是这个。

class UserMailer < ActionMailer::Base  
  # Submission email
  def submission_batch(batch)
    recipients  batch.user.email
    from        "webmaster@gmail.com"
    subject     "Thank you for submitting a batch"
    body        :batch => batch
  end 
end

控制器动作/方法是

def update_samp
    @batch = Batch.find(params[:id])

     respond_to do |format|
       if @batch.update_attributes(params[:batch])
         UserMailer.deliver_submission_batch(@batch)
         flash[:success] = 'Batch and Sample details were successfully stored and an automated email briefing the details have been sent to your email. '
         format.html { redirect_to(@batch) }         
         format.xml  { head :ok }
       else
         format.html { render :action => "sample_production" }
         format.xml  { render :xml => @batch.errors, :status => :unprocessable_entity }
       end
     end
  end

然后我的视图如下

Hi <%= @batch.user.name %>
 and further you describe the email 

现在有些事我想提一下。

  1. 检查您的smtp设置。我使用Gmail发送邮件。我在开发和生产环境中设置了我的设置。

  2. 检查您发送的变量的名称/拼写。

  3. 确保html.erb文件与user_mailer.rb中定义的方法名称相同

  4. 希望这些事情有所帮助。

    干杯