问题
I'm creating a mail object like so:
textpart_to_inject= params[:text]
htmlpart_to_inject= params[:html]
message_all = Mail.new do
to #{params[:to]}
from #{params[:from]}
subject #{params[:subject]}
text_part do
body textpart_to_inject
end
html_part do
content_type 'text/html; charset=UTF-8'
body htmlpart_to_inject
end
end
I then want to insert that into the database like so:
@incoming_mail = IncomingMail.create(:message_all => Base64.encode64(message_all), :message_plain => Base64.encode64(params[:text]))
But that errors with:
TypeError (can't convert Mail::Message into String):
app/controllers/incoming_mails_controller.rb:56:in `create'
My database columns are as follows: message_all = bytea message_plain = text
Ideas? thanks
回答1:
Have you looked at Rails's serialize
method? It sounds like that's exactly what you want to do. See the docs here (also check out the section about 1/6 from the top under the heading "Saving arrays, hashes, and other non-mappable objects in text columns"). It might even be as simple as adding
serialize :message_all
serialize :message_plain
to your IncomingMail model, but don't take my word on that.
Your error, by the way, is coming up because Base64.encode64()
only works on strings.
Hope this helps!
来源:https://stackoverflow.com/questions/4870184/rails-storing-a-actionmail-mail-new-object-in-the-database