Note that there are some explanatory texts on larger screens.

plurals
  1. POActiveModel and ActionMailer
    primarykey
    data
    text
    <p>Rails 3.1.2 with mail gem 2.3.0</p> <p>I'm trying to use ActionMailer with an ActiveModel based form. I've defined my table less model like so:</p> <pre><code>class StprodApp include ActiveModel::Validations include ActiveModel::Conversion extend ActiveModel::Naming attr_accessor :email,:name attr_reader :errors validates_presence_of :email, :message =&gt; "Please enter your email address" def initialize(attributes = {}) attributes.each do |name, value| send("#{name}=", value) end @errors = ActiveModel::Errors.new(self) end def persisted? false end end </code></pre> <p>So far so good, I can post a form based on stprod_app, and validations work</p> <p>So now I want to send an email based off this model, so I do this in my create action:</p> <pre><code>@stprod_app = StprodApp.new(params[:stprod_app]) if @stprod_app.valid? EmployeeMailer.stprod_app(@stprod_app).deliver end </code></pre> <p>I get this error</p> <pre><code>undefined method `index' for #&lt;StprodApp:0x007f95325a1ad0&gt; </code></pre> <p>and this stack trace</p> <pre><code>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' </code></pre> <p>So I read the docs for the mail gem, and it says this:</p> <p>---snip---</p> <p>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.</p> <p>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:').</p> <p>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.</p> <p>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.</p> <p>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.</p> <p>---snip---</p> <p>It looks like the index error is Rail's last desperate attempt to decode/encode? the email. The only 'index' I found in rails that makes sense is in ActiveSupport::Multibyte::Chars. </p> <p>It sounds like I have to implement an encode/decode method in my ActiveModel, but I can't figure out how!</p> <p>Anybody know how to define an ActiveModel that works with ActionMailer?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload