I am trying to setup a contact form using Rails 4.2's deliver_later method. However, I can only get deliver_now to work, as deliver_later is trying to serialize my object and fails each time.
Here's my setup:
messages_controller.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
message.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
The error I get when calling ContactMailer.contact_form(@message).deliver_later is:
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
Ideally I'd like this to be a background process. I will be adding something like Sidekiq soon but I think it's best I get this serialization problem fixed beforehand.
Any help is appreciated! Thanks :)
 
     
     
     
    