# Link\_to and url\_for in ActionMailer

**URL:** <https://www.ruby-forum.com/t/link-to-and-url-for-in-actionmailer/77502>\
**Category:** Rails\
**Created:** [November 28, 2006, 2:59pm UTC](https://www.ruby-forum.com/t/link-to-and-url-for-in-actionmailer/77502 "2006-11-28T14:59:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![reggieb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3e96dc/32.png) [@reggieb](https://www.ruby-forum.com/u/reggieb)\
**Post date:** [November 28, 2006, 2:59pm UTC](https://www.ruby-forum.com/t/link-to-and-url-for-in-actionmailer/77502/1 "2006-11-28T14:59:14Z")

</div>

I am new to ActionMailer and having a problem.

I want to be able to send a notification e-mail when a new item is added  
to a list. Sending an e-mail to the right people on creation is working  
fine.

However, I want to include in the e-mail a link to the application so  
that users can click on the link in the e-mail and immediately be taken  
to the show view for the new item. When I add a link\_to element to the  
e-mail template, I get the error message:

undefined method `url\_for’ for #JobRequestMailer:0x77fe458

I have tried adding a link to the application helper to the mailer  
model:

helper :application

but that doesn’t seem to have helped.

Can some one either tell me how to access an ActionView method from  
within an ActionMailer object, or provide another work around.

I know I could construct the anchor manually, but then I’ll need to  
rewrite it if the application moves.

---

<div class="post-metadata">

**Author:** ![reggieb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3e96dc/32.png) [@reggieb](https://www.ruby-forum.com/u/reggieb)\
**Post date:** [November 28, 2006, 3:57pm UTC](https://www.ruby-forum.com/t/link-to-and-url-for-in-actionmailer/77502/2 "2006-11-28T15:57:14Z")

</div>

El 28/11/2006, a las 14:59, Rob N.  
escribió:

> that users can click on the link in the e-mail and immediately be
> 
> but that doesn’t seem to have helped.
> 
> Can some one either tell me how to access an ActionView method from  
> within an ActionMailer object, or provide another work around.
> 
> I know I could construct the anchor manually, but then I’ll need to  
> rewrite it if the application moves.

That is exactly the point. link\_to and url\_for construct the url by  
knowing the hostname of the request. ActionMailer can’t know this  
unless you pass the url. You may want to build the url in the  
controller and pass the url to the deliver function.

If you are calling the mailer from a cron job you will have to put a  
fixed hostname, since it can’t guess it with no request object.

> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Jorge B. Ordovás [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://amedias.org/](http://amedias.org/) [ES]  
[http://koke.amedias.org/](http://koke.amedias.org/) [EN]

Warp Networks [http://www.warp.es/](http://www.warp.es/)  
María de Luna 11, 50018 Zaragoza,  
España
