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 => "no-reply@mail.com"
def pledge_confirmation(pledge)
@pledge = pledge
mail(:to => pledge.email, :subject => "Thanks - pledge confirmation")
end
end
Log
So let’s say Sam Andreas at sacrificial.address@gmail.com 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"=>"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'
Struggling hard, but can’t see where the error is in pledge_confirmation.
Do you have any pointers? 🙂
Your code looks fine. The error is complaining about an “invalid multibyte char (US-ASCII)”. You’d better recreate
pledge_mailer.rbfrom scratch and save it with charset UTF-8 instead of US-ASCII.