Topic: ActiveModel and ActionMailer problem

On Rails 3.1.2 and mail gem 2.3.0 I have a problem

I'm using an ActiveModel based form, i.e.

class StprodApp 
  include ActiveModel::Validations
  include ActiveModel::Conversion
  extend ActiveModel::Naming
  attr_accessor :email,:name
  def initialize(attributes = {})
    attributes.each do |name, value|
      send("#{name}=", value)
    end    
    @errors = ActiveModel::Errors.new(self)
  end
  attr_reader :errors
  validates_presence_of :email, :message => "Please enter your email address"
  def persisted?
    false
  end

So far so good,  I create a table less form and it validates.

Now, I want to send an email using ActionMailer.  I do this:

@stprod_app = StprodApp.new(params[:stprod_app])
if @stprod_app.valid?
  EmployeeMailer.stprod_app(@stprod_app).deliver
end

I get this as an error message:

undefined method `index' for #<StprodApp:0x007f9533700d30>

and I get this in my stack trace

mail (2.3.0) lib/mail/encodings.rb:117:in `value_decode'
mail (2.3.0) lib/mail/encodings.rb:101:in `decode_encode'
mail (2.3.0) lib/mail/fields/unstructured_field.rb:74:in `do_decode'
mail (2.3.0) lib/mail/fields/unstructured_field.rb:56:in `decoded'
mail (2.3.0) lib/mail/fields/unstructured_field.rb:107:in `wrapped_value'
mail (2.3.0) lib/mail/fields/unstructured_field.rb:70:in `do_encode'
mail (2.3.0) lib/mail/fields/unstructured_field.rb:52:in `encoded'
mail (2.3.0) lib/mail/field.rb:123:in `method_missing'
mail (2.3.0) lib/mail/header.rb:190:in `block in encoded'
mail (2.3.0) lib/mail/header.rb:189:in `each'
mail (2.3.0) lib/mail/header.rb:189:in `encoded'
mail (2.3.0) lib/mail/message.rb:1708:in `encoded'
actionmailer (3.1.2) lib/action_mailer/base.rb:451:in `set_payload_for_mail'
actionmailer (3.1.2) lib/action_mailer/base.rb:431:in `block in deliver_mail'
activesupport (3.1.2) lib/active_support/notifications.rb:53:in `block in instrument'
activesupport (3.1.2) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.1.2) lib/active_support/notifications.rb:53:in `instrument'
actionmailer (3.1.2) lib/action_mailer/base.rb:430:in `deliver_mail'
mail (2.3.0) lib/mail/message.rb:230:in `deliver'
app/controllers/stprod_app_controller.rb:20:in `create'

reading the mail 2.3.0 README, it says this:

Encodings

If you didn't know, handling encodings in Emails is not as straight forward as you would hope.

I have tried to simplify it some:

All objects that can render into an email, have an #encoded method. Encoded will return the object as a complete string ready to send in the mail system, that is, it will include the header field and value and CRLF at the end and wrapped as needed.

All objects that can render into an email, have a :decoded method. Decoded will return the object's "value" only as a string. This means it will not include the header fields (like 'To:' or 'Subject:').

By default, calling #to_s on a container object will call its encoded method, while #to_s on a field object will call it's decoded method. So calling #to_s on a Mail object will return the mail, all encoded ready to send, while calling #to_s on the From field or the body will return the decoded value of the object. The header object of Mail is considered a container. If you are in doubt, call #encoded, or #decoded explicitly, this is safer if you are not sure.

Structured fields that have parameter values that can be encoded (e.g. Content-Type) will provide decoded parameter values when you call the parameter names as methods against the object.

Structured fields that have parameter values that can be encoded (e.g. Content-Type) will provide encoded parameter values when you call the parameter names through the object.parameters[''] method call.

From what I can make out of this,  I need to implement an 'encoded' and 'decoded' method for my ActiveModel object,  and it's simply a matter of doing so by calling to_s on each attribute,  and to_s for the entire model.

OR is it simpler?  The error message appears to be a last ditch effort by the mail gem to encode and/or decode parts of the ActionMailer message.  The only 'index' method in rails I can find that makes sense is in ActiveSupport::Multibyte::Chars.

So I either have to include/extend ActiveSupport::Multibyte::Chars in the ActiveModel definition, or define encoded and decoded????  So far, I've failed at either method of getting this to work.

I'm sure SOMEBODY has implemented an ActionMailer using an ActiveModel,  anybody know the secret?

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: ActiveModel and ActionMailer problem

SOLVED

So stupid.

I left off the crucial piece,  the call to mail:

I was doing this:

mail(:to=> @app.antibot, 
  :from=>@emp.email, 
  :cc => makecc(@emp),
  :subject=>"Short Term Production Insurance",
  :app=>@app) # <<< DOH!!!!!

Don't know why,  some kind of brain fart incurred when porting from rails 2.X to 3.X

mail only expects mail specific hash values like :to, :from, :subject, etc,  certainly NOT :app!!!!!

so ashamed! sad

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.