問題
我有一個應用程序,在那裏可以對一個禮物作出承諾。一旦做出承諾,應該向出質人發送確認電子郵件。嘗試發送郵件時,服務器正在發出500 Internal Server Error
。爲什麼ActionMailer不能提供我的mongoid對象?
語境:
- 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 => "[email protected]"
def pledge_confirmation(pledge)
@pledge = pledge
mail(:to => pledge.email, :subject => "Thanks - pledge confirmation")
end
end
日誌
所以我們說Sam Andreas
在[email protected]
承諾$42.42
禮品4f2695009f5b7f3464000001
。
這愉快地保存到Mongodb並重定向到:root
。隨着加入呼叫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"=>"[email protected]", "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"=>"[email protected]", "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
。
你有什麼建議嗎? :)
嘗試使用'cat -v pledge_mailer.rb',它應該顯示任何奇怪字節的位置。 – 2012-02-08 03:46:32
@ muistooshort謝謝!我沒有發現其他字節。但我按照Mischa的回答重新創建了它,並解決了問題。 – 2012-02-08 04:15:21