17

我正在尝试使用 Rails 4.2 的 Deliver_later 方法设置联系表格。但是,我只能让 Deliver_now 工作,因为 Deliver_later 试图序列化我的对象并且每次都失败。

这是我的设置:

消息控制器.rb

class MessagesController < ApplicationController
  def new
    @message = Message.new
  end

  def create
    @message = Message.new(params[:message])
    if @message.valid?
      ContactMailer.contact_form(@message).deliver_later
      redirect_to root_path, notice: "Message sent! Thank you for contacting us."
    else
      render :new
    end
  end
end

contact_mailer.rb

class ContactMailer < ApplicationMailer
  default :to => Rails.application.secrets['email']

  def contact_form(msg)
    @message = msg
    mail(:subject => msg.subject, from: msg.email)
  end
end

消息.rb

class Message
    include ActiveModel::Model
    include ActiveModel::Conversion

    ## Not sure if this is needed ##
    include ActiveModel::Serialization

    extend ActiveModel::Naming

    attr_accessor :name, :subject, :email, :body

    validates_presence_of :email, :body
    validates_format_of :email, with: /\A([^\s]+)((?:[-a-z0-9]\.)[a-z]{2,})\z/i
    validates_length_of :body, :maximum => 1000

    def initialize(attributes = {})
      attributes.each { |name, value| send("#{name}=", value) }
    end

    ## Not sure if this is needed ##
    def attribtues
      {'name' => nil, 'subject' => nil, 'email' => nil, 'body' => nil}
    end
end

我打电话时遇到的错误ContactMailer.contact_form(@message).deliver_later是:

ActiveJob::SerializationError in MessagesController#create 

Unsupported argument type: Message
Extracted source (around line #10): 
if @message.valid?
  ContactMailer.contact_form(@message).deliver_later
  redirect_to root_path, notice: "Message sent! Thank you for contacting us."
else
  render :new

理想情况下,我希望这是一个后台进程。我很快就会添加类似 Sidekiq 的东西,但我认为最好事先解决这个序列化问题。

任何帮助表示赞赏!谢谢 :)

4

4 回答 4

12

为了使用您的类ActiveJob(这就是deliver_later委托),它需要能够通过其 ID 唯一地标识对象。此外,它需要在反序列化时通过ID稍后找到它(在邮件程序/作业中不需要手动反序列化)。

class Message
  ...
  include GlobalID::Identification
  ...

  def id
    ...
  end

  def self.find(id)
    ...
  end
end

ActiveRecord将为您提供这些方法,但由于您不使用它,您需要自己实现它。由您决定要将记录存储在哪里,但老实说,我认为使用ActiveRecord下面的表格会更好。

于 2015-01-12T09:37:45.753 回答
10

避免必须使用 ActiveRecord 支持对象或创建不必要的表的简单解决方案:

除了将 Message 对象传递给 contact_form 方法,您还可以将消息参数传递给 contact_form 方法,然后在该方法内初始化 Message 对象。

这将解决问题而无需创建表,因为您正在延迟作业工作者的内存空间中初始化对象。

例如:

消息控制器.rb

MessagesController < ApplicationController
    def new
        @message = Message.new
    end

    def create
        @message = Message.new(params[:message])

        if @message.valid?
            ContactMailer.contact_form(params[:message]).deliver_later
            redirect_to root_path, notice: "Message sent! Thank you for contacting us."
        else
            render :new
        end
    end
end

contact_mailer.rb

class ContactMailer < ApplicationMailer
    default :to => Rails.application.secrets['email']

    def contact_form(msg_params)
        @message = Message.new(msg_params)
        mail(:subject => msg.subject, from: msg.email)
    end
end
于 2015-09-30T07:39:56.720 回答
4

我今天遇到了类似的问题,并解决了如下。

  1. 将无表对象转换为 JSON 字符串
  2. 将其传递给邮寄者
  3. 将json字符串转换为hash

环境

  • 导轨 5.0.2

消息控制器.rb

class MessagesController < ApplicationController

  # ...

  def create
    @message = Message.new(message_params)
    if @message.valid?
      ContactMailer.contact_form(@message.serialize).deliver_later
      redirect_to root_path, notice: "Message sent! Thank you for contacting us."
    else
      render :new
    end
  end

  # ...
end

contact_mailer.rb

class ContactMailer < ApplicationMailer
  default :to => Rails.application.secrets['email']

  def contact_form(message_json)
    @message = JSON.parse(message_json).with_indifferent_access

    mail(subject: @message[:subject], from: @message[:email])
  end
end

消息.rb

class Message
  include ActiveModel::Model

  attr_accessor :name, :subject, :email, :body

  validates_presence_of :email, :body
  validates_format_of :email, with: /\A([^\s]+)((?:[-a-z0-9]\.)[a-z]{2,})\z/i
  validates_length_of :body, :maximum => 1000

  # Convert an object to a JSON string
  def serialize
    ActiveSupport::JSON.encode(self.as_json)
  end
end

希望这会帮助任何人。

于 2017-05-02T21:15:37.953 回答
0

您需要在传递给 AJ 之前序列化对象并在邮件程序中反序列化。

于 2015-01-12T09:24:10.540 回答