为什么ActionMailer不提供我的mongoid对象?

时间:2012-02-08 03:31:43

标签: ruby-on-rails mongodb mongoid actionmailer

问题

我有一个应用程序可以对礼物做出承诺。一旦作出承诺,应将电子邮件确认发送给出质人。尝试邮件时,服务器正在发出500 Internal Server Error

上下文:

  • Ruby on Rails 3.2.0
  • Actionmailer 3.2.0
  • Mongoid 2.4.3
  • 瘦1.3.1

我在之前的应用上使用过Actionmailer,但这是我第一次使用Mongoid。

代码

class Pledge
  include Mongoid::Document
  field :name, :type => String
  field :email, :type => String
  field :amount, :type => Float

  embedded_in :gift
end 


class PledgesController < ApplicationController
  def create
    @gift = Gift.find(params[:gift_id])
    @pledge = @gift.pledges.new(params[:pledge])

    respond_to do |format|
      if @pledge.save
        PledgeMailer.pledge_confirmation(@pledge).deliver
        format.html { redirect_to :root, notice: 'Pledge successfully created.' }
      else
        ...
      end
    end
  end
...
end


class PledgeMailer < ActionMailer::Base
  default :from => "no-reply@mail.com"

  def pledge_confirmation(pledge)
    @pledge = pledge
    mail(:to => pledge.email, :subject => "Thanks - pledge confirmation")
  end
end

日志

所以,我要Sam Andreas sacrificial.address@gmail.com承诺$42.42赠送4f2695009f5b7f3464000001礼物:root

这很乐意保存到Mongodb并重定向到PledgeMailer。通过添加对Started POST "/gifts/4f2695009f5b7f3464000001/pledges" for 127.0.0.1 at 2012-02-08 14:13:21 +1100 Processing by PledgesController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"EwgOwALSb8uUsT4Ow1+RBvAEihXKXoqKS1JA9ZfpUfg=", "pledge"=>{"name"=>"Sam Andreas", "email"=>"sacrificial.address@gmail.com", "amount"=>"42.42"}, "commit"=>"Create Pledge", "gift_id"=>"4f2695009f5b7f3464000001"} MONGODB danspressie_development['gifts'].find({:_id=>BSON::ObjectId('4f2695009f5b7f3464000001')}).limit(-1).sort([[:_id, :asc]]) MONGODB danspressie_development['gifts'].update({"_id"=>BSON::ObjectId('4f2695009f5b7f3464000001')}, {"$push"=>{"pledges"=>{"_id"=>BSON::ObjectId('4f31e8519f5b7f41d1000002'), "name"=>"Sam Andreas", "email"=>"sacrificial.address@gmail.com", "amount"=>42.42}}}) Completed 500 Internal Server Error in 11ms SyntaxError (/Users/daniel/Dropbox/dev/src/danspressie/app/mailers/pledge_mailer.rb:4: invalid multibyte char (US-ASCII) /Users/daniel/Dropbox/dev/src/danspressie/app/mailers/pledge_mailer.rb:4: syntax error, unexpected $end, expecting keyword_end def pledge_confirmation(pledge) ^): app/controllers/pledges_controller.rb:8:in `block in create' app/controllers/pledges_controller.rb:6:in `create' 的调用,我们得到:

pledge_confirmation

苦苦挣扎,但无法看到{{1}}中的错误位置。

你有什么指针吗? :)

1 个答案:

答案 0 :(得分:2)

您的代码看起来很好。错误是抱怨“无效的多字节字符(US-ASCII)”。您最好从头开始重新创建pledge_mailer.rb并使用charset UTF-8而不是US-ASCII保存它。