Forum: Ruby on Rails Actionmailer woes

Posted by Pieter Hugo (pieterhugo)
on 2011-12-14 12:50
hi Guys

I have now spent hours on this and turn to you in desperation.

I am using Actionmailer to fetch emails, but I need to pass a userid to
the receive method, so that the receive method knows what user to pass
the email to. I tried:
(ATTEMPT 1)
class PokeMailer < ActionMailer::Base
  def receive(email,userid)
    ...
  end

I then invoke the receipt of email with

task = PokeMailer.receive(m.pop,userid)

But I get the error 'wrong number of arguments (2 for 1)'

So I though - ok - the actionmailer receive method must be expecting one
argument. Lets create another method to accept the userid and call the
receive method with only the raw email. So I wrote as second PokeMailer
method:
(ATTEMPT 2)
class PokeMailer < ActionMailer::Base
  def getemail(email,userid)
    @userid=userid
    PokeMailer.receive(email)
  end
  def receive(email)
    ..
  end
  ..
end

and call it with

task = PokeMailer.getemail(m.pop,userid)

Now I get the error 'undefined method `getemail' for PokeMailer:Class'

(kindof figured this out - changed def getemail to def self.getemail

My third attempt was to try and instantiate an instance of PokeMailer
with a initialize method:
(ATTEMPT 3)
class PokeMailer < ActionMailer::Base
  def intitalize (userid)
    @userid = userid
    super #not sure if this is needed?
  end
  def receive(email)
    ...
  end
  ...
end

and then call this with something like:

pm = PokeMailer.new(userid)
task = pm.receive(m.pop)

This fails because it seems that pm == nil - it doesn't event get
created?!!!

I am at whits' end. Please save me!

Thanks for anything - even sympathy would help ;)

Pieter Hugo
Posted by Colin Law (Guest)
on 2011-12-14 13:29
(Received via mailing list)
On 14 December 2011 11:50, Pieter Hugo <lists@ruby-forum.com> wrote:
> hi Guys
>
> I have now spent hours on this and turn to you in desperation.
>
> I am using Actionmailer to fetch emails, but I need to pass a userid to
> the receive method, so that the receive method knows what user to pass
> the email to. I tried:
> (ATTEMPT 1)
> class PokeMailer < ActionMailer::Base
> def receive(email,userid)

If you are trying to define a class method (rather than an instance
method) you need
def self.receive(..)
I think this may be the root of all the problems below also.

Colin

> argument. Lets create another method to accept the userid and call the
> end
>
> end
>
> You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscribe@googlegroups.com.
> For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.
>



--
gplus.to/clanlaw
Posted by Pieter Hugo (pieterhugo)
on 2011-12-14 15:35
Hi Colin

Thanks for the feedback. I kinda grokked that eventually. But I can't 
just turn my receive method into a class level method - because then the 
raw email string never gets turned into a tmail object. I any case the 
original code - which works, but doesnt pass my userid, is

class PokeMailer < ActionMailer::Base
  def receive(email)
    ...
  end

I then invoke the receipt of email with

task = PokeMailer.receive(m.pop)

Question - is this now a class method or an instance method? It gets 
called on the PokeMailer class, and responds, but its not defined with 
def self.receive.

I suspect my ruby skills are woefully inadequate.... :(

Any other suggestions?

Pieter
Posted by Colin Law (Guest)
on 2011-12-14 15:54
(Received via mailing list)
On 14 December 2011 14:35, Pieter Hugo <lists@ruby-forum.com> wrote:
> Hi Colin
>
> Thanks for the feedback. I kinda grokked that eventually. But I can't
> just turn my receive method into a class level method - because then the
> raw email string never gets turned into a tmail object. I any case the
> original code - which works, but doesnt pass my userid, is
>
> class PokeMailer < ActionMailer::Base
> def receive(email)

That is an instance method.  To call it you would need a PokeMailer
object and then call it via
my_poke_mailer.receive(..)

>  ...
> end
>
> I then invoke the receipt of email with
>
> task = PokeMailer.receive(m.pop)

You are asking here for a class method, so it will look for something
defined as
def self.receive()
Since you have not overridden that then presumably it will call an
existing method (I don't know PokeMailer so I can't comment on the
details).  It will not call your method.  Take your method out and see
if it still finds a method.

>
> Question - is this now a class method or an instance method? It gets
> called on the PokeMailer class, and responds, but its not defined with
> def self.receive.

not sure what you are asking here.  If you want to find out which
method it is calling have a look at the Rails Guide on debugging, it
will show you how to use ruby-debug to break into your code, you can
then step into the function and see where it goes.

>
> I suspect my ruby skills are woefully inadequate.... :(

So are mine, just possibly slightly less so.  The problem is that the
problem one is tackling often seems to require just a little more
knowledge than one's current state. :)

Colin
Posted by Pieter Hugo (pieterhugo)
on 2011-12-14 16:14
Hi Colin

Thanks for bearing with me. Your thinking is spot on in agreement with
how I understand ruby. And yet

> class PokeMailer < ActionMailer::Base
> def receive(email)
>  ...
> end
>
> I then invoke the receipt of email with
>
> task = PokeMailer.receive(m.pop)

works 100%

I tried creating an instance of PokeMailer (see first post)

pm = PokeMailer.new(userid)

But it just returns nil, and pm.receive says nil doesn't have a receive
method.

I think its something to do with Actionmailer - but it definitely doesnt
behave like a normal class.

:(
Posted by Colin Law (Guest)
on 2011-12-14 16:22
(Received via mailing list)
On 14 December 2011 15:14, Pieter Hugo <lists@ruby-forum.com> wrote:
>> I then invoke the receipt of email with
>>
>> task = PokeMailer.receive(m.pop)
>
> works 100%

What happens if you comment out your receive method and then run the 
code?

Colin

>
>
--
gplus.to/clanlaw
Posted by Frederick Cheung (Guest)
on 2011-12-14 19:24
(Received via mailing list)
On 14 Dec 2011, at 15:14, Pieter Hugo <lists@ruby-forum.com> wrote:

>> I then invoke the receipt of email with
> method.
>
> I think its something to do with Actionmailer - but it definitely doesnt
> behave like a normal class.

There's a method_missing handler which handles calls to class methods 
that aren't there by creating new instances and dispatching the method 
to the newly created instance

Fred
Posted by Pieter Hugo (pieterhugo)
on 2011-12-15 08:37
Thanks for all the help guys. I got a solution in the end. I created a 
class level variable called @@userid which I then set in my 
getemail(email,userid) method before calling receive(email). Nasty, and 
not what I'd have expected, but it works.

Regards

Pieter
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.