Rails-在数据库中存储ActionMail Mail.new对象 [英] Rails - Storing a ActionMail Mail.new object in the database

查看:89
本文介绍了Rails-在数据库中存储ActionMail Mail.new对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在像这样创建一个邮件对象:

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]))

但是出现以下错误:

TypeError (can't convert Mail::Message into String):
  app/controllers/incoming_mails_controller.rb:56:in `create'

我的数据库列如下:
message_all = bytea
message_plain =文本

My database columns are as follows: message_all = bytea message_plain = text

想法?谢谢

推荐答案

您是否看过Rails的序列化方法?听起来这正是您想要做的。请参见文档此处(另请参阅有关从顶部1/6(标题为在文本列中保存数组,哈希和其他不可映射的对象)下。甚至可能像添加

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

到您的IncomingMail模型,但请不要相信我。

to your IncomingMail model, but don't take my word on that.

顺便说一句,由于 Base64.encode64()仅适用于字符串,因此您的错误将出现。

Your error, by the way, is coming up because Base64.encode64() only works on strings.

希望这会有所帮助!

这篇关于Rails-在数据库中存储ActionMail Mail.new对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆