# ActionMailer and url\_for in helper methods

**URL:** <https://www.ruby-forum.com/t/actionmailer-and-url-for-in-helper-methods/158058>\
**Category:** Rails\
**Created:** [January 18, 2009, 9:57pm UTC](https://www.ruby-forum.com/t/actionmailer-and-url-for-in-helper-methods/158058 "2009-01-18T21:57:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chuboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/439d5e/32.png) [@chuboy](https://www.ruby-forum.com/u/chuboy)\
**Post date:** [January 18, 2009, 9:57pm UTC](https://www.ruby-forum.com/t/actionmailer-and-url-for-in-helper-methods/158058/1 "2009-01-18T21:57:40Z")

</div>

Hi,

I was wondering how to get url\_for to work inside ActionMailer. I did  
research on the web but I can’t seem to find something that fits what I  
need.

In my application, each user can specify their custom domain to access  
their. I have an ActionMailer that sends them notifications of changes  
that occur. In there, I have a breadcrumbs helper method, which  
generages something like:

[1](https://www.ruby-forum.com/page/1) \> [2](https://www.ruby-forum.com/page/2)

I need it to be:

[1](http://customdomain/page/1) \> [2](http://customdomain/page/2)

How do you that? I tried using default\_url\_options but it doesn’t  
affect the helper methods I’m using in the email. Can somebody help me?

Thanks!  
Conrad

* * *

Here’s an excerpt of my code:

class UserNotifier \< ActionMailer::Base  
helper :application

```
# Emails the user the recent changes on the wiki

```

def changes(user, page)  
…  
default\_url\_options[:host] = user.custom\_host  
@body =\> {“page” =\> page}  
…  
end  
end

views/user\_notifer/changes.rhtml:  
\<%= breadcrumbs(@page) %\>

---

<div class="post-metadata">

**Author:** ![chuboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/439d5e/32.png) [@chuboy](https://www.ruby-forum.com/u/chuboy)\
**Post date:** [January 19, 2009, 5:13am UTC](https://www.ruby-forum.com/t/actionmailer-and-url-for-in-helper-methods/158058/2 "2009-01-19T05:13:31Z")

</div>

Use the :host option to make it build the whole URL, like this:

url\_for( {:host =\>‘[customerdomain.com](http://customerdomain.com)’, :controller =\> ‘page’, :id =\> 1  
} )

On Sun, Jan 18, 2009 at 2:57 PM, Conrad C. \<

---

<div class="post-metadata">

**Author:** ![chuboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/439d5e/32.png) [@chuboy](https://www.ruby-forum.com/u/chuboy)\
**Post date:** [January 19, 2009, 8:25pm UTC](https://www.ruby-forum.com/t/actionmailer-and-url-for-in-helper-methods/158058/3 "2009-01-19T20:25:53Z")

</div>

Unfortunately, my email is using helper methods that are also used  
throughout the application, so I don’t want to hardcode or pass around a  
host variable everywhere (because there are a lot of URLs being  
generated). I thought the ActionMailer::Base.default\_url\_options[:host]  
would help but it doesn’t seem to work inside the scope of helper  
methods.

Ryan W. wrote:

> Use the :host option to make it build the whole URL, like this:
> 
> url\_for( {:host =\>‘[customerdomain.com](http://customerdomain.com)’, :controller =\> ‘page’, :id =\> 1  
> } )
> 
> On Sun, Jan 18, 2009 at 2:57 PM, Conrad C. \<

---

<div class="post-metadata">

**Author:** ![chuboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/439d5e/32.png) [@chuboy](https://www.ruby-forum.com/u/chuboy)\
**Post date:** [January 19, 2009, 11:36pm UTC](https://www.ruby-forum.com/t/actionmailer-and-url-for-in-helper-methods/158058/4 "2009-01-19T23:36:54Z")

</div>

Perhaps this would be of use:

> **[Generating URLs with ActionMailer](https://matt.berther.io/2008/04/09/generating-urls-with-actionmailer/)**
>
> ActionMailer is a very handy class for sending emails from your rails application. However, the problem that typically arises is the sending of links to your site via rails using the url\_for method. Acording to the API documentation, this is because...

You may also need to pass ‘:only\_path =\> false’, since this was at one  
point  
a requirement with link\_to:

[http://dev.rubyonrails.org/ticket/11446](http://dev.rubyonrails.org/ticket/11446)

This is also fun:

[http://pivotallabs.com/users/nick/blog/articles/281-how-i-learned-to-stop-hating-and-love-action-mailer](http://pivotallabs.com/users/nick/blog/articles/281-how-i-learned-to-stop-hating-and-love-action-mailer)

HTH!

On Mon, Jan 19, 2009 at 1:25 PM, Conrad C. \<
