# Routing of result not clear in ActionMailer

**URL:** <https://www.ruby-forum.com/t/routing-of-result-not-clear-in-actionmailer/151791>\
**Category:** Rails\
**Created:** [October 25, 2008, 7:31pm UTC](https://www.ruby-forum.com/t/routing-of-result-not-clear-in-actionmailer/151791 "2008-10-25T19:31:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![cleca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5f9b8f/32.png) [@cleca](https://www.ruby-forum.com/u/cleca)\
**Post date:** [October 25, 2008, 7:31pm UTC](https://www.ruby-forum.com/t/routing-of-result-not-clear-in-actionmailer/151791/1 "2008-10-25T19:31:18Z")

</div>

Dear all,

I am trying to understand the following behaviour of the ActionMailer  
in Rails 2.

I have an emailer\_controller.rb

class EmailerController \< ApplicationController  
def send\_mail  
Emailer::deliver\_contact\_email(params[:email])  
end  
end

I have a model emailer.rb

class Emailer \< ActionMailer::Base  
def contact\_email(email\_params, sent\_at = Time.now)  
@recipients = “removed\_email\_address@domain.invalid”  
@from = email\_params[:name] + " \<" + email\_params[:address] +  
“\>”  
@subject = email\_params[:subject]  
@sent\_on = sent\_at  
@body[“email\_body”] = email\_params[:body]  
@body[“email\_name”] = email\_params[:name]  
end  
end

and I send from within a form

\<%= form\_tag :action=\> “send\_mail”, :controller=\> “emailer” %\>  
… some details deleted here …  
  
\<%= %\>

and I have a send\_mail.html.erb template with some content (thanx for  
your mail…)

In this setup I get an error: Missing template emailer/  
contact\_email.html.erb

When I add an empty template of that name, everything works nicely.

My question is: WHY do I need a template with the same name as a  
method in the model? So far as I understand Ruby, I am expected to  
have a template for the controller method - but why for the model.

And: If I put some content into that file it is not displayed.

So: What happens with the content in emailer/contact\_email.html.erb ?

Thanx for helping out.

---

<div class="post-metadata">

**Author:** ![cleca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5f9b8f/32.png) [@cleca](https://www.ruby-forum.com/u/cleca)\
**Post date:** [October 25, 2008, 7:35pm UTC](https://www.ruby-forum.com/t/routing-of-result-not-clear-in-actionmailer/151791/2 "2008-10-25T19:35:24Z")

</div>

On Oct 25, 6:30 pm, X [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> In this setup I get an error: Missing template emailer/  
> contact\_email.html.erb
> 
> When I add an empty template of that name, everything works nicely.
> 
> My question is: WHY do I need a template with the same name as a  
> method in the model? So far as I understand Ruby, I am expected to  
> have a template for the controller method - but why for the model.

Where do you thing the content for the email is coming from? Just as  
actions in a controller are backed by template files so are methods in  
an ActionMailer object.

> And: If I put some content into that file it is not displayed.
> 
> So: What happens with the content in emailer/contact\_email.html.erb ?

It should form the body of the email sent.

Fred

---

<div class="post-metadata">

**Author:** ![cleca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5f9b8f/32.png) [@cleca](https://www.ruby-forum.com/u/cleca)\
**Post date:** [October 25, 2008, 8:11pm UTC](https://www.ruby-forum.com/t/routing-of-result-not-clear-in-actionmailer/151791/3 "2008-10-25T20:11:42Z")

</div>

> It should form the body of the email sent.

Ah, okay. Great concept and actually I now realize all testmails were  
empty…

But this just leads to the next problem: Shouldn’t I have access to  
the variables set in the model? Sorry probably I am doing something  
very stupid…

With both templates set to

We mail:  
Recipients: \<%= @recipients %\>  
From: \<%= @from %\>  
Subject: \<%= @subject %\>  
Sent on: \<%= @sent\_on %\>

I just get the text and NO access to the model neither in the one nor  
in the other file.

---

<div class="post-metadata">

**Author:** ![cleca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5f9b8f/32.png) [@cleca](https://www.ruby-forum.com/u/cleca)\
**Post date:** [October 25, 2008, 8:59pm UTC](https://www.ruby-forum.com/t/routing-of-result-not-clear-in-actionmailer/151791/4 "2008-10-25T20:59:12Z")

</div>

Ok, solved the first subpart.

In contact\_email.html.erb I do not have access to all variables of the  
mode but only to  
the contents of the hash @body. So this is working:

Body: \<%= @email\_body %\>  
Name: \<%= @email\_name %\>

Still I am not sure how to solve this for send\_mail.html.erb, but  
probably a render function is missing here or not correct.

---

<div class="post-metadata">

**Author:** ![cleca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5f9b8f/32.png) [@cleca](https://www.ruby-forum.com/u/cleca)\
**Post date:** [October 25, 2008, 9:02pm UTC](https://www.ruby-forum.com/t/routing-of-result-not-clear-in-actionmailer/151791/5 "2008-10-25T21:02:07Z")

</div>

On 25 Oct 2008, at 19:58, X wrote:

> Ok, solved the first subpart.
> 
> In contact\_email.html.erb I do not have access to all variables of the  
> mode but only to  
> the contents of the hash @body. So this is working:

Yup you got it.

> Body: \<%= @email\_body %\>  
> Name: \<%= @email\_name %\>
> 
> Still I am not sure how to solve this for send\_mail.html.erb, but  
> probably a render function is missing here or not correct.  
> What was the problem here?

---

<div class="post-metadata">

**Author:** ![cleca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5f9b8f/32.png) [@cleca](https://www.ruby-forum.com/u/cleca)\
**Post date:** [October 25, 2008, 9:33pm UTC](https://www.ruby-forum.com/t/routing-of-result-not-clear-in-actionmailer/151791/6 "2008-10-25T21:33:12Z")

</div>

On 25 Oct 2008, at 20:19, X wrote:

> The same problem (but it seems to have a different answer).
> 
> In contact\_email.html.erb I did not get access to variable @recipients  
> but only to variable @email\_body, looks like this is rendered off the  
> hash table @body. But in send\_mail.html.erb neither the one nor the  
> other works. I would, however, want to present the user with a copy of  
> the mail he sent via the form in the browser, so I need to know how  
> the send\_mail.html.erb template can access the email body.

the send\_mail template knows absolutely nothing of what happened in  
the mailer. By the time send\_mail.html.erb is rendered the  
actionmailer object with those instance variables probably doesn’t  
even exist anymore.  
Given that those values were just things pulled out of the params hash  
the send\_mail view/its associated controller action can just do the  
same thing.

Fred

---

<div class="post-metadata">

**Author:** ![cleca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5f9b8f/32.png) [@cleca](https://www.ruby-forum.com/u/cleca)\
**Post date:** [October 25, 2008, 9:39pm UTC](https://www.ruby-forum.com/t/routing-of-result-not-clear-in-actionmailer/151791/7 "2008-10-25T21:39:05Z")

</div>

Of course! 🙂

Thanx for the real quick support !!

---

<div class="post-metadata">

**Author:** ![cleca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5f9b8f/32.png) [@cleca](https://www.ruby-forum.com/u/cleca)\
**Post date:** [October 25, 2008, 9:28pm UTC](https://www.ruby-forum.com/t/routing-of-result-not-clear-in-actionmailer/151791/8 "2008-10-25T21:28:28Z")

</div>

> Yup you got it.

Thanx for your quick reply ! Great service 🙂

> > Still I am not sure how to solve this for send\_mail.html.erb, but  
> > probably a render function is missing here or not correct.
> 
> What was the problem here?

The same problem (but it seems to have a different answer).

In contact\_email.html.erb I did not get access to variable @recipients  
but only to variable @email\_body, looks like this is rendered off the  
hash table @body. But in send\_mail.html.erb neither the one nor the  
other works. I would, however, want to present the user with a copy of  
the mail he sent via the form in the browser, so I need to know how  
the send\_mail.html.erb template can access the email body.
