为什么 ActionMailer 不传送我的 mongoid 对象?
问题
我有一个应用程序,人们可以在其中对礼物做出承诺。一旦做出承诺,应向承诺人发送一封确认电子邮件。尝试发送邮件时,服务器发出500 Internal Server Error
。
上下文:
- Ruby on Rails 3.2.0
- Actionmailer 3.2.0
- Mongoid 2.4.3
- Thin 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
在 [电子邮件受保护]
承诺赠送 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
中的错误所在。
你有什么指点吗? :)
Problem
I have an app where one can make pledges against a gift. Once a pledge is made, an email confirmation should be sent to pledger. Server is issuing 500 Internal Server Error
when attempting to mail.
Context:
- Ruby on Rails 3.2.0
- Actionmailer 3.2.0
- Mongoid 2.4.3
- Thin 1.3.1
I've used Actionmailer on previous apps, but this is my first time working with Mongoid.
Code
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
Log
So let's say Sam Andreas
at [email protected]
pledged $42.42
for gift 4f2695009f5b7f3464000001
.
This happily saves to Mongodb and redirects to :root
. With adding the call to PledgeMailer
, we get:
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'
Struggling hard, but can't see where the error is in pledge_confirmation
.
Do you have any pointers? :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的代码看起来不错。该错误抱怨“无效的多字节字符(US-ASCII)”。您最好从头开始重新创建
pledge_mailer.rb
并使用字符集 UTF-8 而不是 US-ASCII 保存它。Your code looks fine. The error is complaining about an "invalid multibyte char (US-ASCII)". You'd better recreate
pledge_mailer.rb
from scratch and save it with charset UTF-8 instead of US-ASCII.